/// <summary> /// Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified /// by the QuadPoints entry in the annotation dictionary. /// </summary> private void ExtractLocationsFromSingleRedactAnnotation(PdfRedactAnnotation redactAnnotation) { IList <Rectangle> regions; PdfArray quadPoints = redactAnnotation.GetQuadPoints(); if (quadPoints != null && !quadPoints.IsEmpty()) { regions = TranslateQuadPointsToRectangles(quadPoints); } else { regions = new List <Rectangle>(); regions.Add(redactAnnotation.GetRectangle().ToRectangle()); } redactAnnotations.Put(redactAnnotation, regions); int page = pdfDocument.GetPageNumber(redactAnnotation.GetPage()); Color cleanUpColor = redactAnnotation.GetInteriorColor(); PdfDictionary ro = redactAnnotation.GetRedactRolloverAppearance(); if (ro != null) { cleanUpColor = null; } foreach (Rectangle region in regions) { AddCleanupLocation(new PdfCleanUpLocation(page, region, cleanUpColor)); } }
public virtual void CleanUpTest45() { String input = inputPath + "emptyPdf.pdf"; String output = outputPath + "emptyPdf.pdf"; String cmp = inputPath + "cmp_emptyPdf.pdf"; PdfAnnotation redactAnnotation = new PdfRedactAnnotation(new Rectangle(97, 405, 383, 40)).SetOverlayText(new PdfString("OverlayTest")).SetDefaultAppearance(new PdfString("/Helv 0 Tf 0 g")); PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output)); pdfDocument.GetFirstPage().AddAnnotation(redactAnnotation); new PdfCleanUpTool(pdfDocument, true).CleanUp(); pdfDocument.Close(); CompareByContent(cmp, output, outputPath, "diff_45"); }
public virtual void CleanUpTest46() { NUnit.Framework.Assert.That(() => { String input = inputPath + "emptyPdf.pdf"; String output = outputPath + "emptyPdf.pdf"; PdfAnnotation redactAnnotation = new PdfRedactAnnotation(new Rectangle(97, 405, 383, 40)).SetOverlayText(new PdfString("OverlayTest")); PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output)); pdfDocument.GetFirstPage().AddAnnotation(redactAnnotation); new PdfCleanUpTool(pdfDocument, true).CleanUp(); pdfDocument.Close(); } , NUnit.Framework.Throws.TypeOf <PdfException>().With.Message.EqualTo(PdfException.DefaultAppearanceNotFound)); ; }
/// <summary> /// Perform tentative cleanup of areas of interest on a given /// <see cref="iText.Kernel.Pdf.PdfPage"/> /// This method will add all redaction annotations to the given page, allowing /// the end-user to choose which redactions to keep or delete. /// </summary> /// <param name="pdfPage">the page to clean up</param> public virtual void TentativeCleanUp(PdfPage pdfPage) { IList <PdfCleanUpLocation> cleanUpLocations = GetPdfCleanUpLocations(pdfPage); // random title generation Random rnd = new Random(SystemUtil.GetTimeBasedIntSeed()); ICollection <String> usedTitles = new HashSet <String>(); foreach (PdfCleanUpLocation loc in cleanUpLocations) { float[] color = loc.GetCleanUpColor().GetColorValue(); // generate random annotation title String title = "Annotation:" + rnd.Next(); while (usedTitles.Contains(title)) { title = "Annotation:" + rnd.Next(); } // convert to annotation PdfAnnotation redact = new PdfRedactAnnotation(loc.GetRegion()).SetDefaultAppearance(new PdfString("Helvetica 12 Tf 0 g" )).SetTitle(new PdfString(title)).Put(PdfName.Subj, PdfName.Redact).Put(PdfName.IC, new PdfArray(new float [] { 0f, 0f, 0f })).Put(PdfName.OC, new PdfArray(color)); pdfPage.AddAnnotation(redact); } }