我们想使用 PDFSharp 将带有(绘制)签名的 png 图像插入到 PDF 中。图像的位置和大小由我们之前在 PDF 中创建的不可见 PdfTextField 确定。使用我们当前的代码,问题是:我们如何从 PdfTextField(包含此字段的页面)中获取页面引用?
代码:
PdfDocument document = PdfReader.Open("C:\\filex.pdf", PdfDocumentOpenMode.Modify);
// Get the root object of all interactive form fields
PdfAcroForm form = document.AcroForm;
// Get all form fields of the whole document
PdfAcroField.PdfAcroFieldCollection fields = form.Fields;
//Get the invisible PdfTextField used as position and size reference
PdfTextField signatureposition= (PdfTextField)fields["thesignature"];
PdfArray signaturerect= (PdfArray)signatureposition.Elements["/Rect"];
string x = signaturerect.Elements[0].ToString();
string y = signaturerect.Elements[1].ToString();
string w = signaturerect.Elements[2].ToString();
string h = signaturerect.Elements[3].ToString();
string imagepath= "C:\\signature.png";
//how to get the correct page reference respect the especified field?
PdfPage page= signatureposition.Owner.Pages[???];
XGraphics gfx = XGraphics.FromPdfPage(page);
XImage image = XImage.FromFile(imagepath);
gfx.DrawImage(image, x, y, width, height);