Removing content with pdfSweep

This is a code example of iText PDF, discover more.

31st May 2016
iText PDF

Switch code for this example

RemoveContentInRectangle.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/27905740/remove-text-occurrences-contained-in-a-specified-area-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.parse;
  13.  
  14. import com.itextpdf.kernel.color.Color;
  15. import com.itextpdf.kernel.geom.Rectangle;
  16. import com.itextpdf.kernel.pdf.PdfDocument;
  17. import com.itextpdf.kernel.pdf.PdfReader;
  18. import com.itextpdf.kernel.pdf.PdfWriter;
  19. import com.itextpdf.licensekey.LicenseKey;
  20. import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
  21. import com.itextpdf.pdfcleanup.PdfCleanUpTool;
  22. import com.itextpdf.samples.GenericTest;
  23. import com.itextpdf.test.annotations.type.SampleTest;
  24.  
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31.  
  32. @Category(SampleTest.class)
  33. public class RemoveContentInRectangle extends GenericTest {
  34.     public static final String DEST = "./target/test/resources/sandbox/parse/remove_content_in_rectangle.pdf";
  35.     public static final String SRC = "./src/test/resources/pdfs/page229.pdf";
  36.  
  37.     public static void main(String[] args) throws IOException {
  38.         File file = new File(DEST);
  39.         file.getParentFile().mkdirs();
  40.         new RemoveContentInRectangle().manipulatePdf(DEST);
  41.     }
  42.  
  43.     public void manipulatePdf(String dest) throws IOException {
  44.         //Load the license file to use cleanup features
  45.         LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-multiple-products.xml");
  46.  
  47.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
  48.  
  49.         List cleanUpLocations = new ArrayList();
  50.         cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40), Color.GRAY));
  51.  
  52.         PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);
  53.         cleaner.cleanUp();
  54.  
  55.         pdfDoc.close();
  56.     }
  57. }
RemoveRedactedContent.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/24037282/any-way-to-create-redactions
  11.  */
  12. package com.itextpdf.samples.sandbox.parse;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.licensekey.LicenseKey;
  18. import com.itextpdf.pdfcleanup.PdfCleanUpTool;
  19. import com.itextpdf.samples.GenericTest;
  20. import com.itextpdf.test.annotations.type.SampleTest;
  21.  
  22. import org.junit.experimental.categories.Category;
  23.  
  24. import java.io.File;
  25. import java.io.IOException;
  26.  
  27. @Category(SampleTest.class)
  28. public class RemoveRedactedContent extends GenericTest {
  29.     public static final String DEST = "./target/test/resources/sandbox/parse/remove_redacted_content.pdf";
  30.     public static final String SRC = "./src/test/resources/pdfs/page229_redacted.pdf";
  31.  
  32.     public static void main(String[] args) throws IOException {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new RemoveRedactedContent().manipulatePdf(DEST);
  36.     }
  37.  
  38.     public void manipulatePdf(String dest) throws IOException {
  39.         //Load the license file to use cleanup features
  40.         LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/itextkey-multiple-products.xml");
  41.  
  42.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
  43.  
  44.         PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, true);
  45.         cleaner.cleanUp();
  46.  
  47.         pdfDoc.close();
  48.     }
  49.  
  50. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now