How can one find and replace text inside a PDF document using PDFBox 2.0, they pulled the old example and it\'s syntax no longer works so I am wondering if it\'s still possi
I spent much time on coming up with a solution for this and ended up acquiring an Acrobat DC subscription so that I could create fields as placeholders for the text to be replaced. These fields in my case, were for customer information and order details so it was not very complex data, but the document was filled with pages of business related conditions and had a very complex layout.
Then I simply did this, which may be suitable for you.
private void update() throws InvalidPasswordException, IOException {
Map map = new HashMap<>();
map.put("fieldname", "value to update");
File template = new File("template.pdf");
PDDocument document = PDDocument.load(template);
List fields = document.getDocumentCatalog().getAcroForm().getFields();
for (PDField field : fields) {
for (Map.Entry entry : map.entrySet()) {
if (entry.getKey().equals(field.getFullyQualifiedName())) {
field.setValue(entry.getValue());
field.setReadOnly(true);
}
}
}
File out = new File("out.pdf");
document.save(out);
document.close();
}
YMMV