Removing content

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

5th November 2015
admin-marketing

Switch code for this example

RemoveContentInRectangle.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/27905740/remove-text-occurrences-contained-in-a-specified-area-with-itext
  4.  */
  5.  
  6. package sandbox.parse;
  7.  
  8. import com.itextpdf.text.BaseColor;
  9. import com.itextpdf.text.DocumentException;
  10. import com.itextpdf.text.Rectangle;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpLocation;
  14. import com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import sandbox.WrapToTest;
  22.  
  23. @WrapToTest
  24. public class RemoveContentInRectangle {
  25.  
  26.     public static final String SRC = "resources/pdfs/page229.pdf";
  27.     public static final String DEST = "results/parse/page229_removed_content.pdf";
  28.    
  29.     public static void main(String[] args) throws IOException, DocumentException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new RemoveContentInRectangle().manipulatePdf(SRC, DEST);
  33.     }
  34.    
  35.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         List cleanUpLocations = new ArrayList();
  39.         cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97f, 405f, 480f, 445f), BaseColor.GRAY));
  40.         PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
  41.         cleaner.cleanUp();
  42.         stamper.close();
  43.         reader.close();
  44.     }
  45. }
RemoveRedactedContent.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/24037282/any-way-to-create-redactions
  4.  */
  5.  
  6. package sandbox.parse;
  7.  
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11. import com.itextpdf.text.pdf.pdfcleanup.PdfCleanUpProcessor;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class RemoveRedactedContent {
  20.  
  21.     public static final String SRC = "resources/pdfs/page229_redacted.pdf";
  22.     public static final String DEST = "results/parse/page229_apply_redacted.pdf";
  23.    
  24.     public static void main(String[] args) throws IOException, DocumentException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new RemoveRedactedContent().manipulatePdf(SRC, DEST);
  28.     }
  29.    
  30.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  31.         PdfReader reader = new PdfReader(src);
  32.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  33.         PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(stamper);
  34.         cleaner.cleanUp();
  35.         stamper.close();
  36.         reader.close();
  37.     }
  38. }
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