List of usage examples for com.lowagie.text Rectangle getWidth
public float getWidth()
From source file:org.webguitoolkit.ui.util.export.PDFEvent.java
License:Apache License
public void onEndPage(PdfWriter writer, Document document) { TableExportOptions exportOptions = wgtTable.getExportOptions(); try {//from w ww . ja v a 2s . c om Rectangle page = document.getPageSize(); if (exportOptions.isShowDefaultHeader() || StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { PdfPTable head = new PdfPTable(3); head.getDefaultCell().setBorder(Rectangle.NO_BORDER); Paragraph title = new Paragraph(wgtTable.getTitle()); title.setAlignment(Element.ALIGN_LEFT); head.addCell(title); Paragraph empty = new Paragraph(""); head.addCell(empty); if (StringUtils.isNotEmpty(exportOptions.getHeaderImage())) { try { URL absoluteFileUrl = wgtTable.getPage().getClass() .getResource("/" + exportOptions.getHeaderImage()); if (absoluteFileUrl != null) { String path = absoluteFileUrl.getPath(); Image jpg = Image.getInstance(path); jpg.scaleAbsoluteHeight(40); jpg.scaleAbsoluteWidth(200); head.addCell(jpg); } } catch (Exception e) { logger.error(e.getMessage()); Paragraph noImage = new Paragraph("Image not found!"); head.addCell(noImage); } } else { head.addCell(empty); } head.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); head.writeSelectedRows(0, -1, document.leftMargin(), page.getHeight() - document.topMargin() + head.getTotalHeight(), writer.getDirectContent()); } if (exportOptions.isShowDefaultFooter() || StringUtils.isNotEmpty(exportOptions.getFooterText()) || exportOptions.isShowPageNumber()) { PdfPTable foot = new PdfPTable(3); String footerText = exportOptions.getFooterText() != null ? exportOptions.getFooterText() : ""; if (!exportOptions.isShowDefaultFooter()) { foot.addCell(new Paragraph(footerText)); foot.addCell(new Paragraph("")); } else { foot.getDefaultCell().setBorder(Rectangle.NO_BORDER); String leftText = ""; if (StringUtils.isNotEmpty(exportOptions.getFooterText())) { leftText = exportOptions.getFooterText(); } Paragraph left = new Paragraph(leftText); left.setAlignment(Element.ALIGN_LEFT); foot.addCell(left); DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, TextService.getLocale()); Date today = new Date(); String date = df.format(today); Paragraph center = new Paragraph(date); center.setAlignment(Element.ALIGN_CENTER); foot.addCell(center); } if (exportOptions.isShowPageNumber()) { Paragraph right = new Paragraph( TextService.getString("pdf.page@Page:") + " " + writer.getPageNumber()); right.setAlignment(Element.ALIGN_LEFT); foot.addCell(right); foot.setTotalWidth(page.getWidth() - document.leftMargin() - document.rightMargin()); foot.writeSelectedRows(0, -1, document.leftMargin(), document.bottomMargin(), writer.getDirectContent()); } else { foot.addCell(new Paragraph("")); } } } catch (Exception e) { throw new ExceptionConverter(e); } }
From source file:org.xhtmlrenderer.pdf.ITextUserAgent.java
License:Open Source License
public ImageResource getImageResource(String uriStr) { ImageResource resource = null;/*from www.j a va 2 s. c o m*/ if (ImageUtil.isEmbeddedBase64Image(uriStr)) { resource = loadEmbeddedBase64ImageResource(uriStr); } else { uriStr = resolveURI(uriStr); resource = (ImageResource) _imageCache.get(uriStr); if (resource == null) { InputStream is = resolveAndOpenStream(uriStr); if (is != null) { try { URI uri = new URI(uriStr); if (uri.getPath() != null && uri.getPath().toLowerCase().endsWith(".pdf")) { PdfReader reader = _outputDevice.getReader(uri); PDFAsImage image = new PDFAsImage(uri); Rectangle rect = reader.getPageSizeWithRotation(1); image.setInitialWidth(rect.getWidth() * _outputDevice.getDotsPerPoint()); image.setInitialHeight(rect.getHeight() * _outputDevice.getDotsPerPoint()); resource = new ImageResource(uriStr, image); } else { Image image = Image.getInstance(readStream(is)); scaleToOutputResolution(image); resource = new ImageResource(uriStr, new ITextFSImage(image)); } _imageCache.put(uriStr, resource); } catch (Exception e) { XRLog.exception("Can't read image file; unexpected problem for URI '" + uriStr + "'", e); } finally { try { is.close(); } catch (IOException e) { // ignore } } } } if (resource != null) { FSImage image = resource.getImage(); if (image instanceof ITextFSImage) { image = (FSImage) ((ITextFSImage) resource.getImage()).clone(); } resource = new ImageResource(resource.getImageUri(), image); } else { resource = new ImageResource(uriStr, null); } } return resource; }
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()); }/*from w w w .j av a2 s . c om*/ 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:oscar.oscarEncounter.oscarConsultationRequest.pageUtil.EctConsultationFormRequestPrintPdf.java
License:Open Source License
public void printPdf() throws IOException, DocumentException { EctConsultationFormRequestUtil reqForm = new EctConsultationFormRequestUtil(); reqForm.estRequestFromId((String) request.getAttribute("reqId")); //make sure we have data to print if (reqForm == null) throw new DocumentException(); // init req form info reqForm.specAddr = request.getParameter("address"); if (reqForm.specAddr == null) { reqForm.specAddr = new String(); }/*from w w w .ja v a 2 s . co m*/ reqForm.specPhone = request.getParameter("phone"); if (reqForm.specPhone == null) { reqForm.specPhone = ""; } reqForm.specFax = request.getParameter("fax"); if (reqForm.specFax == null) { reqForm.specFax = ""; } //Create new file to save form to String path = OscarProperties.getInstance().getProperty("DOCUMENT_DIR"); String fileName = path + "ConsultationRequestForm-" + UtilDateUtilities.getToday("yyyy-MM-dd.hh.mm.ss") + ".pdf"; FileOutputStream out = new FileOutputStream(fileName); //Create the document we are going to write to document = new Document(); writer = PdfWriter.getInstance(document, out); //Use the template located at '/oscar/oscarEncounter/oscarConsultationRequest/props' reader = new PdfReader("/oscar/oscarEncounter/oscarConsultationRequest/props/consultationFormRequest.pdf"); Rectangle pSize = reader.getPageSize(1); width = pSize.getWidth(); height = pSize.getHeight(); document.setPageSize(pSize); document.addTitle("Consultation Form Request"); document.addCreator("OSCAR"); document.open(); //Create the font we are going to print to bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb = writer.getDirectContent(); ct = new ColumnText(cb); cb.setColorStroke(new Color(0, 0, 0)); // start writing the pdf document PdfImportedPage page1 = writer.getImportedPage(reader, 1); cb.addTemplate(page1, 1, 0, 0, 1, 0, 0); addFooter(); setAppointmentInfo(reqForm); // add the dynamically positioned text elements float dynamicHeight = 0; dynamicHeight = addDynamicPositionedText("Reason For Consultation: ", reqForm.reasonForConsultation, dynamicHeight, reqForm); dynamicHeight = addDynamicPositionedText("Pertinent Clinical Information: ", reqForm.clinicalInformation, dynamicHeight, reqForm); dynamicHeight = addDynamicPositionedText("Significant Concurrent Problems: ", reqForm.concurrentProblems, dynamicHeight, reqForm); dynamicHeight = addDynamicPositionedText("Current Medications: ", reqForm.currentMedications, dynamicHeight, reqForm); dynamicHeight = addDynamicPositionedText("Allergies: ", reqForm.allergies, dynamicHeight, reqForm); document.close(); reader.close(); writer.close(); out.close(); // combine the recently created pdf with any pdfs that were added to the consultation request form combinePDFs(fileName); }
From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java
License:Open Source License
public void onEndPage(PdfWriter writer, Document document) { try {// w w w . ja v a 2s. c o m Rectangle page = document.getPageSize(); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); int pageNum = document.getPageNumber(); float width = page.getWidth(); float height = page.getHeight(); //add patient name header for every page but the first. if (pageNum > 1) { cb.beginText(); cb.setFontAndSize(bf, 8); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, handler.getPatientName(), 575, height - 30, 0); cb.endText(); } //add footer for every page cb.beginText(); cb.setFontAndSize(bf, 8); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "-" + pageNum + "-", width / 2, 30, 0); cb.endText(); // add promotext as footer if it is enabled if (OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT") != null) { cb.beginText(); cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 6); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, OscarProperties.getInstance().getProperty("FORMS_PROMOTEXT"), width / 2, 19, 0); cb.endText(); } // throw any exceptions } catch (Exception e) { throw new ExceptionConverter(e); } }
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();//w w w. ja v a 2 s .com 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.importpages.NameCards.java
public static void createSheet(int p) throws DocumentException, IOException { Rectangle rect = new Rectangle(PageSize.A4); Document document = new Document(rect); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(SHEET[p - 1])); document.open();/*from w w w . j a v a 2 s .c om*/ PdfContentByte canvas = writer.getDirectContentUnder(); PdfReader reader = new PdfReader(NameCard.CARD); PdfImportedPage front = writer.getImportedPage(reader, p); float x = rect.getWidth() / 2 - front.getWidth(); float y = (rect.getHeight() - (front.getHeight() * 5)) / 2; canvas.setLineWidth(0.5f); canvas.moveTo(x, y - 15); canvas.lineTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth(), y - 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y - 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); for (int i = 0; i < 5; i++) { for (int j = 0; j < 2; j++) { canvas.addTemplate(front, x, y); x += front.getWidth(); } x = rect.getWidth() / 2 - front.getWidth(); y += front.getHeight(); canvas.moveTo(x, y); canvas.lineTo(x - 15, y); canvas.moveTo(x + front.getWidth() * 2, y); canvas.lineTo(x + front.getWidth() * 2 + 15, y); canvas.stroke(); } canvas.moveTo(x, y + 15); canvas.lineTo(x, y); canvas.moveTo(x + front.getWidth(), y + 15); canvas.lineTo(x + front.getWidth(), y); canvas.moveTo(x + front.getWidth() * 2, y + 15); canvas.lineTo(x + front.getWidth() * 2, y); canvas.stroke(); document.close(); }
From source file:questions.ocg.AddOptionalWatermark.java
public static void main(String[] args) throws DocumentException, IOException { PdfReader reader = new PdfReader(RESOURCE); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); Image image1 = Image.getInstance(IMAGE_PRINTED); Image image2 = Image.getInstance(IMAGE_NOT_PRINTED); PdfLayer watermark_printed = new PdfLayer("printed", stamper.getWriter()); watermark_printed.setOn(false);/* w w w .ja v a 2 s.c o m*/ watermark_printed.setOnPanel(false); watermark_printed.setPrint("print", true); PdfLayer watermark_not_printed = new PdfLayer("not_printed", stamper.getWriter()); watermark_not_printed.setOn(true); watermark_not_printed.setOnPanel(false); watermark_not_printed.setPrint("print", false); for (int i = 0; i < stamper.getReader().getNumberOfPages();) { PdfContentByte cb = stamper.getUnderContent(++i); Rectangle rectangle = stamper.getReader().getPageSizeWithRotation(i); cb.beginLayer(watermark_printed); float AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image1.getPlainWidth()) / 2; float AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image1.getPlainHeight()) / 2; image1.setAbsolutePosition(AbsoluteX, AbsoluteY); cb.addImage(image1); cb.endLayer(); cb.beginLayer(watermark_not_printed); AbsoluteX = rectangle.getLeft() + (rectangle.getWidth() - image2.getPlainWidth()) / 2; AbsoluteY = rectangle.getBottom() + (rectangle.getHeight() - image2.getPlainHeight()) / 2; image2.setAbsolutePosition(AbsoluteX, AbsoluteY); cb.addImage(image2); cb.endLayer(); } stamper.close(); }