0

I have faced a task where I need to:

  1. read pdf template (no pictures, simple text with one table at the bottom for signature)
  2. recognize specific text
  3. replace specific text with data from system
  4. generate new pdf with replaced text to user

At first I have tried Lowagie library which from 2... version moved to itext Then used Itext Then moved to PDFBox

I saw many examples in stackoverflow and such where point source pdf, destination pdf, word to change Copy it, paste it, make classes imports but the new pdf is generated without any changes

Am trying on these libs:

// PDFBox generator
compile 'org.apache.pdfbox:pdfbox:2.0.9'
// Itext generator
compile 'com.itextpdf:itext-pdfa:5.5.10'
compile 'com.itextpdf:itextg:5.5.9'
// Lowagie generator
compile group: 'com.lowagie', name: 'itext', version: '2.1.7'

Maybe libs are changing and this causes some functions not to work, but can you suggest a working solution for you?

Have tried these covnertions:

private void processPDF(String src, String dest) throws IOException, DocumentException {

    PdfReader reader = new PdfReader(src);
    PdfDictionary dict = reader.getPageN(1);
    PdfObject object = dict.getDirectObject(PdfName.CONTENTS);

    if(object instanceof PRStream){
        PRStream stream = (PRStream)object;
        byte[] data = PdfReader.getStreamBytes(stream);
        String dd = new String(data, "UTF8")
                .replace("text", "replacedText");
        stream.setData(dd.getBytes("UTF8"));
    }

    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

private void processPDF3(String src, String dest) throws InvalidPasswordException, IOException {
    Map<String, String> map = new HashMap<>();
    map.put("text", "replacedText");
    File template = new File(src);
    PDDocument document = PDDocument.load(template);
    List<PDField> fields = document.getDocumentCatalog().getAcroForm().getFields();
    for (PDField field : fields) {
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (entry.getKey().equals(field.getFullyQualifiedName())) {
                field.setValue(entry.getValue());
                field.setReadOnly(true);
            }
        }
    }
    File out = new File(dest);
    document.save(out);
    document.close();
}

Also used this: iText Add values to placeholders in PDF cover page dynamically

And variations of this: PDFBox 2.0 RC3 -- Find and replace text

Would be very thanksful for your help

Bronius Janonis
  • 75
  • 2
  • 12
  • 2
    Why was the ReplaceText example removed? https://pdfbox.apache.org/2.0/migration.html#why-was-the-replacetext-example-removed – Tilman Hausherr Jun 19 '18 at 10:12
  • 1
    Please read the comments to this question: https://stackoverflow.com/questions/50916949/what-is-the-best-way-to-replace-text-in-a-pdf Also: you have copied an example from one of my books without reading the warnings I wrote down when I commented on that example. You can either accept that your requirement can't be met, or spend some more weeks on research to come to the same conclusion. It's entirely up to you. By the way: Lowagie is the name of a person (me); **NOT** the name of a product. I don't appreciate it when people talk about Lowagie library as is Lowagie were a thing, not a person. – Bruno Lowagie Jun 19 '18 at 12:09
  • @BrunoLowagie thank you for your response, I am reading a lot of information and I noticed these aspects of yours yet was thinking of some workarounds as I need to filter only some specific words like { <$projectName/> ] TJ or similar Sometimes I miss important points (it happens) I have not digged so deep into the creation of the library and in case I offended you, excuse me, that was not my intention Although it is good to get feedback from the creator of the lib - thank you – Bronius Janonis Jun 19 '18 at 12:45
  • 2
    PDF is not a good format for creating templates. Every snippet of text in a PDF document is added to a page at an absolute position. If you replace a small snippet, say `<$date>` with a long snippet, say `Tuesday, June 19, 2018`, your complete layout will be screwed. That's why we advise to create tempates in HTML as described here: https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-5-custom-tag-workers-and-css-appliers – Bruno Lowagie Jun 19 '18 at 15:53
  • 1
    ... and if you have to resort to pdf templates, make the variables form fields. – mkl Jun 19 '18 at 22:11

0 Answers0