Search in sources :

Example 6 with PdfReader

use of com.itextpdf.kernel.pdf.PdfReader 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 7 with PdfReader

use of com.itextpdf.kernel.pdf.PdfReader 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 8 with PdfReader

use of com.itextpdf.kernel.pdf.PdfReader 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)

Example 9 with PdfReader

use of com.itextpdf.kernel.pdf.PdfReader 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 10 with PdfReader

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

the class FilteredImagesCacheTest method assertNumberXObjects.

private void assertNumberXObjects(String output, int n) throws IOException {
    PdfDocument doc = new PdfDocument(new PdfReader(output));
    int xObjCount = 0;
    for (int i = 0; i < doc.getNumberOfPdfObjects(); ++i) {
        PdfObject pdfObject = doc.getPdfObject(i);
        if (pdfObject != null && pdfObject.isStream()) {
            PdfDictionary dict = (PdfDictionary) pdfObject;
            if (PdfName.Image.equals(dict.getAsName(PdfName.Subtype)) && dict.containsKey(PdfName.Width) && dict.containsKey(PdfName.Height)) {
                ++xObjCount;
            }
        }
    }
    Assert.assertEquals(n, xObjCount);
}
Also used : PdfDictionary(com.itextpdf.kernel.pdf.PdfDictionary) PdfObject(com.itextpdf.kernel.pdf.PdfObject) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument)

Aggregations

PdfDocument (com.itextpdf.kernel.pdf.PdfDocument)64 PdfReader (com.itextpdf.kernel.pdf.PdfReader)64 PdfWriter (com.itextpdf.kernel.pdf.PdfWriter)56 ExtendedITextTest (com.itextpdf.test.ExtendedITextTest)43 IntegrationTest (com.itextpdf.test.annotations.type.IntegrationTest)43 Test (org.junit.Test)43 Rectangle (com.itextpdf.kernel.geom.Rectangle)22 CompositeCleanupStrategy (com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy)22 RegexBasedCleanupStrategy (com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy)20 PdfString (com.itextpdf.kernel.pdf.PdfString)13 ConfirmEvent (com.itextpdf.commons.actions.confirmations.ConfirmEvent)11 ArrayList (java.util.ArrayList)11 ByteArrayOutputStream (com.itextpdf.io.source.ByteArrayOutputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)9 CleanUpImagesCompareTool (com.itextpdf.pdfcleanup.util.CleanUpImagesCompareTool)7 CompareTool (com.itextpdf.kernel.utils.CompareTool)5 Document (com.itextpdf.layout.Document)5 PdfAutoSweepTools (com.itextpdf.pdfcleanup.autosweep.PdfAutoSweepTools)5 PdfCleanUpLocation (com.itextpdf.pdfcleanup.PdfCleanUpLocation)4 FileInputStream (java.io.FileInputStream)4