Exemple #1
0
        public static void Sign(string input, string output, ImageData stamper, ICipherParameters privateKey, X509Certificate[] chain, string flag)
        {
            PdfDocument document = new PdfDocument(new PdfReader(input));

            PdfAcroForm acroForm = PdfAcroForm.GetAcroForm(document, false);
            bool        append   = (acroForm != null && acroForm.GetSignatureFlags() != 0);

            int pageNumber = document.GetNumberOfPages();

            RegexBasedLocationExtractionStrategy strategy = new RegexBasedLocationExtractionStrategy(flag);
            PdfDocumentContentParser             parser   = new PdfDocumentContentParser(document);

            parser.ProcessContent(pageNumber, strategy);
            var locations = new List <IPdfTextLocation>(strategy.GetResultantLocations());

            document.Close();

            StampingProperties properties = new StampingProperties();

            properties = append ? properties.UseAppendMode() : properties;

            PdfSigner signer = new PdfSigner(new PdfReader(input), new FileStream(output, FileMode.Create), properties);

            signer.SetCertificationLevel(PdfSigner.CERTIFIED_NO_CHANGES_ALLOWED);

            PdfSignatureAppearance appearance = signer.GetSignatureAppearance();

            appearance.SetPageNumber(pageNumber);

            int size = locations.Count;

            if (size != 0)
            {
                IPdfTextLocation location = locations[size - 1];

                float flagX = location.GetRectangle().GetX();
                float flagY = location.GetRectangle().GetY();

                float width  = stamper.GetWidth();
                float height = stamper.GetHeight();

                float x = flagX - width / 2;
                float y = flagY - height / 2;

                appearance.SetRenderingMode(PdfSignatureAppearance.RenderingMode.GRAPHIC);
                appearance.SetSignatureGraphic(stamper);
                appearance.SetPageRect(new Rectangle(x, y, width, height));
            }

            PrivateKeySignature signature = new PrivateKeySignature(privateKey, DigestAlgorithms.SHA256);

            signer.SignDetached(signature, chain, null, null, null, 0, PdfSigner.CryptoStandard.CADES);
        }
Exemple #2
0
        /// <summary>
        /// Get all
        /// <see cref="iText.PdfCleanup.PdfCleanUpLocation"/>
        /// objects from a given
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// </summary>
        /// <param name="doc">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfDocument"/>
        /// to be processed
        /// </param>
        /// <returns>
        /// a List of
        /// <see cref="iText.PdfCleanup.PdfCleanUpLocation"/>
        /// objects
        /// </returns>
        public virtual IList <PdfCleanUpLocation> GetPdfCleanUpLocations(PdfDocument doc)
        {
            PdfDocumentContentParser   parser  = new PdfDocumentContentParser(doc);
            IList <PdfCleanUpLocation> toClean = new List <PdfCleanUpLocation>();

            for (int pageNr = 1; pageNr <= doc.GetNumberOfPages(); pageNr++)
            {
                parser.ProcessContent(pageNr, strategy);
                foreach (IPdfTextLocation rect in strategy.GetResultantLocations())
                {
                    if (rect != null)
                    {
                        toClean.Add(new PdfCleanUpLocation(pageNr, rect.GetRectangle(), strategy.GetRedactionColor(rect)));
                    }
                }
                ResetStrategy();
            }
            JavaCollectionsUtil.Sort(toClean, new _IComparer_137());
            return(toClean);
        }
Exemple #3
0
        /// <summary>
        /// Get all
        /// <see cref="iText.PdfCleanup.PdfCleanUpLocation"/>
        /// objects from a given
        /// <see cref="iText.Kernel.Pdf.PdfPage"/>
        /// </summary>
        /// <param name="page">
        /// the
        /// <see cref="iText.Kernel.Pdf.PdfPage"/>
        /// to be processed
        /// </param>
        /// <returns>
        /// a List of
        /// <see cref="iText.PdfCleanup.PdfCleanUpLocation"/>
        /// objects
        /// </returns>
        public virtual IList <PdfCleanUpLocation> GetPdfCleanUpLocations(PdfPage page)
        {
            // get document
            PdfDocument doc = page.GetDocument();
            // create parser
            PdfDocumentContentParser parser = new PdfDocumentContentParser(doc);
            // get page number
            int pageNr = doc.GetPageNumber(page);
            // process document
            IList <PdfCleanUpLocation> toClean = new List <PdfCleanUpLocation>();

            parser.ProcessContent(pageNr, strategy);
            foreach (IPdfTextLocation rect in strategy.GetResultantLocations())
            {
                if (rect != null)
                {
                    toClean.Add(new PdfCleanUpLocation(pageNr, rect.GetRectangle(), strategy.GetRedactionColor(rect)));
                }
            }
            // reset strategy for next iteration
            ResetStrategy();
            // return
            return(toClean);
        }
Exemple #4
0
        //
        // Helper methods
        //

        private void Merge(PdfDocument from, int pageNum)
        {
            PdfPage   page            = from.GetPage(pageNum);
            Rectangle currentPageRect = page.GetPageSize();

            if (!(pageSize.GetHeight() == currentPageRect.GetHeight()) ||
                !(pageSize.GetWidth() == currentPageRect.GetWidth()))
            {
                throw new PdfException("Page size of the copied page should be the same as "
                                       + "the page size of the resultant document.");
            }

            PdfFormXObject formXObject = page.CopyAsFormXObject(pdfDocument);

            PdfDocumentContentParser contentParser = new PdfDocumentContentParser(from);
            PageVerticalAnalyzer     finder        = contentParser.ProcessContent(pageNum, new PageVerticalAnalyzer());

            List <float> verticalFlips = finder.GetVerticalFlips();

            if (verticalFlips.Count < 2)
            {
                return;
            }

            Rectangle pageSizeToImport = page.GetPageSize();

            int startFlip = verticalFlips.Count - 1;

            bool first = true;

            while (startFlip > 0)
            {
                if (!first)
                {
                    NewPage();
                }

                float freeSpace = yPosition - (pageSize.GetBottom() + bottom);
                int   endFlip   = startFlip + 1;
                while ((endFlip > 1) && (verticalFlips[startFlip] - verticalFlips[endFlip - 2] < freeSpace))
                {
                    endFlip -= 2;
                }

                if (endFlip < startFlip)
                {
                    float height = verticalFlips[startFlip] - verticalFlips[endFlip];

                    canvas.SaveState();
                    canvas.Rectangle(0, yPosition - height, pageSizeToImport.GetWidth(), height);
                    canvas.Clip();
                    canvas.EndPath();

                    canvas.AddXObjectAt(formXObject, 0,
                                        yPosition - (verticalFlips[startFlip] - pageSizeToImport.GetBottom()));

                    canvas.RestoreState();
                    yPosition -= height + gap;
                    startFlip  = endFlip - 1;
                }
                else if (!first)
                {
                    throw new ArgumentException(String.Format("Page {0} content sections too large.", page));
                }

                first = false;
            }
        }