iTextSharp.text.pdf.PdfSignatureAppearance.Close C# (CSharp) Метод

Close() публичный Метод

public Close ( PdfDictionary update ) : void
update PdfDictionary
Результат void
        public void Close(PdfDictionary update)
        {
            try {
                if (!preClosed)
                    throw new DocumentException(MessageLocalization.GetComposedMessage("preclose.must.be.called.first"));
                ByteBuffer bf = new ByteBuffer();
                foreach (PdfName key in update.Keys) {
                    PdfObject obj = update.Get(key);
                    PdfLiteral lit = exclusionLocations[key];
                    if (lit == null)
                        throw new ArgumentException(MessageLocalization.GetComposedMessage("the.key.1.didn.t.reserve.space.in.preclose", key.ToString()));
                    bf.Reset();
                    obj.ToPdf(null, bf);
                    if (bf.Size > lit.PosLength)
                        throw new ArgumentException(MessageLocalization.GetComposedMessage("the.key.1.is.too.big.is.2.reserved.3", key.ToString(), bf.Size, lit.PosLength));
                    if (tempFile == null)
                        Array.Copy(bf.Buffer, 0, bout, lit.Position, bf.Size);
                    else {
                        raf.Seek(lit.Position, SeekOrigin.Begin);
                        raf.Write(bf.Buffer, 0, bf.Size);
                    }
                }
                if (update.Size != exclusionLocations.Count)
                    throw new ArgumentException(MessageLocalization.GetComposedMessage("the.update.dictionary.has.less.keys.than.required"));
                if (tempFile == null) {
                    originalout.Write(bout, 0, boutLen);
                }
                else {
                    if (originalout != null) {
                        raf.Seek(0, SeekOrigin.Begin);
                        long length = raf.Length;
                        byte[] buf = new byte[8192];
                        while (length > 0) {
                            int r = raf.Read(buf, 0, (int)Math.Min((long)buf.Length, length));
                            if (r < 0)
                                throw new EndOfStreamException(MessageLocalization.GetComposedMessage("unexpected.eof"));
                            originalout.Write(buf, 0, r);
                            length -= r;
                        }
                    }
                }
            }
            finally {
                if (tempFile != null) {
                    try{raf.Close();}catch{}
                    if (originalout != null)
                        try{File.Delete(tempFile);}catch{}
                }
                if (originalout != null)
                    try{originalout.Close();}catch{}
            }
        }

Usage Example

Пример #1
1
        /**
         * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
         * @param sap the signature appearance
         * @param tsa the timestamp generator
         * @param signatureName the signature name or null to have a name generated
         * automatically
         * @throws Exception
         */
        public static void Timestamp(PdfSignatureAppearance sap, ITSAClient tsa, String signatureName) {
            int contentEstimated = tsa.GetTokenSizeEstimate();
            sap.SetVisibleSignature(new Rectangle(0,0,0,0), 1, signatureName);

            PdfSignature dic = new PdfSignature(PdfName.ADOBE_PPKLITE, PdfName.ETSI_RFC3161);
            dic.Put(PdfName.TYPE, PdfName.DOCTIMESTAMP);
            sap.CryptoDictionary = dic;

            Dictionary<PdfName,int> exc = new Dictionary<PdfName,int>();
            exc[PdfName.CONTENTS] = contentEstimated * 2 + 2;
            sap.PreClose(exc);
            Stream data = sap.RangeStream;
            IDigest messageDigest = DigestUtilities.GetDigest(tsa.GetDigestAlgorithm());
            byte[] buf = new byte[4096];
            int n;
            while ((n = data.Read(buf, 0, buf.Length)) > 0) {
                messageDigest.BlockUpdate(buf, 0, n);
            }
            byte[] tsImprint = new byte[messageDigest.GetDigestSize()];
            messageDigest.DoFinal(tsImprint, 0);
            byte[] tsToken = tsa.GetTimeStampToken(tsImprint);

            if (contentEstimated + 2 < tsToken.Length)
                throw new Exception("Not enough space");

            byte[] paddedSig = new byte[contentEstimated];
            System.Array.Copy(tsToken, 0, paddedSig, 0, tsToken.Length);

            PdfDictionary dic2 = new PdfDictionary();
            dic2.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true));
            sap.Close(dic2);
        }
All Usage Examples Of iTextSharp.text.pdf.PdfSignatureAppearance::Close