After spending days browsing the forums and source code for iTextsharp, I found a solution. Instead of filling Acrofield with HTML text, I used ColumnText. I parse the html text and load IElements into Paragraph. Then add a paragraph to the column text. Then I overlaid a ColumnText on top of where 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();
}
catch (Exception ex)
{
throw;
}
}
Here is an example of calling this function.
string htmlText ="<b>Hello</b><br /><i>World</i>";
IList<AcroFields.FieldPosition> pos = form.GetFieldPositions("Field1");
AddHTMLToContent(htmlText, stamp.GetOverContent(pos[0].page), pos);
One thing that I came across is that my Acrofield had a predefined font size. Because these functions set the ColumnText to the top of the field, any font changes must be made to the function. The following 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();
}
catch (Exception ex)
{
throw;
}
}
I hope this saves someone some time and energy in the future.
source
share