Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
637 views
in Technique[技术] by (71.8m points)

c# - How do I make an iTextSharp signature appear on every page of a document?

when I sign a document the signature only appears on the first page. Is there anything I can do to make it appear on every page? This is the method I'm using right now to sign the PDF, hope it helps find a solution:

public static PdfStamper SignHashedUser(string Target, SysX509.X509Certificate2 Certificate, string Reason, string Location, bool AddVisibleSign, PdfReader objReader, int pags)
    {
        X509CertificateParser objCP = new X509CertificateParser();
        X509Certificate[] objChain = new X509Certificate[] { objCP.ReadCertificate(Certificate.RawData) };

        PdfStamper objStamper = PdfStamper.CreateSignature(objReader, new FileStream(Target, FileMode.Create), '');
        PdfSignatureAppearance objSA = objStamper.SignatureAppearance;
        int[] perms = { PdfWriter.AllowPrinting, PdfWriter.AllowFillIn };


        if (AddVisibleSign)
            objSA.SetVisibleSignature(new Rectangle(50, 50, 250, 100), pags, null);
//pags define in which page of the PDF will the signature appear 
        objSA.SignDate = DateTime.Now;
        objSA.SetCrypto(null, objChain, null, null);
        objSA.Acro6Layers = true;
        objSA.Render = PdfSignatureAppearance.SignatureRender.NameAndDescription;
        PdfSignature objSignature = new PdfSignature(PdfName.ADOBE_PPKMS, PdfName.ADBE_PKCS7_SHA1);
        objSignature.Date = new PdfDate(objSA.SignDate);
        objSignature.Name = PdfPKCS7.GetSubjectFields(objChain[0]).GetField("CN");
        if (objSA.Reason != null)
            objSignature.Reason = objSA.Reason;
        if (objSA.Location != null)
            objSignature.Location = objSA.Location;
        objSA.CryptoDictionary = objSignature;
        int intCSize = 4000;
        Hashtable objTable = new Hashtable();
        objTable[PdfName.CONTENTS] = intCSize * 2 + 2;
        objSA.PreClose(objTable);

        HashAlgorithm objSHA1 = new SHA1CryptoServiceProvider();

        Stream objStream = objSA.RangeStream;
        int intRead = 0;
        byte[] bytBuffer = new byte[8192];
        while ((intRead = objStream.Read(bytBuffer, 0, 8192)) > 0)
            objSHA1.TransformBlock(bytBuffer, 0, intRead, bytBuffer, 0);
        objSHA1.TransformFinalBlock(bytBuffer, 0, 0);

        byte[] bytPK = SignMsg(objSHA1.Hash, Certificate, false);
        byte[] bytOut = new byte[intCSize];

        PdfDictionary objDict = new PdfDictionary();

        Array.Copy(bytPK, 0, bytOut, 0, bytPK.Length);

        objDict.Put(PdfName.CONTENTS, new PdfString(bytOut).SetHexWriting(true));
        objSA.Close(objDict);

        return objStamper;
    }

EDIT: PdfSignatureAppearance.SetVisibleSignature() is a method including a parameter indicating the page where the Signature should be displayed. However it can't be used to determine the number of pages where one's signature would be shown..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Allow me to copy a sentence and a note from ISO-32000-2. This document isn't available yet because it will only be released in 2016. However, it is very clear on the subject of this question:

enter image description here

This has always been true, however: it was only documented in documentation proprietary to Adobe. Starting with PDF 2.0, it will be normative.

In short: you are asking for something that is in violation with the PDF specification.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...