public virtual void Test01() { System.Console.Out.WriteLine(new FileInfo(sourceFolder).FullName); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "in01.pdf")); // build strategy RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy(iText.IO.Util.StringUtil.RegexCompile ("\\{\\{Signature\\}\\}")); // get locations IList <IPdfTextLocation> locationList = new List <IPdfTextLocation>(); for (int x = 1; x <= pdfDocument.GetNumberOfPages(); x++) { new PdfCanvasProcessor(extractionStrategy).ProcessPageContent(pdfDocument.GetPage(x)); foreach (IPdfTextLocation location in extractionStrategy.GetResultantLocations()) { if (location != null) { locationList.Add(location); } } } // compare NUnit.Framework.Assert.AreEqual(1, locationList.Count); IPdfTextLocation loc = locationList[0]; NUnit.Framework.Assert.AreEqual("{{Signature}}", loc.GetText()); NUnit.Framework.Assert.AreEqual(23, (int)loc.GetRectangle().GetX()); NUnit.Framework.Assert.AreEqual(375, (int)loc.GetRectangle().GetY()); NUnit.Framework.Assert.AreEqual(55, (int)loc.GetRectangle().GetWidth()); NUnit.Framework.Assert.AreEqual(11, (int)loc.GetRectangle().GetHeight()); // close pdfDocument.Close(); }
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); }
public virtual void TestLigatureBeforeLigature() { System.Console.Out.WriteLine(new FileInfo(sourceFolder).FullName); PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + "ligature.pdf")); // build strategy RegexBasedLocationExtractionStrategy extractionStrategy = new RegexBasedLocationExtractionStrategy("ca"); // get locations IList <IPdfTextLocation> locationList = new List <IPdfTextLocation>(); for (int x = 1; x <= pdfDocument.GetNumberOfPages(); x++) { new PdfCanvasProcessor(extractionStrategy).ProcessPageContent(pdfDocument.GetPage(x)); foreach (IPdfTextLocation location in extractionStrategy.GetResultantLocations()) { if (location != null) { locationList.Add(location); } } } // compare NUnit.Framework.Assert.AreEqual(1, locationList.Count); IPdfTextLocation loc = locationList[0]; NUnit.Framework.Assert.AreEqual("ca", loc.GetText()); Rectangle rect = loc.GetRectangle(); NUnit.Framework.Assert.AreEqual(36, rect.GetX(), 0.0001); NUnit.Framework.Assert.AreEqual(655.4600, rect.GetY(), 0.0001); NUnit.Framework.Assert.AreEqual(25.1000, rect.GetWidth(), 0.0001); NUnit.Framework.Assert.AreEqual(20, rect.GetHeight(), 0.0001); pdfDocument.Close(); }
private void removeDuplicates(IList <IPdfTextLocation> sortedList) { IPdfTextLocation lastItem = null; int orgSize = sortedList.Count; for (int i = orgSize - 1; i >= 0; i--) { IPdfTextLocation currItem = sortedList[i]; Rectangle currRect = currItem.GetRectangle(); if (lastItem != null && currRect.EqualsWithEpsilon(lastItem.GetRectangle())) { sortedList.Remove(currItem); } lastItem = currItem; } }
public virtual Color GetRedactionColor(IPdfTextLocation rect) { return(colorByRectangle.ContainsKey(rect.GetRectangle()) ? colorByRectangle.Get(rect.GetRectangle()) : ColorConstants .BLACK); }