在PDF上添加签名图像,而无需使用iTextSharp对其进行数字签名 [英] Add signature image on PDF without digitally signing it using iTextSharp

查看:3866
本文介绍了在PDF上添加签名图像,而无需使用iTextSharp对其进行数字签名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iTextSharp处理PDF。我想将签名图像添加到签名字段,而不对文档进行数字签名(无需包含证书)。

I am using iTextSharp to work with PDFs. 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.

更新:

立即写我有以下代码。

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:


签名已定义。必须在PdfSignatureAppearance中关闭


推荐答案

您正在使用代码对数字签名PDF,你不想要数字签名; - )

You are using code to digitally sign a PDF that you don't want to digitally sign ;-)

如果文档已经签名,添加任何额外的内容(如图像)将破坏签名,但是如果文档尚未签名,则您有不同的选项。

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.

您需要使用 PdfStamper 正常方式,即:不是使用 CreateSignature()方法,而是第6章我的书关于iText。您还需要确定(1)图片是签名字段的一部分(在这种情况下,当PDF实际签名时它会消失)或(2) ,图片需要作为内容流的一部分添加(在这种情况下,一旦您签署文档,图片仍然会显示在此处)。

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. 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),请参阅我关于数字签名的书的代码示例2.6和代码示例2.7 >(参见C#版本的 CreateEmptyField 示例的代码)。在代码示例2.6中,您将学习如何使用自定义 PdfAppearance 创建 PdfFieldField 。在代码示例2.7中,您将学习如何使用 PdfStamper 向现有文档添加签名字段。在您的情况下,您将删除现有的签名字段(使用 removeField()方法)并将其替换为新的 PdfFormField

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). In code sample 2.6, you learn how to create a PdfFormField with a custom PdfAppearance. In code sample 2.7, you learn how to add a signature field to an existing document using 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.

(2)的情况下,您只需创建一个 Image 对象并将其添加到从 PdfStamper 中检索的 PdfContentByte GetOverContent()方法。有关灵感,请参阅第6章的示例

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. See the examples of chapter 6 for inspiration.

在这两种情况下,您需要在两种情况下都知道坐标和页码。此信息可以检索如下:

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

您将得到如下页面: f.page Rectangle 定义位置,如下所示: f.position

You'll get the page like this: f.page and a Rectangle defining the position like this: f.position.

由于我们不知道你需要哪种类型的最终结果,所以很难详细说明。代码中最重要的错误是,您使用 CreateSignature()方法获取 PdfStamper 对象,不想签署文件。

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.

这篇关于在PDF上添加签名图像,而无需使用iTextSharp对其进行数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