Search in sources :

Example 16 with Rectangle

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

the class FilteredImagesCacheTest method filteredImagesCacheTest01.

@Test
public void filteredImagesCacheTest01() throws IOException, InterruptedException {
    // basic test with reusing of xobjects
    String input = inputPath + "multipleImageXObjectOccurrences.pdf";
    String output = outputPath + "filteredImagesCacheTest01.pdf";
    String cmp = inputPath + "cmp_filteredImagesCacheTest01.pdf";
    PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
    for (int i = 0; i < pdfDocument.getNumberOfPages(); ++i) {
        cleanUpLocations.add(new PdfCleanUpLocation(i + 1, new Rectangle(150, 300, 300, 150)));
    }
    cleanUp(pdfDocument, cleanUpLocations);
    compareByContent(cmp, output, outputPath, "1.2");
    assertNumberXObjects(output, 1);
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) 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 17 with Rectangle

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

the class FilteredImagesCacheTest method filteredImagesCacheFlushingTest02.

@Test
public void filteredImagesCacheFlushingTest02() throws IOException, InterruptedException {
    String input = inputPath + "severalImageXObjectOccurrences.pdf";
    String output = outputPath + "filteredImagesCacheFlushingTest02.pdf";
    String cmp = inputPath + "cmp_filteredImagesCacheFlushingTest02.pdf";
    PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
    PdfCleanUpTool cleanUpTool = new PdfCleanUpTool(pdfDocument);
    cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(1, new Rectangle(150, 300, 300, 150)));
    cleanUpTool.cleanUp();
    PdfImageXObject img = pdfDocument.getPage(1).getResources().getImage(new PdfName("Im1"));
    img.makeIndirect(pdfDocument).flush();
    cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(2, new Rectangle(150, 300, 300, 150)));
    cleanUpTool.cleanUp();
    cleanUpTool.addCleanupLocation(new PdfCleanUpLocation(3, new Rectangle(150, 300, 300, 150)));
    cleanUpTool.cleanUp();
    pdfDocument.close();
    compareByContent(cmp, output, outputPath, "1.2");
    assertNumberXObjects(output, 1);
}
Also used : PdfImageXObject(com.itextpdf.kernel.pdf.xobject.PdfImageXObject) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfName(com.itextpdf.kernel.pdf.PdfName) Rectangle(com.itextpdf.kernel.geom.Rectangle) 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 18 with Rectangle

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

the class PdfCleanUpProcessor method annotationIsToBeRedacted.

private boolean annotationIsToBeRedacted(PdfAnnotation annotation, Rectangle redactRegion) {
    // TODO(DEVSIX-1605,DEVSIX-1606,DEVSIX-1607,DEVSIX-1608,DEVSIX-1609)
    removeAnnotIfPartOverlap = true;
    PdfName annotationType = annotation.getPdfObject().getAsName(PdfName.Subtype);
    if (annotationType.equals(PdfName.Watermark)) {
        // TODO /FixedPrint entry effect is not fully investigated: DEVSIX-2471
        Logger logger = LoggerFactory.getLogger(PdfCleanUpProcessor.class);
        logger.warn(CleanUpLogMessageConstant.REDACTION_OF_ANNOTATION_TYPE_WATERMARK_IS_NOT_SUPPORTED);
    }
    PdfArray rectAsArray = annotation.getRectangle();
    Rectangle rect = null;
    if (rectAsArray != null) {
        rect = rectAsArray.toRectangle();
    }
    boolean annotationIsToBeRedacted = processAnnotationRectangle(redactRegion, rect);
    // Special processing for some types of annotations.
    if (PdfName.Link.equals(annotationType)) {
        PdfArray quadPoints = ((PdfLinkAnnotation) annotation).getQuadPoints();
        if (quadPointsForLinkAnnotationAreValid(rect, quadPoints)) {
            annotationIsToBeRedacted = processAnnotationQuadPoints(redactRegion, quadPoints);
        }
    } else if (annotationType.equals(PdfName.Highlight) || annotationType.equals(PdfName.Underline) || annotationType.equals(PdfName.Squiggly) || annotationType.equals(PdfName.StrikeOut)) {
        PdfArray quadPoints = ((PdfTextMarkupAnnotation) annotation).getQuadPoints();
        // The annotation dictionary’s AP entry, if present, shall take precedence over QuadPoints.
        if (quadPoints != null && annotation.getAppearanceDictionary() == null) {
            try {
                annotationIsToBeRedacted = processAnnotationQuadPoints(redactRegion, quadPoints);
            } catch (PdfException ignored) {
            // if quad points array cannot be processed, simply ignore it
            }
        }
    } else if (annotationType.equals(PdfName.Line)) {
        PdfArray line = ((PdfLineAnnotation) annotation).getLine();
        if (line != null) {
            Rectangle drawnLineRectangle = line.toRectangle();
            // Line annotation might contain line leaders, so let's double check overlapping with /Rect area, for simplicity.
            // TODO DEVSIX-1607
            annotationIsToBeRedacted = annotationIsToBeRedacted || processAnnotationRectangle(redactRegion, drawnLineRectangle);
        }
    }
    return annotationIsToBeRedacted;
}
Also used : PdfArray(com.itextpdf.kernel.pdf.PdfArray) PdfException(com.itextpdf.kernel.exceptions.PdfException) PdfName(com.itextpdf.kernel.pdf.PdfName) PdfLinkAnnotation(com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation) PdfLineAnnotation(com.itextpdf.kernel.pdf.annot.PdfLineAnnotation) Rectangle(com.itextpdf.kernel.geom.Rectangle) Logger(org.slf4j.Logger)

