Fill PDF template acrofield with HTML formatted text using iTextSharp

后端 未结 2 430
北荒
北荒 2021-01-03 04:26

I am using iTextSharp to fill in a PDF template. The data I am using is kept in a database and is HTML formatted. My problem is that w

2条回答
  •  再見小時候
    2021-01-03 05:12

    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 pos) 
        {
            Paragraph par = new Paragraph();
            ColumnText c1 = new ColumnText(contentBtye);
            try
            {
                List 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 ="Hello
    World"; IList 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 pos) 
        {
            Paragraph par = new Paragraph();
            ColumnText c1 = new ColumnText(contentBtye);
            try
            {
                List 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.

提交回复
热议问题