private static void booklet(String input) throws Exception { String output = input.replace(".pdf", "-booklet.pdf"); PdfReader reader = new PdfReader(input); int n = reader.getNumberOfPages(); Rectangle pageSize = reader.getPageSize(1); System.out.println("Input page size: " + pageSize); Document doc = new Document(PageSize.A4.rotate(), 0, 0, 0, 0); PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(output)); doc.open(); splitLine(doc, writer); int[] pages = new int[(n + 3) / 4 * 4]; int x = 1, y = pages.length; for (int i = 0; i < pages.length; ) { pages[i++] = y--; pages[i++] = x++; pages[i++] = x++; pages[i++] = y--; } PdfContentByte cb = writer.getDirectContent(); float bottom = (doc.top() - pageSize.getHeight()) / 2 + kOffset; float left = doc.right() / 2 - (pageSize.getWidth() + kTextWidth) / 2 - kMargin; float right = doc.right() / 2 - (pageSize.getWidth() - kTextWidth) / 2 + kMargin; for (int i = 0; i < pages.length; ) { PdfImportedPage page = getPage(writer, reader, pages[i++]); if (page != null) cb.addTemplate(page, left, bottom); page = getPage(writer, reader, pages[i++]); if (page != null) cb.addTemplate(page, right, bottom); doc.newPage(); } doc.close(); }
private static void splitLine(Document doc, PdfWriter writer) { PdfContentByte cb = writer.getDirectContentUnder(); cb.moveTo(doc.right() / 2, doc.bottom()); cb.lineTo(doc.right() / 2, doc.top()); cb.stroke(); }
public static void createPdf2( List<Participant> participants, boolean exportName, boolean exportGroup, boolean exportRenseignement, OutputStream out) throws IOException, DocumentException { Document document = new Document(PageSize.A4.rotate()); // document.setMargins(0,0,0,0); PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); PdfContentByte cb = writer.getDirectContent(); float documentTop = document.top(); float documentBottom = document.bottom(); float documentHeight = documentTop - documentBottom; float left = document.left(); float right = document.right(); float width = right - left; cb.rectangle(left, documentBottom, width, documentHeight); cb.stroke(); float nameHeightPercent = 0.35f; float groupHeightPercent = 0.25f; float nameTop = documentTop; float nameBottom = nameTop; if (exportName) { nameBottom = nameTop - (documentHeight * nameHeightPercent); } float groupeTop = nameBottom; float groupeBottom = nameBottom; if (exportGroup) { groupeBottom = groupeTop - (documentHeight * groupHeightPercent); } float barcodeTop = groupeBottom; float barcodeBottom = documentBottom; ColumnText columnText; for (Participant participant : participants) { float nameFontSize = 65f; float groupFontSize = 45f; float renseignementFontSize = 35f; if (exportName) { columnText = new ColumnText(cb); columnText.setSimpleColumn(left, nameTop, right, nameBottom); // cb.rectangle(left, nameBottom, width, (nameTop - nameBottom)); // cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); String txt = participant.getNom().toUpperCase() + " " + participant.getPrenom(); float previousPos = columnText.getYLine(); columnText.setText(null); columnText.addElement(createCleanParagraph(txt, nameFontSize, true)); while (ColumnText.hasMoreText(columnText.go(true))) { nameFontSize = nameFontSize - 0.5f; columnText.setText(null); columnText.addElement(createCleanParagraph(txt, nameFontSize, true)); columnText.setYLine(previousPos); } columnText.setText(null); columnText.addElement(createCleanParagraph(txt, nameFontSize, true)); columnText.setYLine(previousPos); columnText.go(false); } if (exportGroup) { columnText = new ColumnText(cb); columnText.setSimpleColumn(document.left(), groupeTop, document.right(), groupeBottom); float groupeHeight = groupeTop - groupeBottom; // cb.rectangle(document.left(), groupeTop - groupeHeight, document.right() - // document.left(), groupeHeight); // cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); columnText.setFollowingIndent(0); String txt1 = participant.getGroupe(); String txt2 = exportRenseignement ? participant.getRenseignements() : null; float previousPos = columnText.getYLine(); columnText.setText(null); // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false)); columnText.addElement(createCleanParagraph(txt1, groupFontSize, true)); columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false)); while (ColumnText.hasMoreText(columnText.go(true))) { groupFontSize = groupFontSize - 0.5f; renseignementFontSize = renseignementFontSize - 0.5f; columnText.setText(null); // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false)); columnText.addElement(createCleanParagraph(txt1, groupFontSize, true)); columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); } columnText.setText(null); // columnText.addElement(createCleanParagraph(txt1,groupFontSize,true,txt2,renseignementFontSize,false)); columnText.addElement(createCleanParagraph(txt1, groupFontSize, true)); columnText.addElement(createCleanParagraph(txt2, renseignementFontSize, false)); columnText.setYLine(previousPos); columnText.go(false); } { columnText = new ColumnText(cb); barcodeTop = barcodeTop - 12f; columnText.setSimpleColumn(left, barcodeTop, right, barcodeBottom); float barcodeHeight = barcodeTop - barcodeBottom; // cb.rectangle(left, barcodeTop - barcodeHeight, width, barcodeHeight); // cb.stroke(); columnText.setExtraParagraphSpace(0f); columnText.setAdjustFirstLine(false); columnText.setIndent(0); float previousPos = columnText.getYLine(); columnText.setText(null); columnText.addElement( createCleanBarcode(cb, participant.getNumero(), width, barcodeHeight)); columnText.go(false); } document.newPage(); } document.close(); }