2014-10-02 60 views
0

我正在使用VB.NET和iTextSharp庫嘗試並將圖像放在現有字段中的PDF中。iTextSharp插入圖像GetFieldPosition

我想獲得領域的位置,以便我可以絕對把圖像放在那裏,但它對我來說是失敗的,我無法弄清楚爲什麼。

我的代碼是:

 Dim sig1file As String = HttpContext.Current.Server.MapPath("../files/userfiles/forms/" & tcode & "/" & tcode & "_pap_signature1.png") 
     Dim sig1img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(sig1file) 

     Dim sig1positon() As Single = PDFFields.GetFieldPositions("payer1_signature1") 
     Dim sig1rect As New Rectangle(sig1positon(1), sig1positon(2), sig1positon(3), sig1positon(4)) 
     sig1img.ScaleToFit(sig1rect.Width, sig1rect.Height) 

     sig1img.SetAbsolutePosition(sig1positon(1) + (sig1rect.Width() - sig1img.ScaledWidth())/2, sig1positon(2) + (sig1rect.Height() - sig1img.ScaledHeight())/2) 
     Dim imagewriter As iTextSharp.text.pdf.PdfContentByte = PDFStamper.GetUnderContent(1) 
     imagewriter.AddImage(sig1img) 

我收到以下錯誤:

「」類型的異常出現在App_Web_y4xg45gn.dll但在用戶代碼中沒有處理

附加信息:無法投射'System.Collections.Generic.List`1 [iTextSharp.text.pdf.AcroFields + FieldPosition]'類型的對象來鍵入'System.Single []'。

任何人都可以幫忙嗎?

謝謝

回答

0

我真的解決了我自己的問題。

的解決方案是這樣的

 Dim sig1file As String = HttpContext.Current.Server.MapPath("../files/userfiles/forms/" & tcode & "/" & tcode & "_pap_signature1.png") 
     Dim sig1img As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(sig1file) 

     Dim sig1position As System.Collections.Generic.IList(Of AcroFields.FieldPosition) = PDFFields.GetFieldPositions("payer1_signature1") 
     Dim test As AcroFields.FieldPosition = sig1position(0) 
     Dim left, right, top, bottom As Single 
     left = test.position.Left 
     right = test.position.Right 
     top = test.position.Top 
     bottom = test.position.Bottom