List of usage examples for com.lowagie.text.pdf PdfWriter getDirectContent
public PdfContentByte getDirectContent()
From source file:org.oscarehr.casemgmt.service.PageNumberStamper.java
License:Open Source License
/** * Initializes template and base font/* www . ja v a 2s . co m*/ * * @param writer * @param document */ public void onOpenDocument(PdfWriter writer, Document document) { total = writer.getDirectContent().createTemplate(100, 100); total.setBoundingBox(new Rectangle(-40, -40, 100, 100)); }
From source file:org.oscarehr.casemgmt.service.PromoTextStamper.java
License:Open Source License
/** * Adds promo text, date and current page number to each page * //from ww w .j ava 2 s.c o m * @param writer * @param document */ public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); float textBase = document.bottom() - getBaseOffset(); float width = document.getPageSize().getWidth(); float center = width / 2.0f; cb.beginText(); cb.setFontAndSize(getFont(), getFontSize()); cb.setTextMatrix(document.left(), textBase); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text, center, textBase, 0); cb.endText(); cb.restoreState(); }
From source file:org.oscarehr.common.printing.HtmlToPdfServlet.java
License:Open Source License
private static void appendFooter(byte[] pdf, ByteArrayOutputStream baos) { PdfReader reader = null;//w ww . j a v a 2s.c o m PdfWriter writer = null; Document document = null; try { // do initialization reader = new PdfReader(pdf); document = new Document(PageSize.LETTER); writer = PdfWriterFactory.newInstance(document, baos, FontSettings.HELVETICA_6PT); document.open(); PdfContentByte cb = writer.getDirectContent(); // copy pages for (int i = 1; i <= reader.getNumberOfPages(); i++) { cb.addTemplate(writer.getImportedPage(reader, i), 0, 0); } } catch (Exception e) { throw new RuntimeException("Unable to append document footer", e); } finally { close(document); close(writer); close(reader); } }
From source file:org.oscarehr.phr.web.PHRUserManagementAction.java
License:Open Source License
public ByteArrayOutputStream generateUserRegistrationLetter(String demographicNo, String username, String password) throws Exception { log.debug("Demographic " + demographicNo + " username " + username + " password " + password); DemographicDao demographicDao = (DemographicDao) SpringUtils.getBean("demographicDao"); Demographic demographic = demographicDao.getDemographic(demographicNo); final String PAGESIZE = "printPageSize"; Document document = new Document(); ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); PdfWriter writer = null; try {//from w w w . j av a2 s . c o m writer = PdfWriter.getInstance(document, baosPDF); String title = "TITLE"; String template = "MyOscarLetterHead.pdf"; Properties printCfg = getCfgProp(); String[] cfgVal = null; StringBuilder tempName = null; // get the print prop values Properties props = new Properties(); props.setProperty("letterDate", UtilDateUtilities.getToday("yyyy-MM-dd")); props.setProperty("name", demographic.getFirstName() + " " + demographic.getLastName()); props.setProperty("dearname", demographic.getFirstName() + " " + demographic.getLastName()); props.setProperty("address", demographic.getAddress()); props.setProperty("city", demographic.getCity() + ", " + demographic.getProvince()); props.setProperty("postalCode", demographic.getPostal()); props.setProperty("credHeading", "MyOscar User Account Details"); props.setProperty("username", "Username: " + username); props.setProperty("password", "Password: " + password); //Temporary - the intro will change to be dynamic props.setProperty("intro", "We are pleased to provide you with a log in and password for your new MyOSCAR Personal Health Record. This account will allow you to connect electronically with our clinic. Please take a few minutes to review the accompanying literature for further information.We look forward to you benefiting from this service."); document.addTitle(title); document.addSubject(""); document.addKeywords("pdf, itext"); document.addCreator("OSCAR"); document.addAuthor(""); document.addHeader("Expires", "0"); Rectangle pageSize = PageSize.LETTER; document.setPageSize(pageSize); document.open(); // create a reader for a certain document String propFilename = oscar.OscarProperties.getInstance().getProperty("pdfFORMDIR", "") + "/" + template; PdfReader reader = null; try { reader = new PdfReader(propFilename); log.debug("Found template at " + propFilename); } catch (Exception dex) { log.debug("change path to inside oscar from :" + propFilename); reader = new PdfReader("/oscar/form/prop/" + template); log.debug("Found template at /oscar/form/prop/" + template); } // retrieve the total number of pages int n = reader.getNumberOfPages(); // retrieve the size of the first page Rectangle pSize = reader.getPageSize(1); float width = pSize.getWidth(); float height = pSize.getHeight(); log.debug("Width :" + width + " Height: " + height); PdfContentByte cb = writer.getDirectContent(); ColumnText ct = new ColumnText(cb); int fontFlags = 0; document.newPage(); PdfImportedPage page1 = writer.getImportedPage(reader, 1); cb.addTemplate(page1, 1, 0, 0, 1, 0, 0); BaseFont bf; // = normFont; String encoding; cb.setRGBColorStroke(0, 0, 255); String[] fontType; for (Enumeration e = printCfg.propertyNames(); e.hasMoreElements();) { tempName = new StringBuilder(e.nextElement().toString()); cfgVal = printCfg.getProperty(tempName.toString()).split(" *, *"); if (cfgVal[4].indexOf(";") > -1) { fontType = cfgVal[4].split(";"); if (fontType[1].trim().equals("italic")) fontFlags = Font.ITALIC; else if (fontType[1].trim().equals("bold")) fontFlags = Font.BOLD; else if (fontType[1].trim().equals("bolditalic")) fontFlags = Font.BOLDITALIC; else fontFlags = Font.NORMAL; } else { fontFlags = Font.NORMAL; fontType = new String[] { cfgVal[4].trim() }; } if (fontType[0].trim().equals("BaseFont.HELVETICA")) { fontType[0] = BaseFont.HELVETICA; encoding = BaseFont.CP1252; //latin1 encoding } else if (fontType[0].trim().equals("BaseFont.HELVETICA_OBLIQUE")) { fontType[0] = BaseFont.HELVETICA_OBLIQUE; encoding = BaseFont.CP1252; } else if (fontType[0].trim().equals("BaseFont.ZAPFDINGBATS")) { fontType[0] = BaseFont.ZAPFDINGBATS; encoding = BaseFont.ZAPFDINGBATS; } else { fontType[0] = BaseFont.COURIER; encoding = BaseFont.CP1252; } bf = BaseFont.createFont(fontType[0], encoding, BaseFont.NOT_EMBEDDED); // write in a rectangle area if (cfgVal.length >= 9) { Font font = new Font(bf, Integer.parseInt(cfgVal[5].trim()), fontFlags); ct.setSimpleColumn(Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), Integer.parseInt(cfgVal[7].trim()), (height - Integer.parseInt(cfgVal[8].trim())), Integer.parseInt(cfgVal[9].trim()), (cfgVal[0].trim().equals("left") ? Element.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? Element.ALIGN_RIGHT : Element.ALIGN_CENTER))); ct.setText(new Phrase(12, props.getProperty(tempName.toString(), ""), font)); ct.go(); continue; } // draw line directly if (tempName.toString().startsWith("__$line")) { cb.setRGBColorStrokeF(0f, 0f, 0f); cb.setLineWidth(Float.parseFloat(cfgVal[4].trim())); cb.moveTo(Float.parseFloat(cfgVal[0].trim()), Float.parseFloat(cfgVal[1].trim())); cb.lineTo(Float.parseFloat(cfgVal[2].trim()), Float.parseFloat(cfgVal[3].trim())); // stroke the lines cb.stroke(); // write text directly } else if (tempName.toString().startsWith("__")) { cb.beginText(); cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim())); cb.showTextAligned( (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER)), (cfgVal.length >= 7 ? (cfgVal[6].trim()) : props.getProperty(tempName.toString(), "")), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } else if (tempName.toString().equals("forms_promotext")) { if (OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT") != null) { cb.beginText(); cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim())); cb.showTextAligned( (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER)), OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT"), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } } else { // write prop text cb.beginText(); cb.setFontAndSize(bf, Integer.parseInt(cfgVal[5].trim())); cb.showTextAligned( (cfgVal[0].trim().equals("left") ? PdfContentByte.ALIGN_LEFT : (cfgVal[0].trim().equals("right") ? PdfContentByte.ALIGN_RIGHT : PdfContentByte.ALIGN_CENTER)), (cfgVal.length >= 7 ? ((props.getProperty(tempName.toString(), "").equals("") ? "" : cfgVal[6].trim())) : props.getProperty(tempName.toString(), "")), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } } } catch (DocumentException dex) { baosPDF.reset(); throw dex; } finally { if (document != null) document.close(); if (writer != null) writer.close(); } return baosPDF; }
From source file:org.posterita.core.PDFReportPageEventHelper.java
License:Open Source License
public void onOpenDocument(PdfWriter writer, Document document) { SimpleDateFormat sdf = new SimpleDateFormat(TimestampConvertor.DEFAULT_DATE_PATTERN1); dateAndTime = sdf.format(Calendar.getInstance().getTime()); table = new PdfPTable(2); tpl = writer.getDirectContent().createTemplate(100, 100); tpl.setBoundingBox(new Rectangle(-20, -20, 100, 100)); }
From source file:org.posterita.core.PDFReportPageEventHelper.java
License:Open Source License
public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/* w w w. j a v a 2s .c o m*/ // write the headertable table.setTotalWidth(document.right() - document.left()); table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 50, cb); // compose the footer String text = "Page " + writer.getPageNumber() + " of "; float textSize = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint(text, 10); float textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(PAGE_FOOTER_FONT.getBaseFont(), 10); float adjust = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint("0", 10); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); text = "Report Generated on : " + dateAndTime; textSize = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint(text, 10); textBase = document.bottom() - 20; cb.beginText(); cb.setFontAndSize(PAGE_FOOTER_FONT.getBaseFont(), 10); adjust = PAGE_FOOTER_FONT.getBaseFont().getWidthPoint("0", 10); cb.setTextMatrix(MARGIN, textBase); cb.showText(text); cb.endText(); cb.saveState(); }
From source file:org.projectlibre.export.ImageExport.java
License:Common Public License
public static void export(final GraphPageable pageable, Component parentComponent) throws IOException { final File file = chooseFile(pageable.getRenderer().getProject().getName(), parentComponent); final JobQueue jobQueue = SessionFactory.getInstance().getJobQueue(); Job job = new Job(jobQueue, "Image Export", "Exporting Image...", true, parentComponent); job.addRunnable(new JobRunnable("Image Export", 1.0f) { public Object run() throws Exception { boolean pdf = true; if (file.getName().endsWith(".png")) pdf = false;// www. j av a 2s .c o m Document document = null; PdfWriter writer = null; if (pdf) { document = new Document(); writer = PdfWriter.getInstance(document, new FileOutputStream(file)); } else { } pageable.update(); int pageCount = pageable.getNumberOfPages(); if (pageCount > 0) { ViewPrintable printable = pageable.getSafePrintable(); ExtendedPageFormat pageFormat = pageable.getSafePageFormat(); double width = pageFormat.getWidth(); double height = pageFormat.getHeight(); float startIncrement = 0.1f; float endIncrement = 0.0f; float progressIncrement = (1.0f - startIncrement - endIncrement) / pageCount; for (int p = 0; p < pageCount; p++) { setProgress(startIncrement + p * progressIncrement); if (pdf) { document.setPageSize(new Rectangle((float) width, (float) height)); if (p == 0) document.open(); else document.newPage(); Graphics2D g = writer.getDirectContent().createGraphics((float) width, (float) height); printable.print(g, p); g.dispose(); } else { BufferedImage bi = new BufferedImage((int) width, (int) height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = (Graphics2D) bi.createGraphics(); g2.setBackground(Color.WHITE); printable.print(g2, p); g2.dispose(); ImageIO.write(bi, "png", new FileOutputStream(file)); break; } } if (pdf) document.close(); } setProgress(1.0f); return null; } }); jobQueue.schedule(job); }
From source file:org.projectlibre.export.PDFExport.java
License:Common Public License
public static void export(final GraphPageable pageable, Component parentComponent) throws IOException { final File file = chooseFile(pageable.getRenderer().getProject().getName(), parentComponent); final JobQueue jobQueue = SessionFactory.getInstance().getJobQueue(); Job job = new Job(jobQueue, "PDF Export", "Exporting PDF...", true, parentComponent); job.addRunnable(new JobRunnable("PDF Export", 1.0f) { public Object run() throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file)); pageable.update();/* ww w . j a v a2 s . com*/ int pageCount = pageable.getNumberOfPages(); if (pageCount > 0) { ViewPrintable printable = pageable.getSafePrintable(); ExtendedPageFormat pageFormat = pageable.getSafePageFormat(); double width = pageFormat.getWidth(); double height = pageFormat.getHeight(); float startIncrement = 0.1f; float endIncrement = 0.0f; float progressIncrement = (1.0f - startIncrement - endIncrement) / pageCount; for (int p = 0; p < pageCount; p++) { setProgress(startIncrement + p * progressIncrement); document.setPageSize(new Rectangle((float) width, (float) height)); if (p == 0) document.open(); else document.newPage(); Graphics2D g = writer.getDirectContent().createGraphics((float) width, (float) height); printable.print(g, p); g.dispose(); } document.close(); } setProgress(1.0f); return null; } }); jobQueue.schedule(job); }
From source file:org.revager.export.PDFPageEventHelper.java
License:Open Source License
/** * Sets the marks to the PDF document.//from w w w. j av a2 s . c o m * * @param writer * the PDF writer * @param document * the PDF document */ private void setMarks(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); float height = PDFTools.ptToCm(document.getPageSize().getHeight()); cb.setLineWidth(0.0f); cb.moveTo(0.0f, PDFTools.cmToPt(height / 2.0f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height / 2.0f)); cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.33f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.33f)); cb.moveTo(0.0f, PDFTools.cmToPt(height * 0.66f)); cb.lineTo(PDFTools.cmToPt(0.3f), PDFTools.cmToPt(height * 0.66f)); cb.stroke(); }
From source file:org.revager.export.PDFPageEventHelper.java
License:Open Source License
@Override public void onOpenDocument(PdfWriter writer, Document document) { template = writer.getDirectContent().createTemplate(100, 100); }