5

I have a PDF template with form fields where I used iText 7.1.0 to replace some placeholder text inside different form fields which is working fine:

PdfReader reader = new PdfReader(pdf_template);    
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdf_output));    
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);    
form.GetField("Vertragsnummer").SetValue("12345"); 
form.FlattenFields();    
pdf.Close();    
reader.Close();

Now I have a form field for which option is set to allow rich text format as there are some parts with bold font etc.. Inside the form field standard text there are again placeholders which I want to replace.

Currently I've got the following code:

PdfFormField tf_maintext = form.GetField("maintext");   
tf_maintext.SetRichText(tf_maintext.GetRichText());

Now I need to replace the placeholders but I didn't find any working solution for C#.

For normal form fields I can use

PdfFormField kdnr = form.GetField("Kundennummer");
kdnr.SetValue(kdnr.GetValueAsString().Replace("7777777777", "1111111111"));

the normal Replace function to achieve this.

But I need a solution to replace text in the rich text format field to ensure that the formatting is still there afterwards.

How can I do this?

nkr
  • 3,026
  • 7
  • 31
  • 39

0 Answers0