フォーラムと iTextsharp のソース コードを何日も調べた後、解決策を見つけました。Acrofield を HTML 形式のテキストで埋める代わりに、ColumnText を使用しました。HTML テキストを解析し、IElements を段落に読み込みます。次に、段落を ColumnText に追加します。次に、フィールドの座標を使用して、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)
{
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
これを行っているときに遭遇したことの 1 つは、私の 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;
}
}