Search in sources :

Example 31 with Rectangle

use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.

the class CompositeCleanupStrategy method getResultantLocations.

@Override
public Collection<IPdfTextLocation> getResultantLocations() {
    locations.clear();
    // build return value
    Set<IPdfTextLocation> retval = new LinkedHashSet<>();
    for (int i = 0; i < strategies.size(); i++) {
        ILocationExtractionStrategy s = strategies.get(i);
        Collection<IPdfTextLocation> rects = s.getResultantLocations();
        retval.addAll(rects);
        locations.put(i, new HashSet<>(rects));
    }
    List<IPdfTextLocation> rectangles = new ArrayList<>(retval);
    java.util.Collections.sort(rectangles, new Comparator<IPdfTextLocation>() {

        @Override
        public int compare(IPdfTextLocation l1, IPdfTextLocation l2) {
            Rectangle r1 = l1.getRectangle();
            Rectangle r2 = l2.getRectangle();
            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
    return rectangles;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ILocationExtractionStrategy(com.itextpdf.kernel.pdf.canvas.parser.listener.ILocationExtractionStrategy) IPdfTextLocation(com.itextpdf.kernel.pdf.canvas.parser.listener.IPdfTextLocation) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle)

Example 32 with Rectangle

use of com.itextpdf.kernel.geom.Rectangle 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 33 with Rectangle

use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.

the class PdfCleanUpToolTest method autoSweepCleanUpWithAdditionalLocationTest.

@Test
public void autoSweepCleanUpWithAdditionalLocationTest() throws Exception {
    String in = INPUT_PATH + "page229.pdf";
    String out = OUTPUT_PATH + "autoSweepCleanUpWithAdditionalLocationTest.pdf";
    String cmp = INPUT_PATH + "cmp_autoSweepCleanUpWithAdditionalLocationTest.pdf";
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
    cleanUpLocations.add(new PdfCleanUpLocation(1, new Rectangle(100, 560, 200, 30)));
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy(" (T|t)o ").setRedactionColor(ColorConstants.GREEN));
    try (PdfReader reader = new PdfReader(in);
        PdfWriter writer = new PdfWriter(out);
        PdfDocument document = new PdfDocument(reader, writer)) {
        PdfCleaner.autoSweepCleanUp(document, strategy, cleanUpLocations);
    }
    CompareTool cmpTool = new CompareTool();
    String errorMessage = cmpTool.compareVisually(out, cmp, OUTPUT_PATH, "diff_autoSweepCleanUpWithAdditionalLocationTest_");
    if (errorMessage != null) {
        Assert.fail(errorMessage);
    }
}
Also used : RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) CompareTool(com.itextpdf.kernel.utils.CompareTool) CleanUpImagesCompareTool(com.itextpdf.pdfcleanup.util.CleanUpImagesCompareTool) PdfString(com.itextpdf.kernel.pdf.PdfString) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 34 with Rectangle

use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.

the class PdfCleanUpToolTest method documentWithoutReaderTest.

@Test
public void documentWithoutReaderTest() {
    PdfDocument pdfDocument = new PdfDocument(new PdfWriter(new ByteArrayOutputStream()));
    List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(0, 0, 500, 500)));
    Exception e = Assert.assertThrows(PdfException.class, () -> PdfCleaner.cleanUp(pdfDocument, cleanUpLocations));
    Assert.assertEquals(CleanupExceptionMessageConstant.PDF_DOCUMENT_MUST_BE_OPENED_IN_STAMPING_MODE, e.getMessage());
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Rectangle(com.itextpdf.kernel.geom.Rectangle) ByteArrayOutputStream(java.io.ByteArrayOutputStream) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) IOException(java.io.IOException) PdfException(com.itextpdf.kernel.exceptions.PdfException) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 35 with Rectangle

use of com.itextpdf.kernel.geom.Rectangle in project i7j-pdfsweep by itext.

the class PdfCleanUpToolTest method cleanUpTest39.

@Test
public void cleanUpTest39() throws IOException, InterruptedException {
    String input = INPUT_PATH + "corruptJpeg.pdf";
    String output = OUTPUT_PATH + "corruptJpeg.pdf";
    String cmp = INPUT_PATH + "cmp_corruptJpeg.pdf";
    List<PdfCleanUpLocation> cleanUpLocations = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(100, 350, 100, 200), ColorConstants.ORANGE));
    cleanUp(input, output, cleanUpLocations);
    CleanUpImagesCompareTool cmpTool = new CleanUpImagesCompareTool();
    String errorMessage = cmpTool.extractAndCompareImages(output, cmp, OUTPUT_PATH, "1.2");
    String compareByContentResult = cmpTool.compareByContent(output, cmp, OUTPUT_PATH);
    if (compareByContentResult != null) {
        errorMessage += compareByContentResult;
    }
    if (!errorMessage.equals("")) {
        Assert.fail(errorMessage);
    }
}
Also used : Rectangle(com.itextpdf.kernel.geom.Rectangle) CleanUpImagesCompareTool(com.itextpdf.pdfcleanup.util.CleanUpImagesCompareTool) PdfString(com.itextpdf.kernel.pdf.PdfString) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Aggregations

Rectangle (com.itextpdf.kernel.geom.Rectangle)118 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)76 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)76 Test (org.junit.Test)76 PdfString (com.itextpdf.kernel.pdf.PdfString)42 PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)41 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)37 ArrayList (java.util.ArrayList)29 PdfReader (com.itextpdf.kernel.pdf.PdfReader)22 PdfCanvas (com.itextpdf.kernel.pdf.canvas.PdfCanvas)16 Paragraph (com.itextpdf.layout.element.Paragraph)15 PdfFont (com.itextpdf.kernel.font.PdfFont)12 Document (com.itextpdf.layout.Document)12 Canvas (com.itextpdf.layout.Canvas)11 PdfPage (com.itextpdf.kernel.pdf.PdfPage)10 ColumnDocumentRenderer (com.itextpdf.layout.ColumnDocumentRenderer)9 LogMessages (com.itextpdf.test.annotations.LogMessages)9 AreaBreak (com.itextpdf.layout.element.AreaBreak)8 PdfCleanUpLocation (com.itextpdf.pdfcleanup.PdfCleanUpLocation)8 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)7