【发布时间】:2015-11-16 11:48:44
【问题描述】:
我需要创建仅包含签名名称和日期的签名。 此外,这两个字段必须放置在精确的坐标上,因为这两个字段必须放置在预定义的“修订表”中。
这可能吗?
这是我的代码,与 Bruno Lowagie 的示例非常相似(!),而且可能还没有接近问题的解决方案:
namespace signatures.chapter3 {
public class C3_11_SignWithToken
{
public static String SRC = "../../../../resources/hello.pdf";
public static String DEST = "../../../../results/chapter3/hello_token.pdf";
public void Sign(String src, String dest,
ICollection<X509Certificate> chain, X509Certificate2 pk,
String digestAlgorithm, CryptoStandard subfilter,
String reason, String location,
ICollection<ICrlClient> crlList,
IOcspClient ocspClient,
ITSAClient tsaClient,
int estimatedSize)
{
// Creating the reader and the stamper
PdfReader reader = null;
PdfStamper stamper = null;
FileStream os = null;
try
{
reader = new PdfReader(src);
os = new FileStream(dest, FileMode.Create);
// os = new FileStream(dest, FileMode.Create, FileAccess.Write);
//Activate MultiSignatures
stamper = PdfStamper.CreateSignature(reader, os, '\0', null, true);
//To disable Multi signatures uncomment this line : every new signature will invalidate older ones !
//stamper = PdfStamper.CreateSignature(reader, os, '\0');
// Creating the appearance
PdfSignatureAppearance appearance = stamper.SignatureAppearance;
//appearance.Location = location;
//appearance.SetVisibleSignature(new Rectangle(36, 612, 144, 644), 1, "sig4");
appearance.Reason = "marked as changed";
appearance.Location = location;
//appearance.SetVisibleSignature("Reason");
appearance.Layer2Text = "Signed on " + DateTime.Now;
appearance.SignatureRenderingMode = PdfSignatureAppearance.RenderingMode.DESCRIPTION;
// Creating the signature
IExternalSignature pks = new X509Certificate2Signature(pk, digestAlgorithm);
MakeSignature.SignDetached(appearance, pks, chain, crlList, ocspClient, tsaClient, estimatedSize, subfilter);
}
catch (Exception ex) {
Console.WriteLine("GMA: " + ex.Message);
}
finally
{
if (reader != null)
reader.Close();
if (stamper != null)
stamper.Close();
if (os != null)
os.Close();
}
}
}
非常感谢!
【问题讨论】:
-
你应该有一些代码来解释你的问题
-
是的,这是可能的,免费电子书Digital Signaturs for PDF documents 对此进行了解释。正如前面的评论者所说,关于 StackOverflow 的问题应该包含一些代码,然后解释与该代码相关的问题。现在,您提出的问题可以用“是”或“否”来回答(答案是是的,有可能)。将问题改写为“你能给我代码来执行此操作吗?” 会导致 StackOverflow 上不可接受的问题。这不是 StackOverflow 的工作原理。
-
@blowagie 你是对的 - 我现在已经添加了我的代码。我已经读过你的书——但那件事对我来说仍然不清楚。很高兴收到您的提示。
-
仅包含签名名称和日期...这两个字段必须放在精确的坐标处 - 您可以调用
appearance.GetLayer(2),它会返回PdfTemplate您可以根据自己的意愿进行设计。通过在该模板上定位内容并将签名可视化定位在页面上的appearance.SetVisibleSignature调用中,您可以将这两个字段放在预定义的“修订表”中。
标签: c# itext digital-signature