Removing content with pdfSweep
These examples use the pdfSweep add-on and were written in answer to questions such as
removecontentinrectangle
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.parse;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.pdfcleanup.PdfCleanUpLocation;
import com.itextpdf.pdfcleanup.PdfCleaner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class RemoveContentInRectangle {
public static final String DEST = "./target/sandbox/parse/remove_content_in_rectangle.pdf";
public static final String SRC = "./src/main/resources/pdfs/page229.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RemoveContentInRectangle().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
// The arguments of the PdfCleanUpLocation constructor: the number of page to be cleaned up,
// a Rectangle defining the area on the page we want to clean up,
// a color which will be used while filling the cleaned area.
PdfCleanUpLocation location = new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40),
ColorConstants.GRAY);
cleanUpLocations.add(location);
PdfCleaner.cleanUp(pdfDoc, cleanUpLocations);
pdfDoc.close();
}
}
C#
C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.PdfCleanup;
namespace iText.Samples.Sandbox.Parse
{
public class RemoveContentInRectangle
{
public static readonly String DEST = "results/sandbox/parse/remove_content_in_rectangle.pdf";
public static readonly String SRC = "../../../resources/pdfs/page229.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new RemoveContentInRectangle().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
IList<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>();
// The arguments of the PdfCleanUpLocation constructor: the number of page to be cleaned up,
// a Rectangle defining the area on the page we want to clean up,
// a color which will be used while filling the cleaned area.
PdfCleanUpLocation location = new PdfCleanUpLocation(1,
new Rectangle(97, 405, 383, 40), ColorConstants.GRAY);
cleanUpLocations.Add(location);
PdfCleaner.CleanUp(pdfDoc, cleanUpLocations);
pdfDoc.Close();
}
}
}
removeredactedcontent
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.parse;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.pdfcleanup.PdfCleaner;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class RemoveRedactedContent {
public static final String DEST = "./target/sandbox/parse/remove_redacted_content.pdf";
public static final String SRC = "./src/main/resources/pdfs/page229_redacted.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new RemoveRedactedContent().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
// If you use cleanUpRedactAnnotations method, then regions to be erased are extracted from the redact annotations
// contained inside the given document.
PdfCleaner.cleanUpRedactAnnotations(pdfDoc);
pdfDoc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.PdfCleanup;
namespace iText.Samples.Sandbox.Parse
{
public class RemoveRedactedContent
{
public static readonly String DEST = "results/sandbox/parse/remove_redacted_content.pdf";
public static readonly String SRC = "../../../resources/pdfs/page229_redacted.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new RemoveRedactedContent().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
// If you use CleanUpRedactAnnotations method, then regions to be erased are extracted from the redact annotations
// contained inside the given document.
PdfCleaner.CleanUpRedactAnnotations(pdfDoc);
pdfDoc.Close();
}
}
}