/** * Método que se ejecuta cuando se cierra el documento. * * @param writer Creador de documentos. * @param document Documento del informe. */ public void onCloseDocument(PdfWriter writer, Document document) { // Plantilla con el número total de páginas del documento tpl.beginText(); tpl.setFontAndSize(HELVETICA, 8); tpl.setTextMatrix(0, 0); tpl.showText("" + (writer.getPageNumber() - 1)); tpl.endText(); }
public void onCloseDocument(PdfWriter writer, Document document) { template.beginText(); template.setFontAndSize(arial, FONT_SIZE); template.showText(String.valueOf(writer.getPageNumber() - 1)); template.endText(); }
protected void handleDocument(Document document, PdfWriter writer, Map model) throws DocumentException { java.util.List sampleList = (java.util.List) model.get("list"); Integer scapeNo = (Integer) model.get("scapeNo"); Iterator ir = sampleList.iterator(); try { PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont("Helvetica", "winansi", false); int scapeN = scapeNo.intValue(); int i = scapeN; int pageI = i; while (ir.hasNext()) { Sample sample = (Sample) ir.next(); String internalId = sample.getPatient().getIntSampleId(); PdfTemplate template = cb.createTemplate(templateW, templateL); // USING ean8 BARCODE Barcode128 code = new Barcode128(); code.setCodeType(Barcode.CODE128); code.setX(0.75f); // log.debug("the x is " + codeEAN.getX()); code.setBarHeight(barcodeSize); code.setSize(0); // code.setCode(formatBarcode(sampleId)); code.setCode(internalId); Image imageBar = code.createImageWithBarcode(template, null, null); template.beginText(); template.setFontAndSize(bf, fontSize); template.moveText(x, y2); template.showText(internalId); template.endText(); template.addImage(imageBar, imageBar.width(), 0, 0, imageBar.height(), x, y1); int yNo = pageI / columnNo + 1; int xNo = pageI % columnNo; int templateX = marginX + (xNo * templateW); int templateY = totalPageY - marginY - (yNo * templateL); // log.debug("the i is "+i + " the adjustY is " + tempYAdjust + " the templateY is " + // templateY); cb.addTemplate(template, templateX, templateY); i++; pageI++; if ((i % (rowNo * columnNo)) == 0) { pageI = 0; // we go to a new page document.newPage(); } } document.close(); System.out.println("Finished."); } catch (Exception de) { de.printStackTrace(); } }
/** * PdfTemplates can be wrapped in an Image. * * @param args no arguments needed */ public static void main(String[] args) { System.out.println("PdfTemplate wrapped in an Image"); // step 1: creation of a document-object Rectangle rect = new Rectangle(PageSize.A4); rect.setBackgroundColor(new Color(238, 221, 88)); Document document = new Document(rect, 50, 50, 50, 50); try { // step 2: we create a writer that listens to the document PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("templateImages.pdf")); // step 3: we open the document document.open(); // step 4: PdfTemplate template = writer.getDirectContent().createTemplate(20, 20); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); String text = "Vertical"; float size = 16; float width = bf.getWidthPoint(text, size); template.beginText(); template.setRGBColorFillF(1, 1, 1); template.setFontAndSize(bf, size); template.setTextMatrix(0, 2); template.showText(text); template.endText(); template.setWidth(width); template.setHeight(size + 2); template.sanityCheck(); Image img = Image.getInstance(template); img.setRotationDegrees(90); Chunk ck = new Chunk(img, 0, 0); PdfPTable table = new PdfPTable(3); table.setWidthPercentage(100); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); table.getDefaultCell().setVerticalAlignment(Element.ALIGN_MIDDLE); PdfPCell cell = new PdfPCell(img); cell.setPadding(4); cell.setBackgroundColor(new Color(0, 0, 255)); cell.setHorizontalAlignment(Element.ALIGN_CENTER); table.addCell("I see a template on my right"); table.addCell(cell); table.addCell("I see a template on my left"); table.addCell(cell); table.addCell("I see a template everywhere"); table.addCell(cell); table.addCell("I see a template on my right"); table.addCell(cell); table.addCell("I see a template on my left"); Paragraph p1 = new Paragraph("This is a template "); p1.add(ck); p1.add(" just here."); p1.setLeading(img.getScaledHeight() * 1.1f); document.add(p1); document.add(table); Paragraph p2 = new Paragraph("More templates "); p2.setLeading(img.getScaledHeight() * 1.1f); p2.setAlignment(Element.ALIGN_JUSTIFIED); img.scalePercent(70); for (int k = 0; k < 20; ++k) p2.add(ck); document.add(p2); // step 5: we close the document document.close(); } catch (Exception de) { System.err.println(de.getMessage()); } }