private JpegToPdfParameters basicParameters(String... images) throws IOException { JpegToPdfParameters parameters = new JpegToPdfParameters(); for(String image: images) { parameters.addSource(customNonPdfInput("image/" + image)); } testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); return parameters; }
@Test public void imageWithoutExifMetadata() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); }
@Test public void discardsOutline() throws IOException { List<PdfSource<?>> inputs = new ArrayList<PdfSource<?>>(); inputs.add(customInput("pdf/large_outline.pdf")); inputs.add(customInput("pdf/test_outline.pdf")); setUpParameters(inputs); parameters.setOutlinePolicy(OutlinePolicy.DISCARD); parameters.addPage(0, 3); parameters.addPage(1, 3); parameters.addPage(0, 2); parameters.addPage(1, 2); parameters.addPage(0, 1); parameters.addPage(1, 1); testContext.pdfOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertHasOutline(false); }
private void setUpParameters() throws IOException { parameters = new SetPagesTransitionParameters(); parameters.setCompress(true); parameters.setVersion(PdfVersion.VERSION_1_6); parameters.putTransition(1, PdfPageTransition.newInstance(PdfPageTransitionStyle.BOX_OUTWARD, 1, 5)); parameters.setSource(shortInput()); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); testContext.pdfOutputTo(parameters); }
@Test public void withMargin() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.setMarginInches(0.5f); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertImageAtLocation(d, d.getPage(0), new Point(42, 13), 757, 567); }); }
@Test public void imageWithExifOrientation() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.addSource(customNonPdfInputAsFileSource("image/with_exif_orientation.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertEquals(d.getPage(0).getRotation(), 90); }); }
private void setUpParameters() throws IOException { parameters = new SetPagesLabelParameters(); parameters.setCompress(true); parameters.setVersion(PdfVersion.VERSION_1_6); parameters.putLabel(1, PdfPageLabel.newInstanceWithoutLabel(PdfLabelNumberingStyle.LOWERCASE_ROMANS, 1)); parameters.putLabel(3, PdfPageLabel.newInstanceWithLabel("Test", PdfLabelNumberingStyle.ARABIC, 1)); parameters.putLabel(20, PdfPageLabel.newInstanceWithLabel("OutOfRange", PdfLabelNumberingStyle.ARABIC, 1)); parameters.setSource(shortInput()); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); testContext.pdfOutputTo(parameters); }
@Test public void pageOrientationAuto() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertEquals(d.getPage(0).getMediaBox(), PDRectangle.A4.rotate()); }); }
@Test public void pageOrientationPortrait() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.setPageOrientation(PageOrientation.PORTRAIT); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertEquals(d.getPage(0).getMediaBox(), PDRectangle.A4); }); }
private void doExecute() throws IOException { testContext.pdfOutputTo(parameters); execute(parameters); PDDocument document = testContext.assertTaskCompleted(); testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_7); PDDocumentInformation info = document.getDocumentInformation(); assertEquals("test_author", info.getAuthor()); assertEquals("test_keywords", info.getKeywords()); assertEquals("test_subject", info.getSubject()); assertEquals("test_title", info.getTitle()); }
@Test public void pageOrientationLandscape() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.setPageOrientation(PageOrientation.LANDSCAPE); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { assertEquals(d.getPage(0).getMediaBox(), PDRectangle.A4.rotate()); }); }
@Test public void withStandardInput() throws IOException { AlternateMixMultipleInputParameters parameters = new AlternateMixMultipleInputParameters(); parameters.addInput(new PdfMixInput(shortInput())); parameters.addInput(new PdfMixInput(shortInput(), true, 3)); setUpParameters(parameters); testContext.pdfOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertPages(8).assertVersion(PdfVersion.VERSION_1_5); }
@Test public void testExecutePages() throws IOException { setUpParameters(); testContext.pdfOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertPages(1).forPdfOutput(d -> { COSBase names = d.getDocumentCatalog().getNames().getEmbeddedFiles().getCOSObject().getItem(COSName.NAMES); assertThat(names, instanceOf(COSArray.class)); assertEquals(6, ((COSArray) names).size()); assertNotNull(d.getDocumentCatalog().getCOSObject().getItem(COSName.getPDFName("Collection"))); }); }
@Test public void withEncryptedInput() throws IOException { AlternateMixMultipleInputParameters parameters = new AlternateMixMultipleInputParameters(); parameters.addInput(new PdfMixInput(encryptedInput())); parameters.addInput(new PdfMixInput(stronglyEncryptedInput(), true, 3)); setUpParameters(parameters); testContext.pdfOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertPages(8).assertVersion(PdfVersion.VERSION_1_5); }
@Test public void specificPageSize() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.setPageSize(PageSize.A0); parameters.addSource(customNonPdfInputAsFileSource("image/draft.tiff")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { for(PDPage page: d.getPages()){ assertEquals(page.getMediaBox().getWidth(), parameters.getPageSize().getWidth(), 0.0); assertEquals(page.getMediaBox().getHeight(), parameters.getPageSize().getHeight(), 0.0); } }); }
@Test public void threeDocsMerge() throws IOException { AlternateMixMultipleInputParameters params = new AlternateMixMultipleInputParameters(); params.addInput(new PdfMixInput(shortInput(), true, 1)); params.addInput(new PdfMixInput(stronglyEncryptedInput(), true, 3)); params.addInput(new PdfMixInput(largeOutlineInput())); setUpParameters(params); testContext.pdfOutputTo(params); execute(params); testContext.assertTaskCompleted(); testContext.assertCreator().assertPages(13).assertVersion(PdfVersion.VERSION_1_5).forPdfOutput(d -> { assertHeaderContains(d.getPage(0), "Pagina 4 di 4"); assertHeaderContains(d.getPage(5), "Pagina 3 di 4"); assertHeaderContains(d.getPage(8), "Pagina 2 di 4"); assertHeaderContains(d.getPage(10), "Pagina 1 di 4"); }); }
@Test public void pageSizeShouldMatchImageSize() throws Exception { JpegToPdfParameters parameters = new JpegToPdfParameters(); parameters.setPageSizeMatchImageSize(true); parameters.addSource(customNonPdfInputAsFileSource("image/draft.tiff")); parameters.addSource(customNonPdfInputAsFileSource("image/no_exif.JPG")); testContext.pdfOutputTo(parameters); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); execute(parameters); testContext.assertTaskCompleted(); testContext.forEachPdfOutput(d -> { PDPage p1 = d.getPage(0); assertEquals(p1.getMediaBox().getWidth(), 248.0, 0.0); assertEquals(p1.getMediaBox().getHeight(), 103.0, 0.0); PDPage p2 = d.getPage(1); assertEquals(p2.getMediaBox().getWidth(), 3264.0, 0.0); assertEquals(p2.getMediaBox().getHeight(), 2448.0, 0.0); }); }
@Test public void addingBlankPages() throws IOException { setUpParameters(basicInputs()); parameters.addPage(-1, -1); parameters.addPage(0, 1); parameters.addPage(1, 1); parameters.addPage(-1, -1); parameters.addPage(0, 3); testContext.pdfOutputTo(parameters); execute(parameters); PDDocument outDocument = testContext.assertTaskCompleted(); testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_6); assertPageHasText(outDocument, 1, ""); assertPageHasText(outDocument, 2, "1a"); assertPageHasText(outDocument, 3, "1b"); assertPageHasText(outDocument, 4, ""); assertPageHasText(outDocument, 5, "3a"); testContext.forEachPdfOutput(d -> { assertEquals(d.getPage(0).getMediaBox(), PDRectangle.A4); assertEquals(d.getPage(2).getMediaBox(), d.getPage(3).getMediaBox()); }); }
@Test public void threeDocsMergeWithPageSelection() throws IOException { AlternateMixMultipleInputParameters params = new AlternateMixMultipleInputParameters(); PdfMixInput first = new PdfMixInput(shortInput(), true, 1); first.addPageRange(new PageRange(1, 2)); params.addInput(first); PdfMixInput second = new PdfMixInput(stronglyEncryptedInput(), false, 2); params.addInput(second); PdfMixInput third = new PdfMixInput(largeInput(), false, 6); third.addPageRange(new PageRange(5, 10)); third.addPageRange(new PageRange(22, 23)); params.addInput(third); setUpParameters(params); testContext.pdfOutputTo(params); execute(params); testContext.assertTaskCompleted(); testContext.assertCreator().assertPages(14).assertVersion(PdfVersion.VERSION_1_5).forPdfOutput(d -> { assertHeaderContains(d.getPage(0), "Pagina 2 di 4"); assertHeaderContains(d.getPage(9), "Pagina 1 di 4"); }); }
@Test public void combineAndReorderWithRotation() throws IOException { setUpParameters(basicInputs()); parameters.addPage(0, 1); parameters.addPage(1, 1, Rotation.DEGREES_90); parameters.addPage(0, 2, Rotation.DEGREES_180); parameters.addPage(1, 2, Rotation.DEGREES_270); parameters.addPage(0, 3); parameters.addPage(1, 3, Rotation.DEGREES_90); testContext.pdfOutputTo(parameters); execute(parameters); PDDocument outDocument = testContext.assertTaskCompleted(); assertPageHasText(outDocument, 1, "1a"); assertPageHasText(outDocument, 2, "1b"); assertPageHasText(outDocument, 3, "2a"); assertPageHasText(outDocument, 4, "2b"); assertPageHasText(outDocument, 5, "3a"); assertPageHasText(outDocument, 6, "3b"); testContext.assertCreator().assertPages(6).forEachPdfOutput(d -> { assertEquals(90, d.getPage(1).getRotation()); assertEquals(180, d.getPage(2).getRotation()); assertEquals(270, d.getPage(3).getRotation()); assertEquals(90, d.getPage(5).getRotation()); }); }