简体   繁体   English

在 PDF 上添加签名图像,无需使用 iTextSharp 对其进行数字签名

[英]Add signature image on PDF without digitally signing it using iTextSharp

I am using iTextSharp to work with PDFs.我正在使用 iTextSharp 处理 PDF。 I want to add signature image to Signature field without digitally signing the document (without any involvement of certificate).我想将签名图像添加到签名字段而不对文档进行数字签名(不涉及任何证书)。

Is it possible?有可能吗? I am able to work with digital signing but I also want to just add signature image on signature field without any use of certificates.我可以使用数字签名,但我也想只在签名字段上添加签名图像而不使用任何证书。

UPDATE:更新:

Write now I have following code.现在写我有以下代码。

// Set PDF Reader and PDF Stamper
PdfReader reader = new PdfReader(sourceDocument);

// File stream where PDF will write
FileStream fout = new FileStream(destinationPath, FileMode.Create, FileAccess.ReadWrite);
PdfStamper stamper = PdfStamper.CreateSignature(reader, fout, '\0', null, true);

// Set PDF Appearance              
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
iTextSharp.text.Image signatureFieldImage = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Png);
appearance.SignatureGraphic = signatureFieldImage;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.GRAPHIC;
appearance.SetVisibleSignature(signatureFieldName);

stamper.Close();
reader.Close();
fout.Close();

But when I try to run it, it gives following error:但是当我尝试运行它时,它会出现以下错误:

Signature defined.签名定义。 Must be closed in PdfSignatureAppearance必须在 PdfSignatureAppearance 中关闭

You are using code to digitally sign a PDF that you don't want to digitally sign ;-)您正在使用代码对不想进行数字签名的 PDF 进行数字签名;-)

If the document is already signed, adding any extra content (such as an image) will break the signature, but if the document wasn't signed yet, you have different options.如果文档已经签名,添加任何额外内容(例如图像)将破坏签名,但如果文档尚未签名,您有不同的选择。

You need to use PdfStamper the normal way , that is: not by using the CreateSignature() method, but the way it's described in chapter 6 of my book about iText.您需要以正常方式使用PdfStamper ,即:不是使用CreateSignature()方法,而是使用我关于 iText 的书的第 6 章中描述的方式。 You also need to decide whether or not it's important that (1) the image is part of the signature field (in which case it will disappear when the PDF is actually signed) or (2) the image needs to be added as part of the content stream (in which case it will still be there once you sign the document).您还需要确定(1)图像是签名字段的一部分(在这种情况下它会在 PDF 实际签名时消失)或(2)图像是否需要添加为签名字段的一部分是否重要内容流(在这种情况下,一旦您签署文档,它仍然存在)。

In case of (1) , please take a look at code sample 2.6 and code sample 2.7 of my book about digital signatures (see the CreateEmptyField example for the C# version of the code).(1) 的情况下,请查看我关于数字签名的书的代码示例 2.6 和代码示例 2.7(有关代码的 C# 版本,请参阅CreateEmptyField示例)。 In code sample 2.6, you learn how to create a PdfFormField with a custom PdfAppearance .在代码示例2.6,您将学习如何创建一个PdfFormField使用自定义PdfAppearance In code sample 2.7, you learn how to add a signature field to an existing document using PdfStamper .在代码示例 2.7 中,您将学习如何使用PdfStamper将签名字段添加到现有文档。 In your case, you'd remove the existing signature field (using the removeField() method) and replace it with a new PdfFormField with a different appearance at the exact same coordinates.在您的情况下,您将删除现有的签名字段(使用removeField()方法)并将其替换为在完全相同的坐标处具有不同外观的新PdfFormField

In case of (2) , you'll just create an Image object and add it to the PdfContentByte retrieved from the PdfStamper using the GetOverContent() method.(2) 的情况下,您只需创建一个Image对象并将其添加到使用GetOverContent()方法从PdfContentByte检索到的PdfStamper See the examples of chapter 6 for inspiration.请参阅第 6 章示例以获取灵感。

In both cases, you need to know the coordinates and the page number in both cases.在这两种情况下,您都需要知道这两种情况下的坐标和页码。 This information can be retrieved like this:可以像这样检索此信息:

AcroFields form = stamper.AcroFields;
AcroFields.FieldPosition f = form.GetFieldPositions("mySigName")[0];

You'll get the page like this: f.page and a Rectangle defining the position like this: f.position .你会得到这样的页面: f.page和一个Rectangle定义这样的位置: f.position

As we don't know exactly which type of end result you require, it's hard to go into more detail.由于我们不确切知道您需要哪种类型的最终结果,因此很难详细说明。 The most important error in your code is that you use the CreateSignature() method to obtain a PdfStamper object whereas you don't want to sign the document.代码中最重要的错误是您使用CreateSignature()方法获取PdfStamper对象,而您不想签署文档。

This is how I add a unsigned signature field这就是我添加未签名签名字段的方式

     public void buildPDFMemoSignature()
     {   
        using (var ms = new MemoryStream())
        {
        var doc = new Document(PageSize.A4, 20f, 10f, 30f, 0f);
        {
            PdfWriter writer = PdfWriter.GetInstance(doc, ms);
            doc.Open();
            try
            {
              // add stuff to your PDF

             // Signature is added here ***************

             PdfFormField field = PdfFormField.CreateSignature(writer);
  field.SetWidget(new iTextSharp.text.Rectangle(190, 730, 440, 650), PdfAnnotation.HIGHLIGHT_NONE);
                    //Rectangle(float llx, float lly, float urx, float ury) 
                    field.FieldName = "mySig";
                    field.Flags = PdfAnnotation.FLAGS_PRINT;
                    field.SetPage();
                    field.MKBorderColor = BaseColor.BLACK;
                    field.MKBackgroundColor = BaseColor.WHITE;
                    PdfAppearance tp = PdfAppearance.CreateAppearance(writer, 72, 48);
                    tp.Rectangle(0.5f, 0.5f, 71.5f, 47.5f);
                    tp.Stroke();
                    field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, tp);
                    writer.AddAnnotation(field); 
                    }
                    catch (Exception ex)
                    {  
                      //exceptions                   
                    }
                    finally
                    {
                        doc.Close();
                    }  
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM