Search in sources :

Example 71 with PdfDocument

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

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

Example 73 with PdfDocument

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

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

the class PdfAutoSweepToolsTest method highlightTest.

@Test
public void highlightTest() throws IOException, InterruptedException {
    String input = INPUT_PATH + "Lipsum.pdf";
    String output = OUTPUT_PATH + "highlightTest.pdf";
    String cmp = INPUT_PATH + "cmp_highlightTest.pdf";
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy("(D|d)olor").setRedactionColor(ColorConstants.GREEN));
    PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(output).setCompressionLevel(CompressionConstants.NO_COMPRESSION));
    // sweep
    PdfAutoSweepTools autoSweep = new PdfAutoSweepTools(strategy);
    autoSweep.highlight(pdf);
    pdf.close();
    // compare
    compareByContent(cmp, output, OUTPUT_PATH, "diff_highlightTest_");
}
Also used : RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfAutoSweepTools(com.itextpdf.pdfcleanup.autosweep.PdfAutoSweepTools) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) 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 75 with PdfDocument

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

the class PdfAutoSweepToolsTest method getPdfCleanUpLocationsTest.

@Test
public void getPdfCleanUpLocationsTest() throws IOException {
    String input = INPUT_PATH + "Lipsum.pdf";
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy("(D|d)olor"));
    PdfDocument pdf = new PdfDocument(new PdfReader(input), new PdfWriter(new ByteArrayOutputStream()));
    // sweep
    List cleanUpLocations = (List) new PdfAutoSweepTools(strategy).getPdfCleanUpLocations(pdf.getPage(1));
    pdf.close();
    // compare
    Assert.assertEquals(2, cleanUpLocations.size());
}
Also used : RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfAutoSweepTools(com.itextpdf.pdfcleanup.autosweep.PdfAutoSweepTools) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) List(java.util.List) PdfReader(com.itextpdf.kernel.pdf.PdfReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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