Redact PDF via Java

PDF document sensitive redaction information. Use Aspose.PDF for Java to modify PDF documents programmatically

How to Redact PDF File Using Java Library

In order to redact PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful and easy to use conversion API for Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Redact PDF documents via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Create TextFragmentAbsorber object with search terms as argument.
  3. Set Search Options.
  4. Loop through each fragment collect to redact.
  5. Save PDF file.

Redact PDF Files - Java


    Document doc = new Document("sourceFile.pdf");

    Rectangle rect = new Rectangle(200, 500, 300, 600);
    RedactionAnnotation annot = new RedactionAnnotation(doc.getPages().get_Item(1), rect);

    annot.setFillColor(Color.getBlack());
    annot.setBorderColor(Color.getYellow());
    annot.setColor(Color.getBlue());
    annot.setFontSize(20);
    annot.setOverlayText("REDACTED");
    annot.setTextAlignment(HorizontalAlignment.Center);
    annot.setRepeat(true);
    
    doc.getPages().get_Item(1).getAnnotations().add(annot);
    doc.save("output.pdf");