使用iTextSharp将HTML格式文本填入PDF模板acrofield [英] Fill PDF template acrofield with HTML formatted text using iTextSharp

查看:286
本文介绍了使用iTextSharp将HTML格式文本填入PDF模板acrofield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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


$ b

  public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList< AcroFields.FieldPosition> pos)
{
Paragraph par = new Paragraph();
ColumnText c1 = new ColumnText(contentBtye);
尝试
{
列表< IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
foreach(元素中的IElement元素)
{
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(); //很重要!!!
}
catch(Exception ex)
{

throw;


$ / code $ / pre

这是一个调用这个函数的例子。

  string htmlText =< b> Hello< / b>< br />< i> World< I>中; 
IList< AcroFields.FieldPosition> pos = form.GetFieldPositions(Field1);
// Field1是您试图填充/覆盖的PDF模板中字段的名称
AddHTMLToContent(htmlText,stamp.GetOverContent(pos [0] .page),pos);
//邮票是这个例子中的PdfStamper

这是事实,我的Acofield确实有一个预定义的字体大小。由于该函数将ColumnText设置在字段顶部,所以任何字体更改都必须在函数中完成。这里是一个改变字体大小的例子:

  public void AddHTMLToContent(String htmlText,PdfContentByte contentBtye,IList< AcroFields.FieldPosition> pos)
{
Paragraph par = new Paragraph();
ColumnText c1 = new ColumnText(contentBtye);
尝试
{
列表< IElement> elements = HTMLWorker.ParseToList(new StringReader(htmlText),null);
foreach(元素中的IElement元素)
{
foreach(element.Chunk中的块大块)
{
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(); //非常重要!
}
catch(Exception ex)
{

throw;
}
}

我希望这样可以节省一些时间和精力未来。


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 when I load the AcroField with this text I get it to do the line breaks, but no bold nor italicizing. I have already attempted to use HtmlWorker, but all the examples online show it being used to convert HTML to a PDF but I am trying to set an AcroField in a PDF template. Any help would be appreciated.

解决方案

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<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 a call to 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 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<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.

这篇关于使用iTextSharp将HTML格式文本填入PDF模板acrofield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