【问题标题】:Dynamically adding image in signature field in pdf using itext使用itext在pdf的签名字段中动态添加图像
【发布时间】:2013-11-13 06:04:37
【问题描述】:

我需要一个示例代码或参考,通过使用 itextsharp 动态读取签名字段名称将签名图像放置在签名字段中。图片应该放在签名字段的上方,签名字段的大小对应。有人可以帮我吗..

我正在尝试将签名图像放置在签名字段中,但图像未放置在签名字段中。签名字段矩形的宽度和高度不同,如果更改比例以适应,图像大小会有所不同。这是我的代码:

PdfContentByte pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
iTextSharp.text.pdf.AcroFiel‌​ds fields = stamper.AcroFields;
IList<iTextSharp.text.pdf.AcroFields.FieldPosition> signatureArea = fields.GetFieldPositions("DoctorSign");
TextSharp.text.Rectangle rect = signatureArea.First().position;
image.ScaleAbsolute(rect.Width, rect.Height);
image.SetAbsolutePosition(rect.Left -image.ScaledWidth +(rect.Width -image.ScaledWidth )/2, rect.Bottom+ (rect.Height-image.ScaledHeight)/2 );
pdfContentByte.AddImage(image)

【问题讨论】:

  • 嗨,Noctis..感谢您的即时回复。我正在尝试将签名图像放置在签名字段中,但图像未放置在签名字段中。签名字段矩形的宽度和高度不同,如果更改比例以适应,图像大小在这里得到不同我的代码
  • PdfContentByte pdfContentByte = stamper.GetOverContent(1);iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);iTextSharp.text.pdf.AcroFields fields = stamper.AcroFields;IList signatureArea = fields.GetFieldPositions("DoctorSign");TextSharp.text.Rectangle rect = signatureArea.First().position;image.ScaleAbsolute(rect.Width, rect.Height); image.SetAbsolutePosition(rect.Left -image.ScaledWidth +(rect.Width -image.ScaledWidth )/2, rect.Bottom+ (rect.Height-image.ScaledHeight)/2 );pdfContentByte.AddImage(image);
  • @raj 通常您应该向问题本身添加新信息。您可以添加评论来回答对这些信息的请求,并带有指向这些添加的指针。我在这里为你做的。话虽如此,为什么要更改页面内容而不是签名字段外观?

标签: c# pdf itextsharp


【解决方案1】:

您将能够使用 PDFContentbyte 添加图像。

Foll 是VB .NET 中的代码示例,您可以使用converter.telerik.com 将其转换为c#。

    Imports iTextSharp.text
    Imports iTextSharp.text.pdf

    ' Set the File Details
    Dim fs As New FileStream("FILE_NAME", FileMode.Create, FileAccess.Write)
    Dim reader As New PdfReader(inPDF)
    Dim document As New iTextSharp.text.Document(PageSize.A4)

    ' open writer
    Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
    document.Open()
    Dim cb As PdfContentByte = writer.DirectContent


    ' create the new page and add it to the pdf
    Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
    cb.AddTemplate(page, 0, 0)

    'Add Image
    Dim hdImg As iTextSharp.text.Image
    hdImg = iTextSharp.text.Image.GetInstance(AppDomain.CurrentDomain.BaseDirectory & "Images\sample.png")
    Dim wid As Integer = page.Width
    hdImg.ScalePercent(50)

    hdImg.SetAbsolutePosition(30, 775)
    cb.AddImage(hdImg)

    ' close the streams
    document.Close()
    fs.Close()
    writer.Close()
    reader.Close()

另外,请注意,您需要对“SetAbsolutePosition(30, 775)”中图像坐标的各种组合进行位测试,以将签名设置在正确的位置。

还要注意这里的坐标是点而不是像素。我添加了 wid 变量,以便您知道 pdf 页面的宽度是多少

【讨论】:

  • @阿恩。感谢您的代码和即时响应。首先,我需要找到签名字段大小,然后我需要将图像大小设置为与签名字段大小相同。我可以获得签名字段大小,但我可以将图像重新调整为签名字段大小,但我无法将图像放置在签名字段上。 l 帮助编写代码
  • @raj 您正在使用压模,我个人在使用压模时遇到了很多问题。为什么不试试这个方法我也添加了阅读器,因为您在现有代码中添加签名..
  • @aarn raj 有一个与签名表单字段相关的问题。您给出的代码会删除所有表单字段,因此,显然 在这种情况下没有用处
  • @aarn 我得到了答案。在设置的绝对位置中,我将 rect.left 更改为 rect.right。我知道了。谢谢你们俩的回应。 image.SetAbsolutePosition(rect.Right -image.ScaledWidth +(rect.Width -image.ScaledWidth )/2, rect.Bottom + (rect.Height-image.ScaledHeight)/2);
  • 很高兴您得到了答案。 @mkl 没有提到表格,所以我以为他只想插入图像。
猜你喜欢
  • 2014-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-21
  • 2017-12-19
  • 2013-04-13
  • 1970-01-01
相关资源
最近更新 更多