List of usage examples for com.lowagie.text Font Font
public Font(int family, float size)
From source file:oscar.form.pdfservlet.FrmCustomedPDFServlet.java
License:Open Source License
protected ByteArrayOutputStream generatePDFDocumentBytes(final HttpServletRequest req, final ServletContext ctx) throws DocumentException { logger.debug("***in generatePDFDocumentBytes2 FrmCustomedPDFServlet.java***"); // added by vic, hsfo Enumeration<String> em = req.getParameterNames(); while (em.hasMoreElements()) { logger.debug("para=" + em.nextElement()); }// www . ja v a2s . c o m em = req.getAttributeNames(); while (em.hasMoreElements()) logger.debug("attr: " + em.nextElement()); if (HSFO_RX_DATA_KEY.equals(req.getParameter("__title"))) { return generateHsfoRxPDF(req); } String newline = System.getProperty("line.separator"); ByteArrayOutputStream baosPDF = new ByteArrayOutputStream(); PdfWriter writer = null; String method = req.getParameter("__method"); String origPrintDate = null; String numPrint = null; if (method != null && method.equalsIgnoreCase("rePrint")) { origPrintDate = req.getParameter("origPrintDate"); numPrint = req.getParameter("numPrints"); } logger.debug("method in generatePDFDocumentBytes " + method); String clinicName; String clinicTel; String clinicFax; // check if satellite clinic is used String useSatelliteClinic = req.getParameter("useSC"); logger.debug(useSatelliteClinic); if (useSatelliteClinic != null && useSatelliteClinic.equalsIgnoreCase("true")) { String scAddress = req.getParameter("scAddress"); logger.debug("clinic detail" + "=" + scAddress); HashMap<String, String> hm = parseSCAddress(scAddress); clinicName = hm.get("clinicName"); clinicTel = hm.get("clinicTel"); clinicFax = hm.get("clinicFax"); } else { // parameters need to be passed to header and footer clinicName = req.getParameter("clinicName"); logger.debug("clinicName" + "=" + clinicName); clinicTel = req.getParameter("clinicPhone"); clinicFax = req.getParameter("clinicFax"); } String patientPhone = req.getParameter("patientPhone"); String patientCityPostal = req.getParameter("patientCityPostal"); String patientAddress = req.getParameter("patientAddress"); String patientName = req.getParameter("patientName"); String sigDoctorName = req.getParameter("sigDoctorName"); String rxDate = req.getParameter("rxDate"); String rx = req.getParameter("rx"); String patientDOB = req.getParameter("patientDOB"); String showPatientDOB = req.getParameter("showPatientDOB"); String imgFile = req.getParameter("imgFile"); String patientHIN = req.getParameter("patientHIN"); String patientChartNo = req.getParameter("patientChartNo"); String pracNo = req.getParameter("pracNo"); Locale locale = req.getLocale(); boolean isShowDemoDOB = false; if (showPatientDOB != null && showPatientDOB.equalsIgnoreCase("true")) { isShowDemoDOB = true; } if (!isShowDemoDOB) patientDOB = ""; if (rx == null) { rx = ""; } String additNotes = req.getParameter("additNotes"); String[] rxA = rx.split(newline); List<String> listRx = new ArrayList<String>(); String listElem = ""; // parse rx and put into a list of rx; for (String s : rxA) { if (s.equals("") || s.equals(newline) || s.length() == 1) { listRx.add(listElem); listElem = ""; } else { listElem = listElem + s; listElem += newline; } } // get the print prop values Properties props = new Properties(); StringBuilder temp = new StringBuilder(); for (Enumeration<String> e = req.getParameterNames(); e.hasMoreElements();) { temp = new StringBuilder(e.nextElement().toString()); props.setProperty(temp.toString(), req.getParameter(temp.toString())); } for (Enumeration<String> e = req.getAttributeNames(); e.hasMoreElements();) { temp = new StringBuilder(e.nextElement().toString()); props.setProperty(temp.toString(), req.getAttribute(temp.toString()).toString()); } Document document = new Document(); try { String title = req.getParameter("__title") != null ? req.getParameter("__title") : "Unknown"; // specify the page of the picture using __graphicPage, it may be used multiple times to specify multiple pages // however the same graphic will be applied to all pages // ie. __graphicPage=2&__graphicPage=3 String[] cfgGraphicFile = req.getParameterValues("__cfgGraphicFile"); int cfgGraphicFileNo = cfgGraphicFile == null ? 0 : cfgGraphicFile.length; if (cfgGraphicFile != null) { // for (String s : cfgGraphicFile) { // p("cfgGraphicFile", s); // } } String[] graphicPage = req.getParameterValues("__graphicPage"); ArrayList<String> graphicPageArray = new ArrayList<String>(); if (graphicPage != null) { // for (String s : graphicPage) { // p("graphicPage", s); // } graphicPageArray = new ArrayList<String>(Arrays.asList(graphicPage)); } // A0-A10, LEGAL, LETTER, HALFLETTER, _11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA // and FLSE // the following shows a temp way to get a print page size Rectangle pageSize = PageSize.LETTER; String pageSizeParameter = req.getParameter("rxPageSize"); if (pageSizeParameter != null) { if ("PageSize.HALFLETTER".equals(pageSizeParameter)) { pageSize = PageSize.HALFLETTER; } else if ("PageSize.A6".equals(pageSizeParameter)) { pageSize = PageSize.A6; } else if ("PageSize.A4".equals(pageSizeParameter)) { pageSize = PageSize.A4; } } /* * if ("PageSize.HALFLETTER".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.HALFLETTER; } else if ("PageSize.A6".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.A6; } else if * ("PageSize.A4".equals(props.getProperty(PAGESIZE))) { pageSize = PageSize.A4; } */ // p("size of page ", props.getProperty(PAGESIZE)); document.setPageSize(pageSize); // 285=left margin+width of box, 5f is space for looking nice document.setMargins(15, pageSize.getWidth() - 285f + 5f, 170, 60);// left, right, top , bottom writer = PdfWriter.getInstance(document, baosPDF); writer.setPageEvent(new EndPage(clinicName, clinicTel, clinicFax, patientPhone, patientCityPostal, patientAddress, patientName, patientDOB, sigDoctorName, rxDate, origPrintDate, numPrint, imgFile, patientHIN, patientChartNo, pracNo, locale)); document.addTitle(title); document.addSubject(""); document.addKeywords("pdf, itext"); document.addCreator("OSCAR"); document.addAuthor(""); document.addHeader("Expires", "0"); document.open(); document.newPage(); PdfContentByte cb = writer.getDirectContent(); BaseFont bf; // = normFont; cb.setRGBColorStroke(0, 0, 255); // render prescriptions for (String rxStr : listRx) { bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Paragraph p = new Paragraph(new Phrase(rxStr, new Font(bf, 10))); p.setKeepTogether(true); p.setSpacingBefore(5f); document.add(p); } // render additional notes if (additNotes != null && !additNotes.equals("")) { bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Paragraph p = new Paragraph(new Phrase(additNotes, new Font(bf, 10))); p.setKeepTogether(true); p.setSpacingBefore(10f); document.add(p); } // render optometristEyeParam if (req.getAttribute("optometristEyeParam") != null) { bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); Paragraph p = new Paragraph( new Phrase("Eye " + (String) req.getAttribute("optometristEyeParam"), new Font(bf, 10))); p.setKeepTogether(true); p.setSpacingBefore(15f); document.add(p); } // render QrCode if (PrescriptionQrCodeUIBean.isPrescriptionQrCodeEnabledForCurrentProvider()) { Integer scriptId = Integer.parseInt(req.getParameter("scriptId")); byte[] qrCodeImage = PrescriptionQrCodeUIBean.getPrescriptionHl7QrCodeImage(scriptId); Image qrCode = Image.getInstance(qrCodeImage); document.add(qrCode); } } catch (DocumentException dex) { baosPDF.reset(); throw dex; } catch (Exception e) { logger.error("Error", e); } finally { if (document != null) { document.close(); } if (writer != null) { writer.close(); } } logger.debug("***END in generatePDFDocumentBytes2 FrmCustomedPDFServlet.java***"); return baosPDF; }
From source file:questions.compression.CompressionLevelsFonts.java
public static void createPdf(int compressionLevel) { try {//w ww. jav a 2 s .c o m Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT[compressionLevel + 1])); writer.setCompressionLevel(compressionLevel); document.open(); BaseFont bf = BaseFont.createFont(RESOURCE, BaseFont.WINANSI, BaseFont.EMBEDDED); bf.setCompressionLevel(compressionLevel); Font f = new Font(bf, 12); document.add(new Paragraph("0123456789", f)); document.add(new Paragraph("abcdefghijklmnopqrstuvwxyz", f)); document.add(new Paragraph("ABCDEFGHIJKLMNOPQRSTUVWXYZ", f)); document.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } }
From source file:questions.graphics2D.ArabicText.java
public static void main(String[] args) { Document document = new Document(PageSize.A4); try {// ww w . jav a2s .c o m PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); String text1 = "\u0634\u0627\u062f\u062c\u0645\u0647\u0648\u0631"; String text2 = "\u0634"; java.awt.Font font = new java.awt.Font("arial", 0, 12); PdfContentByte cb = writer.getDirectContent(); java.awt.Graphics2D g2Shapes = cb.createGraphicsShapes(PageSize.A4.getWidth(), PageSize.A4.getHeight()); g2Shapes.setFont(font); g2Shapes.drawString("text1, expected to render RTL", 50, 100); g2Shapes.drawString(text1, 50, 120); g2Shapes.drawString("text2, expected to match right-most glyph above", 50, 140); g2Shapes.drawString(text2, 50, 160); g2Shapes.dispose(); ColumnText text = new ColumnText(cb); Font f = new Font( BaseFont.createFont("c://windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED), 12); text.setSimpleColumn(50, 620, 545, 50); text.setRunDirection(PdfWriter.RUN_DIRECTION_RTL); text.setText(new Phrase(text1, f)); text.go(); text.setText(new Phrase(text2, f)); text.go(); FontMapper arialuni = new FontMapper() { public BaseFont awtToPdf(java.awt.Font font) { try { return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public java.awt.Font pdfToAwt(BaseFont font, int size) { return null; } }; java.awt.Graphics2D g = cb.createGraphics(PageSize.A4.getWidth(), PageSize.A4.getHeight(), arialuni); g.setFont(null); g.drawString("text1, not expected to render RTL", 50, 180); g.drawString(text1, 50, 200); g.drawString("text2, not expected to match right-most glyph above", 50, 220); g.drawString(text2, 50, 240); g.drawString("to your right you see what it SHOULD look like:", 50, 260); g.drawString("If it doesn't, the problem is in the JDK, it's not an iText problem.", 50, 280); g.dispose(); document.close(); } catch (Exception de) { de.printStackTrace(); } }
From source file:questions.importpages.NameCard.java
public static void createOneCard() throws DocumentException, IOException { Rectangle rect = new Rectangle(Utilities.millimetersToPoints(86.5f), Utilities.millimetersToPoints(55)); Document document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(CARD)); writer.setViewerPreferences(PdfWriter.PrintScalingNone); document.open();/*from w w w. j ava2 s. co m*/ PdfReader reader = new PdfReader(LOGO); Image img = Image.getInstance(writer.getImportedPage(reader, 1)); img.scaleToFit(rect.getWidth() / 1.5f, rect.getHeight() / 1.5f); img.setAbsolutePosition((rect.getWidth() - img.getScaledWidth()) / 2, (rect.getHeight() - img.getScaledHeight()) / 2); document.add(img); document.newPage(); BaseFont bf = BaseFont.createFont(FONT, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); Font font = new Font(bf, 12); font.setColor(new CMYKColor(1, 0.5f, 0, 0.467f)); ColumnText column = new ColumnText(writer.getDirectContent()); Paragraph p; p = new Paragraph("Bruno Lowagie\n1T3XT\nbruno@1t3xt.com", font); p.setAlignment(Element.ALIGN_CENTER); column.addElement(p); column.setSimpleColumn(0, 0, rect.getWidth(), rect.getHeight() * 0.75f); column.go(); document.close(); }
From source file:questions.objects.DifferentLeadings.java
public static void main(String[] args) { Document document = new Document(PageSize.A7); try {/*from w w w .j a v a2s . c o m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); Chunk space = new Chunk(' '); String text = "Quick brown fox jumps over the lazy dog."; Phrase phrase1 = new Phrase(text, new Font(Font.HELVETICA, 12)); Phrase phrase2 = new Phrase(new Chunk(text, new Font(Font.TIMES_ROMAN, 24))); Phrase phrase3 = new Phrase(text, new Font(Font.COURIER, 8)); Phrase phrase4 = new Phrase(text, new Font(Font.HELVETICA, 4)); Paragraph paragraph = new Paragraph(); paragraph.add(phrase1); paragraph.add(space); paragraph.add(phrase2); paragraph.add(space); paragraph.add(phrase3); paragraph.add(space); paragraph.add(phrase4); paragraph.setMultipliedLeading(1.5f); paragraph.setAlignment(Element.ALIGN_JUSTIFIED); ColumnText column = new ColumnText(writer.getDirectContent()); column.setSimpleColumn(document.left(), document.bottom(), document.right(), document.top()); column.addElement(paragraph); column.go(); document.newPage(); document.add(paragraph); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } document.close(); }