【问题标题】:Fill PDF template acrofield with HTML formatted text using iTextSharp使用 iTextSharp 用 HTML 格式的文本填充 PDF 模板 acrofield
【发布时间】:2013-03-26 18:25:31
【问题描述】:

我正在使用 iTextSharp 填写 PDF 模板。我使用的数据保存在数据库中,并采用 HTML 格式。我的问题是,当我使用此文本加载 AcroField 时,我得到它来进行换行,但既没有粗体也没有斜体。

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

【问题讨论】:

标签: html forms pdf itext acrofields


【解决方案1】:

花了几天时间浏览论坛和 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;
        }
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多