Fill acrofield PDF with HTML text using iTextSharp

I use iTextSharpto fill out the template PDF. The data used is stored in a database and HTMLformatted. My problem is that when I load AcroFieldwith this text, I get it to do line breaks, but not bold or italic. I already tried to use HtmlWorker, but all the examples online show that it is used to convert HTMLto PDF, but I'm trying to install AcroFieldin a PDF template. Any help would be appreciated.

+5
source share
2 answers

After spending days browsing the forums and source code for iTextsharp, I found a solution. Instead of filling Acrofield with HTML text, I used ColumnText. I parse the html text and load IElements into Paragraph. Then add a paragraph to the column text. Then I overlaid a ColumnText on top of where 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 calling 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 that I came across is that my Acrofield had a predefined font size. Because these functions set the ColumnText to the top of the field, any font changes must be made to the function. The following 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;
        }
    }

I hope this saves someone some time and energy in the future.

+10
source

, , / .

    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();

    }
0

All Articles