List of usage examples for com.lowagie.text PageSize A4
Rectangle A4
To view the source code for com.lowagie.text PageSize A4.
Click Source Link
From source file:org.posterita.core.TabularReport2.java
License:Open Source License
public String createPDFReport() throws DocumentException, FileNotFoundException { String filename = RandomStringGenerator.randomstring() + ".pdf"; String filepath = ReportManager.getReportPath(filename); FileOutputStream fos = new FileOutputStream(filepath); Document document = new Document(PageSize.A4.rotate()); PdfWriter pdfWriter = PdfWriter.getInstance(document, fos); document.open();/*w w w . ja v a 2 s .co m*/ Font style1 = new Font(Font.HELVETICA, 16.0f, Font.BOLD); Font style2 = new Font(Font.HELVETICA, 14.0f); Font style3 = new Font(Font.HELVETICA, 10.0f); //write content Paragraph p = new Paragraph(new Chunk(title, style1)); p.setAlignment(Element.ALIGN_CENTER); document.add(p); p = new Paragraph(new Chunk(subtitle, style2)); p.setAlignment(Element.ALIGN_CENTER); document.add(p); p = new Paragraph("\n\n"); document.add(p); Object[] headers = reportData.get(0); PdfPTable table = new PdfPTable(headers.length); table.setWidthPercentage(100.0f); //int[] widths = new int[]{1,2,1,4}; //table.setWidths(widths); for (Object[] row : reportData) { for (Object data : row) { PdfPCell cell = new PdfPCell(new Phrase(data.toString(), style3)); table.addCell(cell); } } document.add(table); document.close(); return filename; }
From source file:org.posterita.core.TabularReportMerge.java
License:Open Source License
public byte[] getPDFData() throws DocumentException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(document, bos); document.open();//ww w .j a v a2 s.c om // Font Syle Font style1 = new Font(Font.TIMES_ROMAN, 12.0f, Font.BOLD); Font style2 = new Font(Font.TIMES_ROMAN, 8.0f, Font.BOLD); Font style3 = new Font(Font.TIMES_ROMAN, 8.0f); Font style4 = new Font(Font.TIMES_ROMAN, 9.0f, Font.BOLD); float[] widths = { 2f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f }; // Table header PdfPTable mytable = new PdfPTable(7); mytable.setTotalWidth(widths); mytable.setWidthPercentage(100f); PdfPCell header = new PdfPCell( new Paragraph(new Chunk(title, style1) + "\n" + new Chunk(subtitle, style2) + "\n", style1)); header.setColspan(7); header.setHorizontalAlignment(Element.ALIGN_CENTER); header.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); mytable.addCell(header); // Table sub header PdfPCell subheader = new PdfPCell(); subheader = new PdfPCell(new Paragraph("")); mytable.addCell(subheader); subheader = new PdfPCell(new Paragraph("QUANTITY", style2)); subheader.setColspan(2); subheader.setHorizontalAlignment(Element.ALIGN_CENTER); mytable.addCell(subheader); subheader = new PdfPCell(new Paragraph("VALUE (Excl. VAT)", style2)); subheader.setColspan(2); subheader.setHorizontalAlignment(Element.ALIGN_CENTER); mytable.addCell(subheader); subheader = new PdfPCell(new Paragraph("DISCREPANCY", style2)); subheader.setColspan(2); subheader.setHorizontalAlignment(Element.ALIGN_CENTER); mytable.addCell(subheader); // Rows Header PdfPCell rowHeader = new PdfPCell(new Paragraph("PRODUCT NAME", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("BOOK", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("COUNT", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("BOOK", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("COUNT", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("QTY", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("VALUE", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); document.add(mytable); Object[] headers = reportData.get(0); PdfPTable table = new PdfPTable(headers.length); table.setTotalWidth(widths); table.setWidthPercentage(100f); for (Object[] row : reportData) { for (Object data : row) { PdfPCell dataCell = new PdfPCell(new Phrase(data.toString(), style3)); table.addCell(dataCell); } } document.add(table); document.close(); return bos.toByteArray(); }
From source file:org.posterita.core.TabularReportMerge.java
License:Open Source License
/** * Get Selling Items Data//from ww w . j a v a 2s . co m * @return * @throws DocumentException * @throws OperationException */ public byte[] getSellingItemsData() throws DocumentException, OperationException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(document, bos); document.open(); // Font Syle Font style1 = new Font(Font.TIMES_ROMAN, 12.0f, Font.BOLD); Font style2 = new Font(Font.TIMES_ROMAN, 8.0f, Font.BOLD); Font style3 = new Font(Font.TIMES_ROMAN, 8.0f); Font style4 = new Font(Font.TIMES_ROMAN, 9.0f, Font.BOLD); Font style5 = new Font(Font.TIMES_ROMAN, 6.0f); float[] widths = { 2f, 0.5f, 0.5f, 1.0f }; // Table header PdfPTable mytable = new PdfPTable(4); mytable.setTotalWidth(widths); mytable.setWidthPercentage(100f); PdfPCell header = new PdfPCell( new Paragraph(new Chunk(title, style1) + "\n" + new Chunk(subtitle, style2) + "\n", style1)); header.setColspan(4); header.setHorizontalAlignment(Element.ALIGN_CENTER); header.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); mytable.addCell(header); //Date Header PdfPCell dateHeader = new PdfPCell(new Paragraph("From : " + new Chunk( TimestampConvertor.convertTimeStamp(fromDate, TimestampConvertor.DEFAULT_DATE_PATTERN1), style1), style1)); dateHeader.setColspan(1); dateHeader.setHorizontalAlignment(Element.ALIGN_LEFT); dateHeader.setBackgroundColor(Color.GRAY); dateHeader.setBorderColor(Color.WHITE); mytable.addCell(dateHeader); dateHeader = new PdfPCell(new Paragraph("To : " + new Chunk( TimestampConvertor.convertTimeStamp(toDate, TimestampConvertor.DEFAULT_DATE_PATTERN1), style1), style1)); dateHeader.setColspan(3); dateHeader.setHorizontalAlignment(Element.ALIGN_LEFT); dateHeader.setBackgroundColor(Color.GRAY); dateHeader.setBorderColor(Color.WHITE); mytable.addCell(dateHeader); // Info Header PdfPCell infoHeader = new PdfPCell( new Paragraph("ALL VALUES ARE EXCLUSIVE OF VAT - SALES FIGURES ARE TAKEN AFTER DISCOUNT", style5)); infoHeader.setColspan(4); infoHeader.setHorizontalAlignment(Element.ALIGN_CENTER); infoHeader.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); mytable.addCell(infoHeader); // Rows Header PdfPCell rowHeader = new PdfPCell(new Paragraph("PRODUCT NAME", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("QTY", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("VALUE", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("SUPPLIER", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); document.add(mytable); Object[] headers = reportData.get(0); PdfPTable table = new PdfPTable(headers.length); table.setTotalWidth(widths); table.setWidthPercentage(100f); for (Object[] row : reportData) { for (Object data : row) { PdfPCell dataCell = new PdfPCell(new Phrase(data.toString(), style3)); table.addCell(dataCell); } } document.add(table); document.close(); return bos.toByteArray(); }
From source file:org.posterita.core.TabularReportMerge.java
License:Open Source License
/** * Get Stock Enquiry Report Data//from w w w . j a va 2 s.c o m * @return * @throws DocumentException * @throws OperationException */ public byte[] getStockEnquiryData() throws DocumentException, OperationException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); Document document = new Document(PageSize.A4); PdfWriter pdfWriter = PdfWriter.getInstance(document, bos); document.open(); // Font Syle Font style1 = new Font(Font.TIMES_ROMAN, 12.0f, Font.BOLD); Font style2 = new Font(Font.TIMES_ROMAN, 8.0f, Font.BOLD); Font style3 = new Font(Font.TIMES_ROMAN, 8.0f); Font style4 = new Font(Font.TIMES_ROMAN, 7.0f, Font.BOLD); Font style5 = new Font(Font.TIMES_ROMAN, 6.0f); float[] widths = { 1f, 2f, 0.5f, 0.5f, 0.5f, 0.5f }; // Table header PdfPTable mytable = new PdfPTable(6); mytable.setTotalWidth(widths); mytable.setWidthPercentage(100f); PdfPCell header = new PdfPCell( new Paragraph(new Chunk(title, style1) + "\n" + new Chunk(subtitle, style2) + "\n", style1)); header.setColspan(6); header.setHorizontalAlignment(Element.ALIGN_CENTER); header.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); mytable.addCell(header); //Date Header PdfPCell dateHeader = new PdfPCell(new Paragraph("From : " + new Chunk( TimestampConvertor.convertTimeStamp(fromDate, TimestampConvertor.DEFAULT_DATE_PATTERN1), style1), style1)); dateHeader.setColspan(2); dateHeader.setHorizontalAlignment(Element.ALIGN_LEFT); dateHeader.setBackgroundColor(Color.GRAY); dateHeader.setBorderColor(Color.WHITE); mytable.addCell(dateHeader); dateHeader = new PdfPCell(new Paragraph("To : " + new Chunk( TimestampConvertor.convertTimeStamp(toDate, TimestampConvertor.DEFAULT_DATE_PATTERN1), style1), style1)); dateHeader.setColspan(4); dateHeader.setHorizontalAlignment(Element.ALIGN_LEFT); dateHeader.setBackgroundColor(Color.GRAY); dateHeader.setBorderColor(Color.WHITE); mytable.addCell(dateHeader); // Info Header PdfPCell infoHeader = new PdfPCell( new Paragraph("ALL VALUES ARE EXCLUSIVE OF VAT & BASED ON PURCHASE PRICES", style5)); infoHeader.setColspan(6); infoHeader.setHorizontalAlignment(Element.ALIGN_CENTER); infoHeader.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0)); mytable.addCell(infoHeader); // Rows Header PdfPCell rowHeader = new PdfPCell(new Paragraph("BARCODE", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("PRODUCT NAME", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("Opening Stock Qty", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("Opening Stock Value", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("Closing Stock Qty", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); rowHeader = new PdfPCell(new Paragraph("Closing Stock Value", style4)); rowHeader.setHorizontalAlignment(Element.ALIGN_LEFT); mytable.addCell(rowHeader); document.add(mytable); Object[] headers = reportData.get(0); PdfPTable table = new PdfPTable(headers.length); table.setTotalWidth(widths); table.setWidthPercentage(100f); for (Object[] row : reportData) { for (Object data : row) { PdfPCell dataCell = new PdfPCell(new Phrase(data.toString(), style3)); table.addCell(dataCell); } } document.add(table); document.close(); return bos.toByteArray(); }
From source file:org.primefaces.showcase.view.data.dataexporter.CustomizedDocumentsView.java
License:Open Source License
public void preProcessPDF(Object document) throws IOException, BadElementException, DocumentException { Document pdf = (Document) document; pdf.open();/*from w w w . ja v a2 s .c o m*/ pdf.setPageSize(PageSize.A4); ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); String logo = externalContext.getRealPath("") + File.separator + "resources" + File.separator + "demo" + File.separator + "images" + File.separator + "prime_logo.png"; pdf.add(Image.getInstance(logo)); }
From source file:org.revager.export.PDFExporter.java
License:Open Source License
/** * Instantiates a new PDF exporter.//from w w w . j a va2 s.c om * * @param filePath * the file path * @param headTitle * the head title * @param headLogoPath * the head logo path * @param footText * the foot text * * @throws ExportException * If an error occurs while instantiating the PDF exporter class */ protected PDFExporter(String filePath, String headTitle, String headLogoPath, String footText) throws ExportException { pdfDoc = new Document(PageSize.A4, PDFTools.cmToPt(2), PDFTools.cmToPt(2), PDFTools.cmToPt(3.5f), PDFTools.cmToPt(3.2f)); try { pdfWriter = PdfWriter.getInstance(pdfDoc, new FileOutputStream(filePath)); } catch (Exception e) { /* * Not part of unit testing because this exception is only thrown if * an internal error occurs. */ throw new ExportException(translate( "Cannot store PDF file. Either you don't have write permissions or the PDF file cannot be created.")); } pdfWriter.setPageEvent(new PDFPageEventHelper(headTitle, headLogoPath, footText)); pdfDoc.addAuthor(Data.getInstance().getResource("appName")); pdfDoc.addCreator(Data.getInstance().getResource("appName")); pdfDoc.addCreationDate(); pdfDoc.addTitle(headTitle); pdfDoc.addSubject(headTitle); Rectangle page = pdfDoc.getPageSize(); pageWidth = page.getWidth() - pdfDoc.leftMargin() - pdfDoc.rightMargin(); pageHeight = page.getHeight() - pdfDoc.topMargin() - pdfDoc.bottomMargin(); }
From source file:org.schreibubi.JCombinations.ui.GridChartPanel.java
License:Open Source License
/** * Create PDF/*from ww w . j av a 2s . com*/ * * @param name * Filename * @param selection * Selected Nodes * @param dm * DataModel * @param pl * ProgressListener */ @SuppressWarnings("unchecked") public static void generatePDF(File name, DataModel dm, ArrayList<TreePath> selection, ProgressListener pl) { com.lowagie.text.Document document = new Document(PageSize.A4.rotate(), 50, 50, 50, 50); try { ArrayList<ExtendedJFreeChart> charts = dm.getCharts(selection); if (charts.size() > 0) { PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(name)); writer.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.addAuthor("Jrg Werner"); document.addSubject("Created by JCombinations"); document.addKeywords("JCombinations"); document.addCreator("JCombinations using iText"); // we define a header and a footer HeaderFooter header = new HeaderFooter(new Phrase("JCombinations by Jrg Werner"), false); HeaderFooter footer = new HeaderFooter(new Phrase("Page "), new Phrase(".")); footer.setAlignment(Element.ALIGN_CENTER); document.setHeader(header); document.setFooter(footer); document.open(); DefaultFontMapper mapper = new DefaultFontMapper(); FontFactory.registerDirectories(); mapper.insertDirectory("c:\\WINNT\\fonts"); PdfContentByte cb = writer.getDirectContent(); pl.progressStarted(new ProgressEvent(GridChartPanel.class, 0, charts.size())); for (int i = 0; i < charts.size(); i++) { ExtendedJFreeChart chart = charts.get(i); PdfTemplate tp = cb.createTemplate(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN)); Graphics2D g2d = tp.createGraphics(document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN), mapper); Rectangle2D r2d = new Rectangle2D.Double(0, 0, document.right(EXTRA_MARGIN) - document.left(EXTRA_MARGIN), document.top(EXTRA_MARGIN) - document.bottom(EXTRA_MARGIN)); chart.draw(g2d, r2d); g2d.dispose(); cb.addTemplate(tp, document.left(EXTRA_MARGIN), document.bottom(EXTRA_MARGIN)); PdfDestination destination = new PdfDestination(PdfDestination.FIT); TreePath treePath = chart.getTreePath(); PdfOutline po = cb.getRootOutline(); for (int j = 0; j < treePath.getPathCount(); j++) { ArrayList<PdfOutline> lpo = po.getKids(); PdfOutline cpo = null; for (PdfOutline outline : lpo) if (outline.getTitle().compareTo(treePath.getPathComponent(j).toString()) == 0) cpo = outline; if (cpo == null) cpo = new PdfOutline(po, destination, treePath.getPathComponent(j).toString()); po = cpo; } document.newPage(); pl.progressIncremented(new ProgressEvent(GridChartPanel.class, i)); } document.close(); pl.progressEnded(new ProgressEvent(GridChartPanel.class)); } } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } }
From source file:org.signserver.client.cli.performance.PerformanceTestPDFServlet.java
License:Open Source License
/** * Creates a new PDF document by adding the same paragraph over and over. * @param requestSize is the requested size of the PDF in bytes *///w w w. jav a2 s . c om private byte[] createTestPDF(int requestSize) throws Exception { // Create a sample PDF-file ByteArrayOutputStream baos = new ByteArrayOutputStream(); Document pdfDocument = new Document(PageSize.A4, 50, 50, 50, 50); PdfWriter.getInstance(pdfDocument, baos); pdfDocument.open(); pdfDocument.add(new Paragraph(PDF_CONTENT)); final String DUMMYTEXT = "qwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxcvbnmqwertyuiopasdfghjklzxc"; int maxIteration = requestSize / 20; for (int i = 0; i < maxIteration; i++) { pdfDocument.add(new Paragraph(DUMMYTEXT)); } pdfDocument.close(); baos.flush(); System.out.println("Created new PDF-document of " + baos.toByteArray().length + " bytes."); return baos.toByteArray(); }
From source file:org.silverpeas.components.almanach.control.AlmanachPdfGenerator.java
License:Open Source License
static public void buildPdf(String name, AlmanachSessionController almanach, String mode) throws AlmanachRuntimeException { try {//from ww w. ja v a 2 s.c o m String fileName = FileRepositoryManager.getTemporaryPath(almanach.getSpaceId(), almanach.getComponentId()) + name; Document document = new Document(PageSize.A4, 50, 50, 50, 50); // we add some meta information to the document document.addAuthor(almanach.getSettings().getString("author", "")); document.addSubject(almanach.getSettings().getString("subject", "")); document.addCreationDate(); PdfWriter.getInstance(document, new FileOutputStream(fileName)); document.open(); try { Calendar currentDay = Calendar.getInstance(); currentDay.setTime(almanach.getCurrentDay()); String sHeader = almanach.getString("events"); if (mode.equals(PDF_MONTH_ALLDAYS) || mode.equals(PDF_MONTH_EVENTSONLY)) { sHeader += " " + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH)); } sHeader += " " + currentDay.get(Calendar.YEAR); HeaderFooter header = new HeaderFooter(new Phrase(sHeader), false); HeaderFooter footer = new HeaderFooter(new Phrase("Page "), true); footer.setAlignment(Element.ALIGN_CENTER); document.setHeader(header); document.setFooter(footer); createFirstPage(almanach, document); document.newPage(); Font titleFont = new Font(Font.HELVETICA, 24, Font.NORMAL, new Color(255, 255, 255)); Paragraph cTitle = new Paragraph(almanach.getString("Almanach") + " " + almanach.getString("GML.mois" + currentDay.get(Calendar.MONTH)) + " " + currentDay.get(Calendar.YEAR), titleFont); Chapter chapter = new Chapter(cTitle, 1); // Collection<EventDetail> events = // almanach.getListRecurrentEvent(mode.equals(PDF_YEAR_EVENTSONLY)); AlmanachCalendarView almanachView; if (PDF_YEAR_EVENTSONLY.equals(mode)) { almanachView = almanach.getYearlyAlmanachCalendarView(); } else { almanachView = almanach.getMonthlyAlmanachCalendarView(); } List<DisplayableEventOccurrence> occurrences = almanachView.getEvents(); generateAlmanach(chapter, almanach, occurrences, mode); document.add(chapter); } catch (Exception ex) { throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING, "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", ex); } document.close(); } catch (Exception e) { throw new AlmanachRuntimeException("PdfGenerator.generate", AlmanachRuntimeException.WARNING, "AlmanachRuntimeException.EX_PROBLEM_TO_GENERATE_PDF", e); } }
From source file:org.sonar.report.pdf.PDFReporter.java
License:Open Source License
public ByteArrayOutputStream getReport() throws DocumentException, IOException, org.dom4j.DocumentException, ReportException { // Creation of documents Document mainDocument = new Document(PageSize.A4, 50, 50, 110, 50); Toc tocDocument = new Toc(); Document frontPageDocument = new Document(PageSize.A4, 50, 50, 110, 50); ByteArrayOutputStream mainDocumentBaos = new ByteArrayOutputStream(); ByteArrayOutputStream frontPageDocumentBaos = new ByteArrayOutputStream(); PdfWriter mainDocumentWriter = PdfWriter.getInstance(mainDocument, mainDocumentBaos); PdfWriter frontPageDocumentWriter = PdfWriter.getInstance(frontPageDocument, frontPageDocumentBaos); // Events for TOC, header and pages numbers Events events = new Events(tocDocument, new Header(this.getLogo(), this.getProject())); mainDocumentWriter.setPageEvent(events); mainDocument.open();/*ww w .j a va2s. com*/ tocDocument.getTocDocument().open(); frontPageDocument.open(); Logger.info("Generating PDF report..."); printFrontPage(frontPageDocument, frontPageDocumentWriter); printTocTitle(tocDocument); printPdfBody(mainDocument); mainDocument.close(); tocDocument.getTocDocument().close(); frontPageDocument.close(); // Get Readers PdfReader mainDocumentReader = new PdfReader(mainDocumentBaos.toByteArray()); PdfReader tocDocumentReader = new PdfReader(tocDocument.getTocOutputStream().toByteArray()); PdfReader frontPageDocumentReader = new PdfReader(frontPageDocumentBaos.toByteArray()); // New document Document documentWithToc = new Document(tocDocumentReader.getPageSizeWithRotation(1)); ByteArrayOutputStream finalBaos = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(documentWithToc, finalBaos); documentWithToc.open(); copy.addPage(copy.getImportedPage(frontPageDocumentReader, 1)); for (int i = 1; i <= tocDocumentReader.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(tocDocumentReader, i)); } for (int i = 1; i <= mainDocumentReader.getNumberOfPages(); i++) { copy.addPage(copy.getImportedPage(mainDocumentReader, i)); } documentWithToc.close(); // Return the final document (with TOC) return finalBaos; }