List of usage examples for com.lowagie.text.pdf PdfCopy PdfCopy
public PdfCopy(Document document, OutputStream os) throws DocumentException
From source file:questions.importpages.ConcatenateWithTOC.java
public static void main(String[] args) { try {//from w w w . j a v a 2 s . com // suppose we have some TEST PDF files for (int i = 0; i < TEST.length; i++) { createTestPdf(i); } // and we want to concatenate them Document document = new Document(); PdfCopy copy = new PdfCopy(document, new FileOutputStream(RESULT)); copy.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.open(); // but we want to create an outline tree PdfOutline root = copy.getRootOutline(); // we also want an extra page with the file name Document coverpage; ByteArrayOutputStream baos; PdfReader reader; // we want to add page numbers too int pagenumber = 0; BaseFont bf = BaseFont.createFont(); for (int i = 0; i < TEST.length; i++) { // we create the coverpage in memory coverpage = new Document(); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(coverpage, baos); coverpage.open(); coverpage.add(new Paragraph("file: " + TEST[i])); coverpage.close(); // we import that page reader = new PdfReader(baos.toByteArray()); pagenumber++; copy.addPage(getStampedPage(reader, copy, 1, pagenumber, bf)); // we create the bookmark to that page PdfDestination dest = new PdfDestination(PdfDestination.FIT); new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]); // we import the document itself reader = new PdfReader(TEST[i]); for (int j = 1; j <= reader.getNumberOfPages(); j++) { pagenumber++; copy.addPage(getStampedPage(reader, copy, j, pagenumber, bf)); } } document.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (DocumentException de) { de.printStackTrace(); } }
From source file:questions.importpages.ConcatenateWithTOC2.java
public static void main(String[] args) { try {//w w w . ja v a2s. c om // suppose we have some TEST PDF files for (int i = 0; i < TEST.length; i++) { createTestPdf(i); } // and we want to concatenate them Document document = new Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, os); copy.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.open(); // but we want to create an outline tree PdfOutline root = copy.getRootOutline(); // we also want an extra page with the file name Document coverpage; ByteArrayOutputStream baos; PdfReader reader; // we want keep track of the page numbers int pagenumber = 0; for (int i = 0; i < TEST.length; i++) { // we create the coverpage in memory coverpage = new Document(); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(coverpage, baos); coverpage.open(); coverpage.add(new Paragraph("file: " + TEST[i])); coverpage.close(); // we import that page reader = new PdfReader(baos.toByteArray()); pagenumber++; copy.addPage(copy.getImportedPage(reader, 1)); // we create the bookmark to that page PdfDestination dest = new PdfDestination(PdfDestination.FIT); new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]); // we import the document itself reader = new PdfReader(TEST[i]); for (int j = 1; j <= reader.getNumberOfPages(); j++) { pagenumber++; copy.addPage(copy.getImportedPage(reader, j)); } } document.close(); // we want to add page X of Y reader = new PdfReader(os.toByteArray()); int n = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); BaseFont bf = BaseFont.createFont(); for (int i = 1; i <= n; i++) { PdfContentByte canvas = stamper.getOverContent(i); canvas.beginText(); canvas.setFontAndSize(bf, 12); canvas.showTextAligned(Element.ALIGN_LEFT, "page " + i + " of " + n, 36, 26, 0); canvas.endText(); } stamper.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (DocumentException de) { de.printStackTrace(); } }