Add signature image on PDF without digitally signing it using iTextSharp(在 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.
更新:
现在写我有以下代码.
// 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, ' ', 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()
方法,而是描述的方式在我关于 iText 的书的 第 6 章 中.您还需要确定 (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关于数字签名(参见 CreateEmptyField C# 版本代码的示例).在代码示例 2.6 中,您将了解如何使用自定义 PdfAppearance
创建一个 PdfFormField
.在代码示例 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
对象并将其添加到从 检索到的
使用 PdfContentByte
>PdfStamperGetOverContent()
方法.请参阅 第 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 对其进行数字签名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 PDF 上添加签名图像而不使用 iTextSharp 对其进行数字签名
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01