0
votes

so I've been trying to generate a report. I've tried quite a few things already but there always seems to be problems. I'm currently trying iTextSharp 4.1.6.

My current strategy is to use LibreOffice to create a document with editable pdf fields, or I guess they are called "AcroFields". I'm not sure since I can't find a definition. But anyways, I assume that all of these are "AcroFields":

enter image description here

But if I put all of those into a form and export as pdf only some of them show up as AcroFields:

var reader = new PdfReader(File.ReadAllBytes("abc.pdf"));
foreach(var field in reader.AcroFields.Fields)
{
    Console.WriteLine(((DictionaryEntry)field).Key);
}

> Text Box 1
  Check Box 1
  Numeric Field 1
  Formatted Field 1
  Date Field 1
  List Box 1
  Combo Box 1
  Push Button 1
  Option Button 1

Notice how Label Field 1 is not present. If it were present then doing a text replace might be easy. Except it's not present so it's looking like even iText can't do a simple text replace in a pdf. Is this true? How would you replace text in a pdf document using iTextSharp?

1

1 Answers

1
votes

Notice how Label Field 1 is not present.

As there is no AcroForm form field type "label", form labels usually are drawn as regular page content in PDF files.

If it were present then doing a text replace might be easy. Except it's not present so it's looking like even iText can't do a simple text replace in a pdf. Is this true?

Indeed, in general there is no simple text replacement in a PDF.

How would you replace text in a pdf document using iTextSharp?

I would determine the bounding box coordinates of the text to replace using the iText text extraction feature with some extension that returns text plus coordinates. Then I'd remove that text by redaction using iText's PdfCleanUp... classes. Finally I'd add the replacement text as new text in the bounding box determined at start.

Unfortunately for you, both good text extraction and redaction are not present in your version 4.1.6; for this approach you should update at least to 5.5.x.

Alternatively, though, as you've been trying to generate a report, I assume the template design is in your hands. In that case you can put your labels into read-only text fields which you can change (they are read-only only to GUI users).