Removing content with pdfSweep

31st May 2016
iText

These examples use the pdfSweep add-on and were written in answer to questions such as

RemoveContentInRectangle.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/27905740/remove-text-occurrences-contained-in-a-specified-area-with-itext
 */
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.PdfCleanUpTool;
 
import java.io.File;
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/test/resources/pdfs/page229.pdf";
 
    public static void main(String[] args) throws IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new RemoveContentInRectangle().manipulatePdf(DEST);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
 
        List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
        cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(97, 405, 383, 40), ColorConstants.GRAY));
 
        PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);
        cleaner.cleanUp();
 
        pdfDoc.close();
    }
}
RemoveRedactedContent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/24037282/any-way-to-create-redactions
 */
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.PdfCleanUpTool;
 
import java.io.File;
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/test/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);
    }
 
    public void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
 
        PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, true);
        cleaner.cleanUp();
 
        pdfDoc.close();
    }
 
}
RemoveContentInRectangle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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);
 
            PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, cleanUpLocations);
            cleaner.CleanUp();
 
            pdfDoc.Close();
        }
    }
}
RemoveRedactedContent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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 the second argument is true, then regions to be erased are extracted from the redact annotations
            // contained inside the given document. If the second argument is false (that's default behavior),
            // then use PdfCleanUpTool.addCleanupLocation(PdfCleanUpLocation)
            // method to set regions to be erased from the document.
            PdfCleanUpTool cleaner = new PdfCleanUpTool(pdfDoc, true);
            cleaner.CleanUp();
 
            pdfDoc.Close();
        }
    }
}


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