List of usage examples for com.lowagie.text.pdf PdfContentByte showTextAligned
public void showTextAligned(int alignment, String text, float x, float y, float rotation)
From source file:org.oscarehr.common.printing.HtmlToPdfServlet.java
License:Open Source License
private static void printText(PdfContentByte over, String text, float x, float y) { over.beginText();//from www.j a v a2s . c o m over.showTextAligned(PdfContentByte.ALIGN_CENTER, text, x, y, 0); over.endText(); }
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;//from ww w.j av a2 s .c om try { 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.sakaiproject.evaluation.tool.reporting.EvalPDFReportBuilder.java
License:Educational Community License
public void addTitlePage(String evaltitle, String groupNames, String startDate, String endDate, String responseInformation, byte[] bannerImageBytes, String evalSystemTitle, String informationTitle) { try {/*from www .j a v a 2s . c om*/ float pagefooter = paragraphFont.getSize(); PdfContentByte cb = pdfWriter.getDirectContent(); float docMiddle = (document.right() - document.left()) / 2 + document.leftMargin(); Paragraph emptyPara = new Paragraph(" "); emptyPara.setSpacingAfter(100.0f); // Title Paragraph titlePara = new Paragraph("\n\n\n" + evaltitle, frontTitleFont); titlePara.setAlignment(Element.ALIGN_CENTER); document.add(titlePara); // Groups Paragraph groupPara = new Paragraph(groupNames, frontAuthorFont); groupPara.setSpacingBefore(25.0f); groupPara.setAlignment(Element.ALIGN_CENTER); document.add(groupPara); // Little info area? I don't know, it was on the mockup though Paragraph infoPara = new Paragraph(informationTitle, frontInfoFont); infoPara.setAlignment(Element.ALIGN_CENTER); infoPara.setSpacingBefore(90.0f); document.add(infoPara); // Started on Paragraph startedPara = new Paragraph(startDate, frontInfoFont); startedPara.setAlignment(Element.ALIGN_CENTER); startedPara.setSpacingBefore(25.0f); document.add(startedPara); // Ended on Paragraph endedPara = new Paragraph(endDate, frontInfoFont); endedPara.setAlignment(Element.ALIGN_CENTER); endedPara.setSpacingBefore(25.0f); document.add(endedPara); // Reply Rate Paragraph replyRatePara = new Paragraph(responseInformation, frontInfoFont); replyRatePara.setAlignment(Element.ALIGN_CENTER); replyRatePara.setSpacingBefore(25.0f); document.add(replyRatePara); // Logo and Tagline cb.beginText(); cb.setFontAndSize(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.EMBEDDED), 12); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, evalSystemTitle, docMiddle, document.bottom() + 20, 0); cb.endText(); if (bannerImageBytes != null) { Image banner = Image.getInstance(bannerImageBytes); cb.addImage(banner, banner.getWidth(), 0, 0, banner.getHeight(), docMiddle - (banner.getWidth() / 2), document.bottom() + 35); } document.newPage(); responseArea = new ColumnText(cb); responseArea.setSimpleColumn(document.left(), document.top(), document.right() / 2, document.bottom() + pagefooter); responseArea.go(); } catch (DocumentException | IOException de) { throw UniversalRuntimeException.accumulate(de, "Unable to create title page"); } }
From source file:org.unitime.timetable.util.PdfEventHandler.java
License:Open Source License
/** * Print footer string on each page/* w w w . ja va2 s .co m*/ * @param writer * @param document */ public void onEndPage(PdfWriter writer, Document document) { if (getDateTime() == null) { setDateTime(new Date()); } PdfContentByte cb = writer.getDirectContent(); cb.beginText(); cb.setFontAndSize(getBaseFont(), getFontSize()); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, getDateFormat().format(getDateTime()), document.left(), 20, 0); cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, String.valueOf(document.getPageNumber()), document.right(), 20, 0); cb.showTextAligned(PdfContentByte.ALIGN_CENTER, MESSAGES.pdfCopyright(Constants.getVersion()), (document.left() + document.right()) / 2, 20, 0); cb.endText(); return; }
From source file:oscar.eform.util.EFormPDFServlet.java
License:Open Source License
private void writeContent(Properties printCfg, Properties props, Properties measurements, float height, PdfContentByte cb) throws Exception { for (Enumeration e = printCfg.propertyNames(); e.hasMoreElements();) { StringBuilder temp = new StringBuilder(e.nextElement().toString()); String[] cfgVal = printCfg.getProperty(temp.toString()).split(" *, *"); String[] fontType = null; int fontFlags = 0; 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/*w w w .j a v a 2 s . c o m*/ fontFlags = Font.NORMAL; } else { fontFlags = Font.NORMAL; fontType = new String[] { cfgVal[4].trim() }; } String encoding = null; 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; } BaseFont bf = BaseFont.createFont(fontType[0], encoding, BaseFont.NOT_EMBEDDED); String propValue = props.getProperty(temp.toString()); //if not in regular config then check measurements if (propValue == null) { propValue = measurements.getProperty(temp.toString(), ""); } ColumnText ct = new ColumnText(cb); // 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, propValue, font)); ct.go(); continue; } // draw line directly if (temp.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())); cb.stroke(); } else if (temp.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()) : propValue), 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 ? ((propValue.equals("") ? "" : cfgVal[6].trim())) : propValue), Integer.parseInt(cfgVal[1].trim()), (height - Integer.parseInt(cfgVal[2].trim())), 0); cb.endText(); } } }
From source file:oscar.oscarLab.ca.all.pageUtil.LabPDFCreator.java
License:Open Source License
public void onEndPage(PdfWriter writer, Document document) { try {//from www . jav a2 s. com 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.directcontent.InterpretOCR.java
public static void main(String[] args) throws IOException, DocumentException { Document document = new Document(PageSize.LETTER); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open();//from w w w .j a v a2 s .co m PdfContentByte cb = writer.getDirectContent(); BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); cb.beginText(); cb.setFontAndSize(font, 12); String line; String word; @SuppressWarnings("unused") float llx, lly, urx, ury; StringTokenizer tokenizer; BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(RESOURCE))); while ((line = reader.readLine()) != null) { tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { tokenizer.nextToken(); word = tokenizer.nextToken(); llx = Float.parseFloat(tokenizer.nextToken() + "f") / 10; lly = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10; urx = Float.parseFloat(tokenizer.nextToken() + "f") / 10; ury = document.top() - Float.parseFloat(tokenizer.nextToken() + "f") / 10; cb.showTextAligned(Element.ALIGN_LEFT, word, (llx + urx) / 2, lly, 0); } } cb.endText(); document.close(); }
From source file:questions.forms.RadioButtonsOnDifferentPages.java
public static void main(String[] args) { Document document = new Document(); try {/*from w ww .j a v a2 s . co m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); String[] languages = { "English", "French", "Dutch" }; Rectangle rect; // create radio button field and its kids PdfFormField language = PdfFormField.createRadioButton(writer, true); language.setFieldName("language"); language.setValueAsName(languages[0]); for (int i = 0; i < languages.length; i++) { rect = new Rectangle(40, 806 - i * 40, 60, 788 - i * 40); addRadioButton(writer, rect, language, languages[i], i == 0, writer.getPageNumber() + i); } writer.addAnnotation(language); // add the page content for (int i = 0; i < languages.length; i++) { cb.beginText(); cb.setFontAndSize(bf, 18); cb.showTextAligned(Element.ALIGN_LEFT, languages[i], 70, 790 - i * 40, 0); cb.endText(); document.newPage(); } } catch (Exception e) { e.printStackTrace(); } // step 5: we close the document document.close(); }
From source file:questions.importpages.ConcatenateWithTOC.java
public static PdfImportedPage getStampedPage(PdfReader reader, PdfCopy copy, int oldP, int newP, BaseFont bf) throws IOException { PdfImportedPage page = copy.getImportedPage(reader, oldP); PageStamp pagestamp = copy.createPageStamp(page); PdfContentByte canvas = pagestamp.getOverContent(); canvas.beginText();//from ww w . jav a 2 s .c o m canvas.setFontAndSize(bf, 12); canvas.showTextAligned(Element.ALIGN_LEFT, "page " + newP, 36, 26, 0); canvas.endText(); pagestamp.alterContents(); return page; }
From source file:questions.importpages.ConcatenateWithTOC2.java
public static void main(String[] args) { try {/*from w ww .j ava 2 s . c o m*/ // suppose we have some TEST PDF files for (int i = 0; i < TEST.length; i++) { createTestPdf(i); } // and we want to concatenate them Document document = new Document(); ByteArrayOutputStream os = new ByteArrayOutputStream(); PdfCopy copy = new PdfCopy(document, os); copy.setViewerPreferences(PdfWriter.PageModeUseOutlines); document.open(); // but we want to create an outline tree PdfOutline root = copy.getRootOutline(); // we also want an extra page with the file name Document coverpage; ByteArrayOutputStream baos; PdfReader reader; // we want keep track of the page numbers int pagenumber = 0; for (int i = 0; i < TEST.length; i++) { // we create the coverpage in memory coverpage = new Document(); baos = new ByteArrayOutputStream(); PdfWriter.getInstance(coverpage, baos); coverpage.open(); coverpage.add(new Paragraph("file: " + TEST[i])); coverpage.close(); // we import that page reader = new PdfReader(baos.toByteArray()); pagenumber++; copy.addPage(copy.getImportedPage(reader, 1)); // we create the bookmark to that page PdfDestination dest = new PdfDestination(PdfDestination.FIT); new PdfOutline(root, PdfAction.gotoLocalPage(pagenumber, dest, copy), TEST[i]); // we import the document itself reader = new PdfReader(TEST[i]); for (int j = 1; j <= reader.getNumberOfPages(); j++) { pagenumber++; copy.addPage(copy.getImportedPage(reader, j)); } } document.close(); // we want to add page X of Y reader = new PdfReader(os.toByteArray()); int n = reader.getNumberOfPages(); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); BaseFont bf = BaseFont.createFont(); for (int i = 1; i <= n; i++) { PdfContentByte canvas = stamper.getOverContent(i); canvas.beginText(); canvas.setFontAndSize(bf, 12); canvas.showTextAligned(Element.ALIGN_LEFT, "page " + i + " of " + n, 36, 26, 0); canvas.endText(); } stamper.close(); } catch (IOException ioe) { ioe.printStackTrace(); } catch (DocumentException de) { de.printStackTrace(); } }