C# 使用itext在pdf签名字段中动态添加图像

C# 使用itext在pdf签名字段中动态添加图像,c#,pdf,itextsharp,C#,Pdf,Itextsharp,我需要一个示例代码或参考,通过使用itextsharp动态读取签名字段名,将签名图像放置在签名字段中。图像应放置在签名字段上方,签名字段的大小应与之对应。。有人能帮我吗 我试图将签名图像放置在签名字段中,但图像未放置在签名字段中。签名字段矩形的宽度和高度不同,如果更改比例以适应,则图像大小会不同。这是我的代码: PdfContentByte pdfContentByte = stamper.GetOverContent(1); iTextSharp.text.Image image = iTex

我需要一个示例代码或参考,通过使用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)

PdfContentByte PdfContentByte=stamper.GetOverContent(1);
iTextSharp.text.Image Image=iTextSharp.text.Image.GetInstance(inputImageStream);
iTextSharp.text.pdf.AcroFiel‌​ds字段=stamper.AcroFields;
IList signatureArea=fields.GetFieldPositions(“DoctorSign”);
TextSharp.text.Rectangle=signatureArea.First().position;
可缩放溶质(矩形宽度、矩形高度);
image.SetAbsolutePosition(rect.Left-image.ScaledWidth+(rect.Width-image.ScaledWidth)/2,rect.Bottom+(rect.Height-image.ScaledHeight)/2);
pdfContentByte.AddImage(图像)

您可以使用PDFContentbyte添加图像

Foll是VB.NET中的一个代码示例,您可以使用它将其转换为c#

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


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

Hi-Noctis..感谢您的即时回复。我试图将签名图像放置在签名字段中,但图像未放置在签名字段中。签名字段矩形的宽度和高度不同,如果将比例更改为适合,则图像大小get在此不同my codePdfContentByte pdfContentByte=stamper.GetOverContent(1);iTextSharp.text.Image Image=iTextSharp.text.Image.GetInstance(inputImageStream);iTextSharp.text.pdf.AcroFields=stamper.AcroFields;IList signatureArea=fields.GetFieldPositions(“DoctorSign”);TextSharp.text.Rectangle=signatureArea.First().position;可缩放溶质(矩形宽度、矩形高度);image.SetAbsolutePosition(rect.Left-image.ScaledWidth+(rect.Width-image.ScaledWidth)/2,rect.Bottom+(rect.Height-image.ScaledHeight)/2);pdfContentByte.AddImage(图像)@一般来说,你应该在问题本身中添加新的信息。您可以添加一条注释,用一个指向这些添加内容的指针来回答这些信息的请求。我在这里是为你做的。也就是说,为什么要更改页面内容而不是签名字段的外观?@Aarn。感谢您的代码和即时响应。首先,我需要找到签名字段的大小,然后我需要将图像大小设置为与签名字段大小相同。我可以得到签名字段大小,但我可以将图像大小重新调整为签名字段大小,但我无法将图像放置在签名字段上。我会帮忙的code@raj您正在使用stamper&我个人在使用stamper时遇到了很多问题。为什么不试试这个方法呢?我已经添加了reader,因为您正在现有代码中添加签名。@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没有提到表单,所以我认为他只想插入图像。
    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()