Fill PDF template acrofield with HTML formatted text using iTextSharp
Asked Answered
I

2

5

I am using iTextSharp to fill in a PDF template. The data I am using is kept in a database and is formatted HTML. My problem is that when I load the AcroField with this text I get it to do the line breaks, but no bold nor italics.

I have already attempted to use an HtmlWorker, but all the examples online show it being used to convert HTML to a PDF, but I am trying to set an AcroField in a PDF template.

Ivories answered 26/3, 2013 at 18:25 Comment(1)
See this post about using Rich Text Fields https://mcmap.net/q/1714778/-set-different-parts-of-a-form-field-to-have-different-fonts-using-itextsharpSeismoscope
I
11

After spending days looking through forums and iTextsharp source code I found a solution. Instead of fill the Acrofield with the HTML formatted text, I used a ColumnText. I parse the html text and load the IElements into a Paragraph. Then add the paragraph to the ColumnText. Then I overlaid the ColumnText on top of where the Acrofield should be, using the coordinates of the field.

    public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos) 
    {
        Paragraph par = new Paragraph();
        ColumnText c1 = new ColumnText(contentBtye);
        try
        {
            List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
            foreach (IElement element in elements) 
            {
               par.Add(element);
            }
            
            c1.AddElement(par);
            c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
            c1.Go(); //very important!!!
        }
        catch (Exception ex)
        {
            
            throw;
        }
    }

Here is an example of a call to this function.

string htmlText ="<b>Hello</b><br /><i>World</i>";
IList<AcroFields.FieldPosition> pos = form.GetFieldPositions("Field1");
//Field1 is the name of the field in the PDF Template you are trying to fill/overlay
AddHTMLToContent(htmlText, stamp.GetOverContent(pos[0].page), pos);
//stamp is the PdfStamper in this example

One thing I did run into while doing this is the fact that my Acrofield did have a predefined font size. Since this functions set the ColumnText on top on the field, any font changes will have to be done in the function. Here is an example of changing the font size:

 public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList<AcroFields.FieldPosition> pos) 
    {
        Paragraph par = new Paragraph();
        ColumnText c1 = new ColumnText(contentBtye);
        try
        {
            List<IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
            foreach (IElement element in elements) 
            {
                foreach (Chunk chunk in element.Chunks) 
                {
                    chunk.Font.Size = 14;
                }
            }
            par.Add(elements[0]);
            c1.AddElement(par);
            c1.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
            c1.Go();//very important!!!
        }
        catch (Exception ex)
        {
            
            throw;
        }
    }
Ivories answered 27/3, 2013 at 18:50 Comment(0)
I
0

So, I have had to tweak this code around a bit the past few months, and I found a better/less round about way to do this.

    public void Final(string text,string fieldName,string filename) 
    {
        iTextSharp.text.pdf.PdfReader reader = null;
        iTextSharp.text.pdf.PdfStamper stamp = null;

        reader = new PdfReader(file path to template);
        stamp = new PdfStamper(reader, new FileStream(path to new file, FileMode.CreateNew));

        AcroFields form = stamp.AcroFields;

        //get the position of the field
        IList<AcroFields.FieldPosition> pos = form.GetFieldPositions(fieldName);
        //tell itextSharp to overlay this content 
        PdfContentByte contentBtye = stamp.GetOverContent(pos[0].page);

        //create a new paragraph
        Paragraph par = new Paragraph();
        //parse html
        List<IElement> elements = HTMLWorker.ParseToList(new StringReader(text), null);
        for (int k = 0; k < elements.Count; k++)
        {
            par.Add((IElement)elements[k]);
        }
        //create a ColumnText to hold the paragraph and set position to the position of                   the field
        ColumnText ct = new ColumnText(contentBtye);
        ct.SetSimpleColumn(pos[0].position.Left, pos[0].position.Bottom, pos[0].position.Right, pos[0].position.Top);
        ct.AddElement(par);
        ct.Go();
        stamp.Close();
        reader.Close();

    }
Igal answered 30/1, 2014 at 17:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.