正如我發現有一種方法來實現這一目標,雖然沒有通過任何特別優雅的手段(我知道)。
您這樣做的方式是使用iTextSharp至直接在現有表單字段上寫一個文本字段。
這裏的主要警告是,PDF不再成爲一種形式,基本上,所以如果你需要閱讀表單字段的內容,你會被打爛。另一方面,如果表單字段基本上被用作簡單指南來幫助告訴iTextSharp將文本放在pdf上的哪個位置(並且僅用於輸出),那麼這可能是正常的。
using (FileStream filestream = new FileStream(outputpath, FileMode.CreateNew, FileAccess.Write))
{
var stamper = new PdfStamper(reader, filestream);
var acroFields = stamper.AcroFields;
string fieldName = "Field Name";
var fieldPositions = acroFields.GetFieldPositions(fieldName);
var helveticaBold = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14);
var helveticaRegular = FontFactory.GetFont(FontFactory.HELVETICA, 14);
List<Chunk> chunks = new List<Chunk>();
chunks.Add(new Chunk("Rodrigo, you are ", helveticaRegular));
chunks.Add(new Chunk("NOT", helveticaBold));
chunks.Add(new Chunk(" the father!", helveticaRegular));
Phrase currentPhrase = new Phrase();
foreach (Chunk chunk in chunks)
{
currentPhrase.Add(chunk);
}
foreach (var currentPosition in fieldPositions)
{
PdfContentByte currentCanvas = stamper.GetOverContent(currentPosition.page);
ColumnText currentColumnText = new ColumnText(currentCanvas);
currentColumnText.SetSimpleColumn(currentPhrase, currentPosition.position.Left,
currentPosition.position.Bottom, currentPosition.position.Right,
currentPosition.position.Top, 13, Element.ALIGN_LEFT);
currentColumnText.Go();
}
stamper.FormFlattening = true;
stamper.Close();
}
檢查此其他方法:http://stackoverflow.com/q/15644655/114029 –