Search in sources :

Example 1 with PdfCleanUpLocation

use of com.itextpdf.pdfcleanup.PdfCleanUpLocation in project i7j-pdfsweep by itext.

the class PdfAutoSweepTools method getPdfCleanUpLocations.

/**
 * Get all {@link PdfCleanUpLocation} objects from a given {@link PdfPage}.
 *
 * @param page the {@link PdfPage} to be processed
 *
 * @return a List of {@link PdfCleanUpLocation} objects
 */
public List<PdfCleanUpLocation> getPdfCleanUpLocations(PdfPage page) {
    // get document
    PdfDocument doc = page.getDocument();
    // create parser
    PdfDocumentContentParser parser = new PdfDocumentContentParser(doc);
    // get page number
    int pageNr = doc.getPageNumber(page);
    // process document
    List<PdfCleanUpLocation> toClean = new ArrayList<>();
    parser.processContent(pageNr, strategy);
    for (IPdfTextLocation rect : strategy.getResultantLocations()) {
        if (rect != null) {
            toClean.add(new PdfCleanUpLocation(pageNr, rect.getRectangle(), strategy.getRedactionColor(rect)));
        }
    }
    // reset strategy for next iteration
    resetStrategy();
    // return
    return toClean;
}
Also used : PdfDocumentContentParser(com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) ArrayList(java.util.ArrayList) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument)

Example 2 with PdfCleanUpLocation

use of com.itextpdf.pdfcleanup.PdfCleanUpLocation in project i7j-pdfsweep by itext.

the class PdfAutoSweepTools method getPdfCleanUpLocations.

/**
 * Get all {@link PdfCleanUpLocation} objects from a given {@link PdfDocument}.
 *
 * @param doc the {@link PdfDocument} to be processed
 *
 * @return a List of {@link PdfCleanUpLocation} objects
 */
public List<PdfCleanUpLocation> getPdfCleanUpLocations(PdfDocument doc) {
    PdfDocumentContentParser parser = new PdfDocumentContentParser(doc);
    List<PdfCleanUpLocation> toClean = new ArrayList<>();
    for (int pageNr = 1; pageNr <= doc.getNumberOfPages(); pageNr++) {
        parser.processContent(pageNr, strategy);
        for (IPdfTextLocation rect : strategy.getResultantLocations()) {
            if (rect != null) {
                toClean.add(new PdfCleanUpLocation(pageNr, rect.getRectangle(), strategy.getRedactionColor(rect)));
            }
        }
        resetStrategy();
    }
    java.util.Collections.sort(toClean, new Comparator<PdfCleanUpLocation>() {

        @Override
        public int compare(PdfCleanUpLocation o1, PdfCleanUpLocation o2) {
            if (o1.getPage() != o2.getPage()) {
                return o1.getPage() < o2.getPage() ? -1 : 1;
            }
            Rectangle r1 = o1.getRegion();
            Rectangle r2 = o2.getRegion();
            if (r1.getY() == r2.getY()) {
                return r1.getX() == r2.getX() ? 0 : (r1.getX() < r2.getX() ? -1 : 1);
            } else {
                return r1.getY() < r2.getY() ? -1 : 1;
            }
        }
    });
    return toClean;
}
Also used : PdfDocumentContentParser(com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle)

Example 3 with PdfCleanUpLocation

use of com.itextpdf.pdfcleanup.PdfCleanUpLocation in project i7j-pdfsweep by itext.

the class PdfAutoSweepTools method tentativeCleanUp.

/**
 * Perform tentative cleanup of areas of interest on a given {@link PdfPage}
 * This method will add all redaction annotations to the given page, allowing
 * the end-user to choose which redactions to keep or delete.
 *
 * @param pdfPage the page to clean up
 */
