我已经能够使用 x,y 坐标对新文本进行绝对定位,但新文本隐藏在现有图像后面。我玩过 Stamper,OverContent,但没有成功。
这是我正在使用的代码:
PdfReader reader = new PdfReader(new RandomAccessFileOrArray(fileNameExisting), null);
Rectangle size = reader.GetPageSizeWithRotation(1);
using (var outStream = new FileStream(fileNameNew, FileMode.Create))
{
Document document = new Document(size);
PdfWriter writer = PdfWriter.GetInstance(document, outStream);
document.Open();
try
{
PdfContentByte cb = writer.DirectContent;
cb.BeginText();
try
{
cb.SetFontAndSize(BaseFont.CreateFont(), 12);
cb.SetTextMatrix(10, 100);
cb.ShowText("My new text");
}
finally
{
cb.EndText();
}
PdfImportedPage page = writer.GetImportedPage(reader, 1);
cb.AddTemplate(page, 0, 0);
}
finally
{
document.Close();
writer.Close();
reader.Close();
}
}