Example 19 with Rectangle

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

the class PdfCleanUpTool method cleanUpPage.

/**
 * Cleans a page from the document by erasing all the areas which
 * are provided or extracted from redaction annotations.
 *
 * @param pageNumber the page to be cleaned up
 * @param cleanUpLocations the locations to be cleaned up
 */
private void cleanUpPage(int pageNumber, List<PdfCleanUpLocation> cleanUpLocations) {
    if (cleanUpLocations.size() == 0) {
        return;
    }
    List<Rectangle> regions = new ArrayList<>();
    for (PdfCleanUpLocation cleanUpLocation : cleanUpLocations) {
        regions.add(cleanUpLocation.getRegion());
    }
    PdfPage page = pdfDocument.getPage(pageNumber);
    PdfCleanUpProcessor cleanUpProcessor = new PdfCleanUpProcessor(regions, pdfDocument);
    cleanUpProcessor.setFilteredImagesCache(filteredImagesCache);
    cleanUpProcessor.processPageContent(page);
    if (processAnnotations) {
        cleanUpProcessor.processPageAnnotations(page, regions, redactAnnotations != null);
    }
    PdfCanvas pageCleanedContents = cleanUpProcessor.popCleanedCanvas();
    page.put(PdfName.Contents, pageCleanedContents.getContentStream());
    page.setResources(pageCleanedContents.getResources());
    colorCleanedLocations(pageCleanedContents, cleanUpLocations);
}
Also used : Rectangle(com.itextpdf.kernel.geom.Rectangle) ArrayList(java.util.ArrayList) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfPage(com.itextpdf.kernel.pdf.PdfPage)

Example 20 with Rectangle

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

the class PdfCleanUpTool method extractLocationsFromSingleRedactAnnotation.

/**
 * Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified
 * by the QuadPoints entry in the annotation dictionary.
 */
private void extractLocationsFromSingleRedactAnnotation(PdfRedactAnnotation redactAnnotation) {
    List<Rectangle> regions;
    PdfArray quadPoints = redactAnnotation.getQuadPoints();
    if (quadPoints != null && !quadPoints.isEmpty()) {
        regions = translateQuadPointsToRectangles(quadPoints);
    } else {
        regions = new ArrayList<>();
        regions.add(redactAnnotation.getRectangle().toRectangle());
    }
    redactAnnotations.put(redactAnnotation, regions);
    int page = pdfDocument.getPageNumber(redactAnnotation.getPage());
    Color cleanUpColor = redactAnnotation.getInteriorColor();
    PdfDictionary ro = redactAnnotation.getRedactRolloverAppearance();
    if (ro != null) {
        cleanUpColor = null;
    }
    for (Rectangle region : regions) {
        addCleanupLocation(new PdfCleanUpLocation(page, region, cleanUpColor));
    }
}
Also used : PdfArray(com.itextpdf.kernel.pdf.PdfArray) PdfDictionary(com.itextpdf.kernel.pdf.PdfDictionary) Color(com.itextpdf.kernel.colors.Color) Rectangle(com.itextpdf.kernel.geom.Rectangle)

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