IText Android - add text to an existing PDF file

We have a PDF file with some fields for collecting some data, and I have to programmatically use iText on Android by adding text to these positions. I thought of different ways to achieve this, there is little success in each of them.

Note. For most of my tests, I use the version of iText (iTextG 5.5.4) for Android and Samsung Galaxy Note 10.1 2014 (Android 4.4).

  • The approach that I took from the very beginning was to "draw" the text at the given coordinates for this page. This has some problems with managing fields (I need to know the length of the lines, and it would be difficult to place each text in the exact pdf coordinate). But most importantly, the performance of the process is very slow in some devices / OSVersions (works fine in Nexus 5 with 5.0.2, but takes a few minutes with 5 MB Pdf on note 10.1).

    pdfReader = new PdfReader(is); document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); pdfCopy = new PdfCopy(document, baos); document.open(); PdfImportedPage page; PdfCopy.PageStamp stamp; for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) { page = pdfCopy.getImportedPage(pdfReader, i); // First page = 1 stamp = pdfCopy.createPageStamp(page); for (int i=0; i<10; i++) { int posX = i*50; int posY = i*100; Phrase phrase = new Phrase("Example text", FontFactory.getFont(FontFactory.HELVETICA, 12, BaseColor.RED)); ColumnText.showTextAligned(stamp.getOverContent(), Element.ALIGN_CENTER, phrase, posX, posY, 0); } stamp.alterContents(); pdfCopy.addPage(page); } 
  • However, we add “form fields” instead of drawing. That way, I can customize the TextField and not control the texts themselves. However, the final PDF file should not contain any annotations, so I will need to copy it to a new Pdf without annotations and with these “form fields”. I do not have an example of this, because I could not do it, I don’t even know if it is possible / worth it.

  • The third option is to get the Pdf with the "form fields" already added, so I need to fill them out. However, I still need to create a new Pdf with all these fields and without annotations ...

I would like to know what is the best way to complete this process and any help in achieving it. I am really new to iText and any help would be greatly appreciated.

Thanks!

EDIT

In the end, I used the third option: a PDF with editable fields that we fill out, and then we use “alignment” to create an immutable PDF file with all existing texts.

The code is as follows:

  pdfReader = new PdfReader(is); FileOutputStream fios = new FileOutputStream(outPdf); PdfStamper pdfStamper = new PdfStamper(pdfReader, fios); //Filling the PDF (It totally necessary that the PDF has Form fields) fillPDF(pdfStamper); //Setting the PDF to uneditable format pdfStamper.setFormFlattening(true); pdfStamper.close(); 

and form filling method:

 public static void fillPDF(PdfStamper stamper) throws IOException, DocumentException{ //Getting the Form fields from the PDF AcroFields form = stamper.getAcroFields(); Set<String> fields = form.getFields().keySet(); for(String field : fields){ form.setField("name", "Ernesto"); form.setField("surname", "Lage"); } } } 

The only thing related to this approach is that you need to know the name of each field to fill it out.

+5
source share
1 answer

IText has a process known as "flattening" that takes form fields and replaces them with text that contains fields.

I have not used iText after a few years (and not at all on Android), but if you are looking for a manual or online examples for “smoothing”, you should find how to do it.

+4
source

Source: https://habr.com/ru/post/1214736/


All Articles