@Test public void extractOddPages() throws IOException { setUpParametersOddPages(); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_6).assertPages(2); }
@Test public void formIsStripped() throws IOException { setUpParametersWithForms(); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_6).assertOutputSize(1).assertHasAcroforms(false); }
@Test public void outlineIsStripped() throws IOException { setUpParametersWithOutline(); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertCreator().assertVersion(PdfVersion.VERSION_1_6).assertOutputSize(1).assertHasOutline(false); }
@Test public void testExecuteBurst() throws IOException { setUpParameters(PredefinedSetOfPages.ALL_PAGES); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(4); }
@Test public void testIncludingPageAfterOff() throws IOException { ExtractByOutlineParameters parameters = setUpParameters(1, "pdf/payslip_with_bookmarks.pdf", null); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertOutputContainsFilenames("1_Employee One.pdf", "3_Employee Three.pdf", "2_Employee Two.pdf"); testContext.assertOutputSize(3); testContext.assertPages("1_Employee One.pdf", 1); }
@Test public void backPagesMultipleEncryptedInput() throws IOException { setUpParametersMultipleEncryptedInput(); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(2).forEachPdfOutput(d -> assertEquals(10, d.getNumberOfPages())); } }
void doExecute(AbstractPdfToMultipleImageParameters parameters, int size) throws IOException { testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(size).forEachRawOutput(p -> { try { RenderedImage ri = ImageIO.read(p.toFile()); assertTrue(ri.getHeight() > 0); assertTrue(ri.getWidth() > 0); } catch (Exception e) { LOG.error("Test failed", e); fail(e.getMessage()); } }); } }
@Test public void testExecuteLevel2() throws IOException { SplitByOutlineLevelParameters parameters = setUpParameters(2, null); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(3).forEachPdfOutput(d -> { assertTrue(nonNull(d.getDocumentCatalog().getDocumentOutline())); }); }
public void doTestEven() throws IOException { parameters.addPage(2); parameters.addPage(4); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(2); }
@Test public void batchMode() throws IOException { setUpParameters(PredefinedSetOfPages.EVEN_PAGES); parameters.addSource(shortInput()); parameters.addSource(mediumInput()); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(19); testContext.assertOutputContainsFilenames("1_short-test-file.pdf", "3_short-test-file.pdf", "1_medium-test-file.pdf", "3_medium-test-file.pdf", "5_medium-test-file.pdf", "7_medium-test-file.pdf", "9_medium-test-file.pdf", "11_medium-test-file.pdf", "13_medium-test-file.pdf", "15_medium-test-file.pdf", "17_medium-test-file.pdf", "19_medium-test-file.pdf", "21_medium-test-file.pdf", "23_medium-test-file.pdf", "25_medium-test-file.pdf", "27_medium-test-file.pdf", "29_medium-test-file.pdf", "31_medium-test-file.pdf", "33_medium-test-file.pdf" ); }
@Test public void testExcelConversion() throws IOException { PdfToExcelParameters params = getParams(); execute(params); testContext.assertTaskCompleted(); testContext.assertOutputSize(1).assertOutputContainsFilenames("tabular-data.xlsx").forEachRawOutput(p -> { try { InputStream in = new FileInputStream(p.toFile()); Workbook wb = WorkbookFactory.create(in); assertThat(wb.getNumberOfSheets(), is(2)); Sheet sheet = wb.getSheetAt(0); assertThat(sheet.getPhysicalNumberOfRows(), is(37)); assertThat(sheet.getSheetName(), is("Table 1 (Page 1)")); assertThat(getDataRow(sheet, 0), is(Arrays.asList("OrderDate", "Region", "Rep", "Item", "Units", "Unit Cost", "Total"))); assertThat(getDataRow(sheet, 10), is(Arrays.asList("6/8/15", "East", "Jones", "Binder", "60", "8.99", "539.40"))); assertThat(getDataRow(sheet, 13), is(Arrays.asList("7/29/15", "East", "Parent", "Binder", "81", "19.99", "1,619.19"))); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test public void testPageRotation180() throws Exception { WatermarkParameters parameters = new WatermarkParameters(customNonPdfInput("image/draft.png")); setUpParams(parameters, customInput("pdf/rotation_180_test_file.pdf")); parameters.setDimension(new Dimension(200, 83)); execute(parameters); testContext.assertTaskCompleted(); testContext.forPdfOutput(d -> { assertImageAtLocation(d, d.getPage(0), new Rectangle(585, 792, -200, -83)); }); }
@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()); }); }
@Test public void testCsvConversion() throws IOException { PdfToExcelParameters params = getParams(); params.setOutputPrefix("[BASENAME]_[FILENUMBER]"); params.setCsvFormat(true); execute(params); testContext.assertTaskCompleted(); testContext.assertOutputSize(2) .assertOutputContainsFilenames("tabular-data_1.csv", "tabular-data_2.csv") .forRawOutput("tabular-data_1.csv", p -> { try { List<List<String>> contents = parseCsv(p.toFile()); assertThat(contents.size(), is(37)); assertThat(contents.get(0), is(Arrays.asList("OrderDate", "Region", "Rep", "Item", "Units", "Unit Cost", "Total"))); assertThat(contents.get(10), is(Arrays.asList("6/8/15", "East", "Jones", "Binder", "60", "8.99", "539.40"))); assertThat(contents.get(13), is(Arrays.asList("7/29/15", "East", "Parent", "Binder", "81", "19.99", "1,619.19"))); } catch (Exception e) { throw new RuntimeException(e); } }); }
@Test public void testPageScale() throws IOException { parameters = new ScaleParameters(0.6); setUpParameters(); parameters.setScaleType(ScaleType.PAGE); execute(parameters); testContext.assertTaskCompleted(); testContext.assertOutputSize(2).assertOutputContainsFilenames("1_test_file.pdf", "2_test_file.pdf") .forEachPdfOutput(d -> { PDPage page = d.getPage(0); PDRectangle expected = new PDRectangle(0f, 0f, 357f, 505.2f); assertEquals(expected, page.getMediaBox()); assertEquals(expected, page.getCropBox()); }); } }
@Test public void keepsOutline() 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.RETAIN); 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); PDDocument outDocument = testContext.assertTaskCompleted(); PDDocumentOutline outline = outDocument.getDocumentCatalog().getDocumentOutline(); assertNotNull("Has outline", outline); for(int i = 1; i < 34; i++) { testContext.assertOutlineContains("Bookmark" + i); } for(int i = 34; i <= 49; i++) { testContext.assertOutlineDoesntContain("Bookmark" + i); } testContext.assertOutlineContains("Test first level."); testContext.assertOutlineContains("Secondpage test first level."); }
@Test public void testSplitDeeperLevel() throws IOException { ExtractByOutlineParameters parameters = setUpParameters(3); testContext.directoryOutputTo(parameters); execute(parameters); testContext .assertOutputContainsFilenames("1_Creating Assemblies.pdf", "2_Using Profiles.pdf", "3_Using Profiles by OS.pdf", "4_Setting Source Code Control System.pdf", "5_Versioning.pdf", "6_Using internal Repositories.pdf", "7_Installing Artifact in Remote Repository.pdf", "8_Install 3rdParty jar to Remote Repository.pdf", "9_Preparing Releases.pdf", "10_Performing Releases.pdf", "11_IntegrationTest with tomcat.pdf", "12_Online webdevelopment with Jetty plugin.pdf", "13_Online webdevelopment and automatic deployment with tomcat plugin.pdf") .assertOutputSize(13); }
@Test public void testWithMatchingText() throws IOException { ExtractByOutlineParameters parameters = setUpParameters(3, "(Using)+.+"); parameters.setIncludePageAfter(true); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertOutputSize(3).assertPages("1_Using Profiles.pdf", 2) .assertPages("2_Using Profiles by OS.pdf", 2).assertPages("3_Using internal Repositories.pdf", 2); }
@Test public void extractWithForms() throws IOException { parameters = new ExtractPagesParameters(); parameters.addPageRange(new PageRange(1, 1)); parameters.setExistingOutputPolicy(ExistingOutputPolicy.OVERWRITE); parameters.setCompress(true); parameters.setVersion(PdfVersion.VERSION_1_6); parameters.addSource(formInput()); testContext.directoryOutputTo(parameters); execute(parameters); testContext.assertOutputSize(1); testContext.forEachPdfOutput(d -> { assertNotNull(d.getDocumentCatalog().getAcroForm()); }); } }
@Test public void resize_allLandscape() throws IOException { executeChangeOfPageSize(PageSize.A5, customInput("pdf/multiple-sized-pages-all-landscape.pdf")); // number of pages does not change testContext.assertPages(2).forEachPdfOutput(d -> { PDPage page = d.getPage(0); // page has new size // was A4 landscape, expect A5 landscape PDRectangle expected = PDRectangle.A5.rotate(); assertEqualsRect(expected, page.getMediaBox()); assertEqualsRect(expected, page.getCropBox()); page = d.getPage(1); // page has new size // was A3 landscape, expect A5 landscape assertEqualsRect(expected, page.getMediaBox()); assertEqualsRect(expected, page.getCropBox()); }); }