This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Other Open Source Projects and the fly likes PdfBox - how to Replace String with double value in pdf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "PdfBox - how to Replace String with double value in pdf" Watch "PdfBox - how to Replace String with double value in pdf" New topic
Author

PdfBox - how to Replace String with double value in pdf

Krishna Rajendran
Greenhorn

Joined: Oct 17, 2011
Posts: 5
My requirement:

Your loan amount @loanamount is approved.

I wanted to replace @loanamount to 5,00,000. If i try with pdfbox api,i am not able to replace. i am getting empty space in pdf.
If i use same api and if try to replace @loanamount to abcd. now its replacing

replaceFirst("@loanamount","5,00,00"); - not able to replace. getting empty content in pdf
replaceFirst("@loanamount","abcd"); - able to replace.
Could anyone help me out?

Can anyone of you suggest me anyother open source api to replace string in pdf?

Thanks in Advance...:)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You're using a string in both cases, so it's quite likely that your problem diagnosis is incorrect.

And you haven't given us enough information to help out. In fact you've given us nothing but two bare lines of code. A full example of code which does and doesn't work would be much more useful.
Krishna Rajendran
Greenhorn

Joined: Oct 17, 2011
Posts: 5
Thanks for your quick response Paul.

Please see the below code.


sample content in pdf:
Dear ABC,

Your loan amount @loanamount is approved.

Regards,
DEF

Output:Your loan amount is approved.
I m getting empty spaces instead of @loanamount
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
I don't know what op.getOperation() = "TJ" and op.getOperation() = "Tj" mean, but since they're apparently different things I'm not too surprised that the result would be different.


Ping & DNS - my free Android networking tools app
Krishna Rajendran
Greenhorn

Joined: Oct 17, 2011
Posts: 5
Thanks for your response. Ulf

//Tj and TJ are the two operators that display strings in a PDF

if (op.getOperation().equals("Tj"))
{
// Tj takes one operator and that is the string
// to display so lets update that operator
COSString previous = (COSString) tokens.get(j - 1);

System.out.println("COSString::"+previous);

String string = previous.getString();

string = string.replace("@loanamount", "5,00,000");

//Word you want to change. Currently this code changes word "Solr" to "Solr123"
previous.reset();



previous.append(string.getBytes());

}

When i run this code, only Tj loop (if loop) is getting called.

TJ loop is not getting called. so we no need to worry about TJ right now.
Krishna Rajendran
Greenhorn

Joined: Oct 17, 2011
Posts: 5
Could anyone help me out ?

Please suggest me java open source api to replace caption or content in existing pdf.

Thanks in advance.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41634
    
  55
It would be very useful to have an SSCCE and a PDF to go with it. As it is, the problem is a bit abstract.

Fundamentally, PDFs are not meant for this kind of editing; I think it's quite possible that you won't find a free solution for this; I'm surprised you even got this to work (sort of).
 
Consider Paul's rocket mass heater.
 
subject: PdfBox - how to Replace String with double value in pdf