2013-03-26 65 views
5

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

+0

看到这个职位有关使用富文本字段http://stackoverflow.com/a/4412527/231316 – 2013-03-26 19:49:00

回答

9

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

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 

我这样做时遇到的一件事是,我的Acofield确实有预定义的字体大小。由于该函数将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; 
     } 
    } 

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

+0

你一定帮...这有点令人费解的方式工作。感谢您发布您的发现! “fields.SetFieldRichValue”不起作用是一种遗憾。我现在正在使用iTextSharp 5.4.2。 – 2013-07-06 00:07:48

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

    } 
相关问题