public void tentativeCleanUp(PdfPage pdfPage) {
    List<PdfCleanUpLocation> cleanUpLocations = getPdfCleanUpLocations(pdfPage);
    for (PdfCleanUpLocation loc : cleanUpLocations) {
        PdfString title = new PdfString("Annotation:" + annotationNumber);
        annotationNumber++;
        float[] color = loc.getCleanUpColor().getColorValue();
        // convert to annotation
        PdfAnnotation redact = new PdfRedactAnnotation(loc.getRegion()).setDefaultAppearance(new PdfString("Helvetica 12 Tf 0 g")).setTitle(title).put(PdfName.Subj, PdfName.Redact).put(PdfName.IC, new PdfArray(new float[] { 0f, 0f, 0f })).put(PdfName.OC, new PdfArray(color));
        pdfPage.addAnnotation(redact);
    }
}
Also used : PdfRedactAnnotation(com.itextpdf.kernel.pdf.annot.PdfRedactAnnotation) PdfArray(com.itextpdf.kernel.pdf.PdfArray) PdfAnnotation(com.itextpdf.kernel.pdf.annot.PdfAnnotation) PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) PdfString(com.itextpdf.kernel.pdf.PdfString)

Example 4 with PdfCleanUpLocation

use of com.itextpdf.pdfcleanup.PdfCleanUpLocation in project i7j-pdfsweep by itext.

the class CleanupImageWithColorSpaceTest method cleanUpTestColorSpaceJpegBaselineEncoded.

@Test
public void cleanUpTestColorSpaceJpegBaselineEncoded() throws Exception {
    // cleanup jpeg image with baseline encoded data
    String input = inputPath + "imgSeparationCsJpegBaselineEncoded.pdf";
    String output = outputPath + "imgSeparationCsJpegBaselineEncoded.pdf";
    String cmp = inputPath + "cmp_imgSeparationCsJpegBaselineEncoded.pdf";
    cleanUp(input, output, Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(60f, 600f, 100f, 50f), ColorConstants.GREEN)));
    compareByContent(cmp, output, outputPath, "11");
}
Also used : PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) Rectangle(com.itextpdf.kernel.geom.Rectangle) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 5 with PdfCleanUpLocation

use of com.itextpdf.pdfcleanup.PdfCleanUpLocation in project i7j-pdfsweep by itext.

the class CleanupImageWithColorSpaceTest method cleanUpTestColorSpace.

@Test
public void cleanUpTestColorSpace() throws Exception {
    String input = inputPath + "imgSeparationCs.pdf";
    String output = outputPath + "imgSeparationCs.pdf";
    String cmp = inputPath + "cmp_imgSeparationCs.pdf";
    cleanUp(input, output, Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(60f, 780f, 60f, 45f), ColorConstants.GREEN)));
    compareByContent(cmp, output, outputPath, "9");
}
Also used : PdfCleanUpLocation(com.itextpdf.pdfcleanup.PdfCleanUpLocation) Rectangle(com.itextpdf.kernel.geom.Rectangle) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Aggregations

PdfCleanUpLocation (com.itextpdf.pdfcleanup.PdfCleanUpLocation)12 Rectangle (com.itextpdf.kernel.geom.Rectangle)8 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)6 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)6 Test (org.junit.Test)6 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)5 PdfReader (com.itextpdf.kernel.pdf.PdfReader)4 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)4 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)2 PdfDocumentContentParser (com.itextpdf.kernel.pdf.canvas.parser.PdfDocumentContentParser)2 IPdfTextLocation (com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation)2 CompareTool (com.itextpdf.kernel.utils.CompareTool)2 CleanUpImagesCompareTool (com.itextpdf.pdfcleanup.util.CleanUpImagesCompareTool)2 ArrayList (java.util.ArrayList)2 PdfArray (com.itextpdf.kernel.pdf.PdfArray)1 PdfString (com.itextpdf.kernel.pdf.PdfString)1 PdfAnnotation (com.itextpdf.kernel.pdf.annot.PdfAnnotation)1 PdfRedactAnnotation (com.itextpdf.kernel.pdf.annot.PdfRedactAnnotation)1 LogMessages (com.itextpdf.test.annotations.LogMessages)1