使用iTextSharp使用HTML格式的文本填充PDF模板acrofield

时间:2013-03-26 18:25:31

标签: forms pdf itextsharp html-formatting acrofields

我正在使用iTextSharp填写PDF模板。我使用的数据保存在数据库中,格式为HTML。我的问题是,当我使用此文本加载AcroField时,我会让它执行换行符,但没有粗体,也没有 italicizing 。我已尝试使用HtmlWorker,但在线的所有示例都显示它用于将HTML转换为PDF,但我尝试在PDF中设置AcroField模板。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:10)

在花了几天时间浏览论坛和iTextsharp源代码后,我找到了解决方案。我没有使用HTML格式的文本填充Acrofield,而是使用了ColumnText。我解析html文本并将IElements加载到段落中。然后将段落添加到ColumnText。然后我使用字段的坐标将ColumnText覆盖在Acrofield所在的顶部。

    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;
        }
    }

以下是调用此函数的示例。

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

我这样做的一件事就是我的Acrofield确实有预定义的字体大小。由于此函数将ColumnText设置在字段的顶部,因此必须在函数中完成任何字体更改。以下是更改字体大小的示例:

 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;
        }
    }

我希望将来节省一些时间和精力。

答案 1 :(得分:0)

所以,在过去的几个月里,我不得不稍微调整一下这段代码,并且我找到了一个更好/更少的方法来做到这一点。

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

    }