2012-10-29 44 views
1

我们想要将带有(绘制)签名的png图像插入PDFSharp的PDF中。图像的位置和大小由我们先前在PDF中创建的不可见的PdfTextField确定。使用我们当前的代码,问题是:我们如何从PdfTextField(包含此字段的页面)获取页面引用?PDFSharp使用PdfTextField作为位置和大小参考插入PDF中的图像

代码:

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); 

回答

2

最后我们解决了它创建这个函数:

protected int PaginaCampo(string campofirma, PdfDocument document) 
{ 
    for (int i = 0; i < document.Pages.Count; i++) 
    { 
     PdfAnnotations anotations = document.Pages[i].Annotations; 
     for (int j = 0; j < anotations.Count; j++) 
     { 
      if (anotations[j].Title != campofirma) continue; 
      return i; 
     } 
    } 
    return -1; 
} 

不是最好的解决方案,但它的工作原理......如果有人添加了一个更好的,我们将给予正确回答他/她

+1

campofirma代表什么?它是firma(签名?)的字符串吗? –

相关问题