Sunday, 15 September 2013

forms - Fill PDF template acrofield with HTML formatted text using iTextSharp -


After

I am using iTextSharp to fill in a pdf template for. The data I am using is kept in a database and formatted as HTML . My problem is that when I get the load AcroField this text I get it to break the line but with no bold and italicizing . I've already tried using HtmlWorker , but used to convert all the examples online HTML to a PDF But I'm trying to set up a AcroField in the PDF template will be appreciated. After looking through the expense forums and iTextSharp source code I got a solution after reading the text "itemprop =" text "

Rather than filling the acrophil with HTML formatted text, I used a column text I parse html text and load IElements in the paragraph. Then add paragraph to the column text. Then I use the ColumnText overlapping area coordinates on top of where Acrofield should be. Public Zero AddHTMLToContent (String htmlText, PdfContentByte contentBtye, IList & lt; AcroFields.FieldPosition & gt; status). {Paragraph para = new paragraph (); Column Text C1 = New Column Text (contentBtye); {List & lt; IElement & gt; Element = HTMLWorker.ParseToList (new stringreader (html text), empty); Front (element element in elements) {par.Add (element); } C1 Adelim; C1.SetSimpleColumn (Position [0] .position.Left, Position [0] .position.Bottom, Position [0] .position.Right, Position [0] .position.Top); C1.Go (); // Vital key !!! } Hold (pre-ex) {throw; }}

Here is an example of a call for this function

  string htmlText = ". & Lt; p & gt; Hello & lt; / P & gt; & lt; br / & gt; & lt; i & gt; World & lt; / i & gt; "; IList & LT; AcroFields.FieldPosition & gt; Pos = form.GetFieldPositions ("Field 1"); // field 1 is the name of the field in the PDF template that you are trying to fill / overlay AddHTMLToContent (htmlText, stamp.GetOverContent (pos [0] .page), pos); // Stamp In this example is pdfstammer   

While doing this, I had run a thing the fact that my Acrofield had a predefined font size as this function returned to the column at the top of the column Text is set, any font change in the function will be required. Here is an example of changing font size:

  Public Zero AddHTMLToContent (string htmlText, PdfContentByte contentBtye, IList & lt; acroFields.FieldPosition & gt; pos) {paragraph para = new paragraph (); Column Text C1 = New Column Text (contentBtye); {List & lt; IElement & gt; Element = HTMLWorker.ParseToList (new stringreader (html text), empty); Prior (scales element in elements) {advance (chunk of chunk in element. Chunk) {chunk.Font.Size = 14; }} Par.Add (element [0]); C1.AddElement (equal); C1.SetSimpleColumn (pos [0]. Position.Left, pos [0] .position.bottom, pos [0] .position.right, pos [0] .position.Top); C1.Go (); // Very important !!! } Hold (pre-ex) {throw; }}   

I hope this saves some time and energy in the future.

No comments:

Post a Comment