/** * <a href="http://stackoverflow.com/questions/28991291/how-to-remove-whitespace-on-merge"> * How To Remove Whitespace on Merge * </a> * <p> * Testing {@link PdfVeryDenseMergeTool} using the OP's files on a even smaller page. * </p> */ @Test public void testMergeGrandizerFilesA5() throws DocumentException, IOException { try ( InputStream docA = getClass().getResourceAsStream("Header.pdf"); InputStream docB = getClass().getResourceAsStream("Body.pdf"); InputStream docC = getClass().getResourceAsStream("Footer.pdf"); ) { PdfVeryDenseMergeTool tool = new PdfVeryDenseMergeTool(new RectangleReadOnly(595,421), 18, 18, 5); PdfReader readerA = new PdfReader(docA); PdfReader readerB = new PdfReader(docB); PdfReader readerC = new PdfReader(docC); try (FileOutputStream fos = new FileOutputStream(new File(RESULT_FOLDER, "GrandizerMerge-veryDense-A5.pdf"))) { List<PdfReader> inputs = Arrays.asList(readerA, readerB, readerC); tool.merge(fos, inputs); } finally { readerA.close(); readerB.close(); readerC.close(); } } }
public String savePageAsPdf(boolean scaled) throws IOException, DocumentException { String pdfName = ""; // Define test screenshot root String test = ""; if (TestNamingUtil.isTestNameRegistered()) { test = TestNamingUtil.getTestNameByThread(); } else { test = "undefined"; } File testRootDir = ReportContext.getTestDir(); File artifactsFolder = ReportContext.getArtifactsFolder(); String fileID = test.replaceAll("\\W+", "_") + "-" + System.currentTimeMillis(); pdfName = fileID + ".pdf"; String fullPdfPath = artifactsFolder.getAbsolutePath() + "/" + pdfName; // TODO: test this implementation and change back to capture if necessary Image image = Image.getInstance(testRootDir.getAbsolutePath() + "/" + Screenshot.captureFailure(driver, "")); Document document = null; if (scaled) { document = new Document(PageSize.A4, 10, 10, 10, 10); if (image.getHeight() > (document.getPageSize().getHeight() - 20) || image.getScaledWidth() > (document.getPageSize().getWidth() - 20)) { image.scaleToFit(document.getPageSize().getWidth() - 20, document.getPageSize().getHeight() - 20); } } else { document = new Document(new RectangleReadOnly(image.getScaledWidth(), image.getScaledHeight())); } PdfWriter.getInstance(document, new FileOutputStream(fullPdfPath)); document.open(); document.add(image); document.close(); return fullPdfPath; }
byte[] createSampleDocument() throws IOException, DocumentException { try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) { Document doc = new Document(new RectangleReadOnly(842,595)); PdfWriter.getInstance(doc, baos); doc.open(); doc.add(new Paragraph("Test Page 1")); doc.newPage(); doc.add(new Paragraph("Test Page 2")); doc.close(); return baos.toByteArray(); } }