Search in sources :

Example 66 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class CleanupLicenseEventsTest method cleanUpRedactAnnotationsSendsCoreAndCleanUpEventTest.

@Test
public void cleanUpRedactAnnotationsSendsCoreAndCleanUpEventTest() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument document = new PdfDocument(new PdfReader(INPUT_PATH + "absentICentry.pdf"), new PdfWriter(baos));
    String oldProducer = document.getDocumentInfo().getProducer();
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<PdfCleanUpLocation>();
    PdfCleanUpLocation lineLoc = new PdfCleanUpLocation(1, new Rectangle(100, 560, 200, 30), ColorConstants.GREEN);
    cleanUpLocations.add(lineLoc);
    PdfCleaner.cleanUpRedactAnnotations(document, new CleanUpProperties());
    document.close();
    List<ConfirmEvent> events = handler.getEvents();
    Assert.assertEquals(2, events.size());
    Assert.assertEquals(ITextCoreProductEvent.PROCESS_PDF, events.get(0).getEvent().getEventType());
    Assert.assertEquals(PdfSweepProductEvent.CLEANUP_PDF, events.get(1).getEvent().getEventType());
    try (PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())))) {
        String expectedProdLine = createExpectedProducerLine(new ConfirmedEventWrapper[] { getCoreEvent(), getCleanUpEvent() }, oldProducer);
        Assert.assertEquals(expectedProdLine, pdfDocument.getDocumentInfo().getProducer());
    }
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) ArrayList(java.util.ArrayList) Rectangle(com.itextpdf.kernel.geom.Rectangle) ByteArrayOutputStream(com.itextpdf.io.source.ByteArrayOutputStream) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfirmEvent(com.itextpdf.commons.actions.confirmations.ConfirmEvent) IntegrationTest(com.itextpdf.test.annotations.type.IntegrationTest) ExtendedITextTest(com.itextpdf.test.ExtendedITextTest) Test(org.junit.Test)

Example 67 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class CleanupLicenseEventsTest method autoSweepCleanUpSendsCoreAndCleanUpEventsTest.

@Test
public void autoSweepCleanUpSendsCoreAndCleanUpEventsTest() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument document = new PdfDocument(new PdfReader(INPUT_PATH + "fontCleanup.pdf"), new PdfWriter(baos));
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy("leonard"));
    PdfCleaner.autoSweepCleanUp(document, strategy);
    String oldProducer = document.getDocumentInfo().getProducer();
    document.close();
    List<ConfirmEvent> events = handler.getEvents();
    Assert.assertEquals(2, events.size());
    Assert.assertEquals(ITextCoreProductEvent.PROCESS_PDF, events.get(0).getEvent().getEventType());
    Assert.assertEquals(PdfSweepProductEvent.CLEANUP_PDF, events.get(1).getEvent().getEventType());
    try (PdfDocument pdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray())))) {
        String expectedProdLine = createExpectedProducerLine(new ConfirmedEventWrapper[] { getCoreEvent(), getCleanUpEvent() }, oldProducer);
        Assert.assertEquals(expectedProdLine, pdfDocument.getDocumentInfo().getProducer());
    }
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) ByteArrayInputStream(java.io.ByteArrayInputStream) ConfirmEvent(com.itextpdf.commons.actions.confirmations.ConfirmEvent) ByteArrayOutputStream(com.itextpdf.io.source.ByteArrayOutputStream) 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 68 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class FilteredImagesCacheTest method filteredImagesCacheFlushingTest01.

@Test
public void filteredImagesCacheFlushingTest01() throws IOException, InterruptedException {
    String input = inputPath + "severalImageXObjectOccurrences.pdf";
    String output = outputPath + "filteredImagesCacheFlushingTest01.pdf";
    String cmp = inputPath + "cmp_filteredImagesCacheFlushingTest01.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(2).getResources().getImage(new PdfName("Im1"));
    img.getPdfObject().release();
    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 69 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class FilteredImagesCacheTest method filteredImagesCacheTest04.

@Test
public void filteredImagesCacheTest04() throws IOException, InterruptedException {
    // same image with different scaling and the same resultant image area
    String input = inputPath + "multipleScaledImageXObjectOccurrences.pdf";
    String output = outputPath + "filteredImagesCacheTest04.pdf";
    String cmp = inputPath + "cmp_filteredImagesCacheTest04.pdf";
    PdfDocument pdfDocument = new PdfDocument(new PdfReader(input), new PdfWriter(output));
    List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
    float x = 559;
    float y = 600.2f;
    float width = 100;
    float height = 100;
    Rectangle region1 = new Rectangle(x - width, y - height, width, height);
    float scaleFactor = 1.2f;
    width *= scaleFactor;
    height *= scaleFactor;
    Rectangle region2 = new Rectangle(x - width, y - height, width, height);
    for (int i = 0; i < pdfDocument.getNumberOfPages(); i += 2) {
        cleanUpLocations.add(new PdfCleanUpLocation(i + 1, region1));
        cleanUpLocations.add(new PdfCleanUpLocation(i + 2, region2));
    }
    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 70 with PdfDocument

use of com.itextpdf.kernel.pdf.PdfDocument in project i7j-pdfsweep by itext.

the class FilteredImagesCacheTest method filteredImagesCacheTest03.

@Test
public void filteredImagesCacheTest03() throws IOException, InterruptedException {
    // same areas, different src images
    String input = inputPath + "multipleDifferentImageXObjectOccurrences.pdf";
    String output = outputPath + "filteredImagesCacheTest03.pdf";
    String cmp = inputPath + "cmp_filteredImagesCacheTest03.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, 2);
}
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)

Aggregations

PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)205 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)194 Document (com.itextpdf.layout.Document)131 Paragraph (com.itextpdf.layout.element.Paragraph)110 PdfReader (com.itextpdf.kernel.pdf.PdfReader)64 PdfFont (com.itextpdf.kernel.font.PdfFont)45 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)45 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)45 Test (org.junit.Test)45 Rectangle (com.itextpdf.kernel.geom.Rectangle)41 HyphenationConfig (com.itextpdf.layout.hyphenation.HyphenationConfig)32 BufferedReader (java.io.BufferedReader)26 FileReader (java.io.FileReader)26 ArrayList (java.util.ArrayList)26 AreaBreak (com.itextpdf.layout.element.AreaBreak)25 Image (com.itextpdf.layout.element.Image)25 Table (com.itextpdf.layout.element.Table)25 CompositeCleanupStrategy (com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy)22 List (java.util.List)22 Cell (com.itextpdf.layout.element.Cell)21