5

我正在使用 iTextSharp 填写 PDF 模板。我使用的数据保存在数据库中,并采用 HTML 格式。我的问题是,当我AcroField用这个文本加载时,我让它做换行符,但没有粗体或斜体。

我已经尝试使用 HtmlWorker,但所有在线示例都显示它用于将 HTML 转换为 PDF,但我试图AcroField在 PDF 模板中设置一个。

4

2 回答 2

11

花了几天时间浏览论坛和 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;
        }
    }
于 2013-03-27T18:50:22.597 回答
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();

    }
于 2014-01-30T17:23:16.533 回答