Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with replacing a String in PDF using PDFBox

 
Andrew Carney
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need to change an existing text in a PDF document.
I have started experimenting with Apache PDFBox and I am able to read the content of the PDF as text into a String using PDFTextStripper however I can't find the relevant API to write the amended String back into the file.
I am able to create new PDF with the new String however the new PDF is missing the styling and fonts that existed in the original PDF since PDFTextStripper only extracts the text (as the name suggests...)
I have found in the example folder a class called ReplaceString that is supposed to be doing exactly what I'm looking for but when I run it on a PDF it creates a very messy PDF and doesn't even do what it supposed to do.
I have decompiled that class and even there I don't see how the new PDF retains the styling and fonts that existed in the original PDF.
I know that there are other open source libraries out there (such as iText) however I don't think it has the same licensing as Apache has and therefore my company prefers not to use it.
This is as far as I have gone so far, would be great if someone can advise from his own experience how to achieve my goal using PDFBox.
 
Chintan Patel.
Greenhorn
Posts: 1
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use this code to replace string in PDF.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic