Search in sources :

Example 31 with PdfReader

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

the class PdfCleanUpToolTest method autoCleanPageWithAdditionalLocationTest.

@Test
public void autoCleanPageWithAdditionalLocationTest() throws Exception {
    String input = INPUT_PATH + "fontCleanup.pdf";
    String output = OUTPUT_PATH + "autoCleanPageWithAdditionalLocationTest.pdf";
    String cmp = INPUT_PATH + "cmp_autoCleanPageWithAdditionalLocationTest.pdf";
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy("leonard"));
    List<PdfCleanUpLocation> additionalLocation = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(0, 0, 200, 100), ColorConstants.RED));
    try (PdfReader reader = new PdfReader(input);
        PdfWriter writer = new PdfWriter(output);
        PdfDocument document = new PdfDocument(reader, writer)) {
        PdfCleaner.autoSweepCleanUp(document.getPage(1), strategy, additionalLocation);
    }
    compareByContent(cmp, output, OUTPUT_PATH, "autoCleanPageWithAdditionalLocationTest");
}
Also used : RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) Rectangle(com.itextpdf.kernel.geom.Rectangle) 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 32 with PdfReader

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

the class PdfCleanUpToolTest method documentInNonStampingModeTest.

@Test
public void documentInNonStampingModeTest() throws IOException {
    PdfDocument pdfDocument = new PdfDocument(new PdfReader(INPUT_PATH + "fontCleanup.pdf"));
    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 : Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfReader(com.itextpdf.kernel.pdf.PdfReader) 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 33 with PdfReader

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

the class PdfCleanUpToolTest method autoCleanPageWithAdditionalLocationAndPropertyTest.

@Test
public void autoCleanPageWithAdditionalLocationAndPropertyTest() throws Exception {
    String input = INPUT_PATH + "fontCleanup.pdf";
    String output = OUTPUT_PATH + "autoCleanPageWithAdditionalLocationAndPropertyTest.pdf";
    String cmp = INPUT_PATH + "cmp_autoCleanPageWithAdditionalLocationAndPropertyTest.pdf";
    CompositeCleanupStrategy strategy = new CompositeCleanupStrategy();
    strategy.add(new RegexBasedCleanupStrategy("leonard"));
    List<PdfCleanUpLocation> additionalLocation = Arrays.asList(new PdfCleanUpLocation(1, new Rectangle(0, 0, 200, 100), ColorConstants.RED));
    try (PdfReader reader = new PdfReader(input);
        PdfWriter writer = new PdfWriter(output);
        PdfDocument document = new PdfDocument(reader, writer)) {
        PdfCleaner.autoSweepCleanUp(document.getPage(1), strategy, additionalLocation, new CleanUpProperties());
    }
    compareByContent(cmp, output, OUTPUT_PATH, "autoCleanPageWithAdditionalLocationAndPropertyTest");
}
Also used : RegexBasedCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.RegexBasedCleanupStrategy) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) CompositeCleanupStrategy(com.itextpdf.pdfcleanup.autosweep.CompositeCleanupStrategy) Rectangle(com.itextpdf.kernel.geom.Rectangle) 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 PdfReader

use of com.itextpdf.kernel.pdf.PdfReader in project MtgDesktopCompanion by nicho92.

the class DCIDeckSheetExport method exportDeck.

@Override
public void exportDeck(MagicDeck deck, File dest) throws IOException {
    try (var pdfSrc = new PdfDocument(new PdfReader(this.getClass().getResource("/data/mtg_constructed_deck_registration_sheet.pdf").openStream()));
        var docSrc = new Document(pdfSrc);
        var pdfDest = new PdfDocument(new PdfWriter(dest));
        var docDest = new Document(pdfDest)) {
        pdfDest.setDefaultPageSize(PageSize.A4);
        pdfSrc.copyPagesTo(1, 1, pdfDest);
        float h = pdfDest.getDefaultPageSize().getHeight();
        float w = pdfDest.getDefaultPageSize().getWidth();
        // HEADER
        docDest.add(createParagraphe(getString(LAST_NAME).substring(0, 1).toUpperCase(), w - 35, h - 103));
        if (!getString(FORCED_DATE).equalsIgnoreCase(""))
            docDest.add(createParagraphe(getString(FORCED_DATE), w / 3.2f, h - 127));
        else
            docDest.add(createParagraphe(new SimpleDateFormat(getString(DATE_FORMAT)).format(new Date()), w / 3.2f, h - 127));
        docDest.add(createParagraphe(getString(LOCATION), w / 3.2f, h - 150));
        docDest.add(createParagraphe(getString(EVENT_NAME), w / 1.40f, h - 127));
        docDest.add(createParagraphe(deck.getName(), w / 1.40f, h - 150));
        if (getString(DECK_DESIGNER).equals(""))
            docDest.add(createParagraphe(getString(LAST_NAME) + " " + getString(FIRST_NAME), w / 1.40f, h - 175));
        else
            docDest.add(createParagraphe(getString(DECK_DESIGNER), w / 1.40f, h - 175));
        // MAIN DECK
        var count = 0;
        for (Entry<MagicCard, Integer> e : deck.getMain().entrySet().stream().filter(e -> !e.getKey().isBasicLand()).toList()) {
            docDest.add(createParagraphe(e.getValue() + space + e.getKey().getName(), w / 6.4f, h - 240 - count));
            count += 18;
            notify(e.getKey());
        }
        count = 0;
        for (Entry<MagicCard, Integer> e : deck.getMain().entrySet().stream().filter(e -> e.getKey().isBasicLand()).toList()) {
            docDest.add(createParagraphe(e.getValue() + space + e.getKey().getName(), w / 1.65f, h - 240 - count));
            count += 18;
            notify(e.getKey());
        }
        // SIDEBOARD
        count = 0;
        for (MagicCard mc : deck.getSideBoard().keySet()) {
            docDest.add(createParagraphe(deck.getSideBoard().get(mc) + space + mc.getName(), w / 1.65f, h - 474 - count));
            notify(mc);
            count += 18;
        }
        // BOTTOM card count
        docDest.add(createParagraphe(String.valueOf(deck.getMainAsList().size()), (w / 2) - 25, 40));
        docDest.add(createParagraphe(String.valueOf(deck.getSideAsList().size()), w - 55, 95));
        // LEFT TEXT
        var p = createParagraphe(getString(LAST_NAME), 60, 90);
        p.setRotationAngle(1.5708f);
        docDest.add(p);
        p = createParagraphe(getString(FIRST_NAME), 60, 300);
        p.setRotationAngle(1.5708f);
        docDest.add(p);
        var dci = getString(DCI_NUMBER);
        count = 0;
        for (var i = 0; i < dci.length(); i++) {
            var c = dci.charAt(i);
            p = createParagraphe(String.valueOf(c), 60, (430 + count));
            p.setRotationAngle(1.5708f);
            docDest.add(p);
            count += 22;
        }
    } catch (Exception e) {
        throw new IOException(e.getMessage());
    }
}
Also used : NotImplementedException(org.apache.commons.lang3.NotImplementedException) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Date(java.util.Date) MagicDeck(org.magic.api.beans.MagicDeck) SimpleDateFormat(java.text.SimpleDateFormat) IOException(java.io.IOException) Icon(javax.swing.Icon) MagicCardStock(org.magic.api.beans.MagicCardStock) StandardFonts(com.itextpdf.io.font.constants.StandardFonts) File(java.io.File) Paragraph(com.itextpdf.layout.element.Paragraph) List(java.util.List) AbstractCardExport(org.magic.api.interfaces.abstracts.AbstractCardExport) PdfReader(com.itextpdf.kernel.pdf.PdfReader) Map(java.util.Map) Entry(java.util.Map.Entry) ImageIcon(javax.swing.ImageIcon) MagicCard(org.magic.api.beans.MagicCard) PageSize(com.itextpdf.kernel.geom.PageSize) TextAlignment(com.itextpdf.layout.properties.TextAlignment) Document(com.itextpdf.layout.Document) POMReader(org.magic.tools.POMReader) PdfFontFactory(com.itextpdf.kernel.font.PdfFontFactory) PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) PdfReader(com.itextpdf.kernel.pdf.PdfReader) IOException(java.io.IOException) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Date(java.util.Date) NotImplementedException(org.apache.commons.lang3.NotImplementedException) IOException(java.io.IOException) MagicCard(org.magic.api.beans.MagicCard) SimpleDateFormat(java.text.SimpleDateFormat)

Example 35 with PdfReader

use of com.itextpdf.kernel.pdf.PdfReader in project java-example by saxingz.

the class A0025PdfApplication method main.

public static void main(String[] args) throws IOException {
    byte[] user = "".getBytes();
    byte[] owner = "xyz".getBytes();
    // PdfDocument pdfDoc =
    // new PdfDocument(new PdfReader("D:\\D_desktop\\book\\big2.pdf"),
    // new PdfWriter("D:\\D_desktop\\book\\big4.pdf",
    // new WriterProperties()
    // .setStandardEncryption(user, owner,
    // EncryptionConstants.ALLOW_PRINTING | EncryptionConstants.ALLOW_COPY,
    // EncryptionConstants.ENCRYPTION_AES_256)));
    PdfDocument pdfDoc = new PdfDocument(new PdfReader(INPATH), new PdfWriter(OUTPATH));
    Document document = new Document(pdfDoc);
    Rectangle pageSize;
    PdfCanvas canvas;
    int totalPage = pdfDoc.getNumberOfPages();
    for (int i = 1; i <= totalPage; i++) {
        System.out.print("进度: " + i + " / " + totalPage + "\t");
        PdfPage page = pdfDoc.getPage(i);
        pageSize = page.getPageSize();
        canvas = new PdfCanvas(page);
        addText2(canvas, pageSize, totalPage, i, document, pdfDoc, page);
    }
    pdfDoc.close();
}
Also used : PdfWriter(com.itextpdf.kernel.pdf.PdfWriter) Rectangle(com.itextpdf.kernel.geom.Rectangle) PdfCanvas(com.itextpdf.kernel.pdf.canvas.PdfCanvas) PdfReader(com.itextpdf.kernel.pdf.PdfReader) PdfPage(com.itextpdf.kernel.pdf.PdfPage) PdfDocument(com.itextpdf.kernel.pdf.PdfDocument) Document(com.itextpdf.layout.Document) 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