List of usage examples for com.lowagie.text Document left
public float left()
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildFooter(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();/* w w w .j a v a 2 s. c o m*/ String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.bottom() - 25; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.bottom() - 10); cb.lineTo(document.right(), document.bottom() - 10); cb.stroke(); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); textSize = FontUtils.getDejavu().getWidthPoint(footer.getGeneratedBy(), 7); cb.setTextMatrix(document.right() - textSize, textBase - 10); cb.showText(footer.getGeneratedBy()); textSize = FontUtils.getDejavu().getWidthPoint(generationDate, 7); cb.setTextMatrix(document.right() - textSize, textBase - 20); cb.showText(generationDate); cb.endText(); try { textSize = FontUtils.getDejavu().getWidthPoint(footer.getAdditionalText(), 7); ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(new Phrase(footer.getAdditionalText(), FontUtils.getDejavuRegular7Light()), document.left() + 240, textBase + 10, document.left() + 390, textBase - 25, 10, Element.ALIGN_LEFT); ct.go(); } catch (DocumentException e) { LOG.warn("Problem with additional text generation in report footer."); } try { ColumnText ct = new ColumnText(cb); ct.setSimpleColumn(document.left(), textBase + 10, document.left() + 230, textBase - 25, 10, Element.ALIGN_LEFT); ct.addText(new Phrase(footer.getCompanyName() + "\n", FontUtils.getDejavuRegular7Light())); if (!"".equals(footer.getAddress())) { ct.addText(new Phrase(footer.getAddress() + "\n", FontUtils.getDejavuRegular7Light())); } if (!"".equals(footer.getPhoneEmail())) { ct.addText(new Phrase(footer.getPhoneEmail(), FontUtils.getDejavuRegular7Light())); } ct.go(); } catch (DocumentException e) { LOG.warn("Problem with company text generation in report footer."); } cb.addTemplate(total, document.right() - adjust, textBase); cb.restoreState(); }
From source file:com.qcadoo.report.api.pdf.PdfPageNumbering.java
License:Open Source License
private void buildHeader(final PdfWriter writer, final Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState();//from w ww .j ava2s. c o m String text = footer.getPage() + " " + writer.getPageNumber() + " " + footer.getIn() + " "; float textBase = document.top() + 22; float textSize = FontUtils.getDejavu().getWidthPoint(text, 7); cb.setColorFill(ColorUtils.getLightColor()); cb.setColorStroke(ColorUtils.getLightColor()); cb.beginText(); cb.setFontAndSize(FontUtils.getDejavu(), 7); float adjust = FontUtils.getDejavu().getWidthPoint("0", 7); cb.setTextMatrix(document.right() - textSize - adjust, textBase); cb.showText(text); cb.endText(); cb.addTemplate(total, document.right() - adjust, textBase); cb.setLineWidth(1); cb.setLineDash(2, 2, 1); cb.moveTo(document.left(), document.top() + 12); cb.lineTo(document.right(), document.top() + 12); cb.stroke(); cb.restoreState(); }
From source file:de.tr1.cooperator.manager.web.CreateSubscriberListAction.java
License:Open Source License
/** * This method is called by the struts-framework if the submit-button is pressed. * It creates a PDF-File and puts in into the output-stream of the response. *//* w w w .j av a 2s . c o m*/ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { CreateSubscriberListForm myForm = (CreateSubscriberListForm) form; boolean bSortByName; if (myForm.getSortBy().equals(myForm.SORTBYPN)) bSortByName = false; else bSortByName = true; //create Collection for the results... Event eEvent = EventManager.getInstance().getEventByID(myForm.getEventID()); Collection cSubscriberList = UserManager.getInstance().getUsersByCollection(eEvent.getSubscriberList()); Collection cExamResults = EventResultManager.getInstance().getResults(eEvent.getID()); Iterator cSubscriberListIT = cSubscriberList.iterator(); Collection cSubscriberResultList = new ArrayList(); while (cSubscriberListIT.hasNext()) { User CurUser = (User) cSubscriberListIT.next(); String UserPNR = CurUser.getPersonalNumber(); Iterator cExamResultsIT = cExamResults.iterator(); ExamResult curExamResult = null; String ResultUserPNR = null; while (cExamResultsIT.hasNext()) { curExamResult = (ExamResult) cExamResultsIT.next(); ResultUserPNR = curExamResult.getUserPersonalNumber(); if (UserPNR.equals(ResultUserPNR)) break; } if (UserPNR.equals(ResultUserPNR)) { if (bSortByName) { UserResultSortByName URS = new UserResultSortByName(CurUser, "" + curExamResult.getResult()); cSubscriberResultList.add(URS); } else { UserResultSortByPersonalNumber URS = new UserResultSortByPersonalNumber(CurUser, "" + curExamResult.getResult()); cSubscriberResultList.add(URS); } } else { if (bSortByName) { UserResultSortByName URS = new UserResultSortByName(CurUser, "-"); cSubscriberResultList.add(URS); } else { UserResultSortByPersonalNumber URS = new UserResultSortByPersonalNumber(CurUser, "-"); cSubscriberResultList.add(URS); } } } //sort List Collections.sort((List) cSubscriberResultList); BaseFont bf; //36pt = 0.5inch Document document = new Document(PageSize.A4, 36, 36, 72, 72); try { bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); } catch (Exception e) { Log.addLog("CreateSubscriberListAction: Error creating BaseFont: " + e); //2do: add ErrorMessage and return to inputFormular! return mapping.findForward("GeneralFailure"); } //calculate the number of cols and their width int cols = 0; ArrayList alWidth = new ArrayList(); float boldItalicFactor = 1.2f; if (myForm.getShowNumber()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_NUMBER, TABLEHEADER_FONTSIZE))); if (myForm.getShowPersonalNumber()) alWidth.add(new Float( boldItalicFactor * bf.getWidthPoint(TABLEHEADER_PERSONALNUMBER, TABLEHEADER_FONTSIZE))); if (myForm.getShowName()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_NAME, TABLEHEADER_FONTSIZE))); if (myForm.getShowEmail()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_EMAIL, TABLEHEADER_FONTSIZE))); if (myForm.getShowResult()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_RESULT, TABLEHEADER_FONTSIZE))); if (myForm.getAddInfoField()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_INFO, TABLEHEADER_FONTSIZE))); if (myForm.getAddSignField()) alWidth.add(new Float(boldItalicFactor * bf.getWidthPoint(TABLEHEADER_SIGN, TABLEHEADER_FONTSIZE))); cols = alWidth.size(); float totalWidth = 0; //calculate the whole length Iterator alIterator = alWidth.iterator(); for (; alIterator.hasNext(); totalWidth += ((Float) alIterator.next()).floatValue()) ; //calculate relativ width for the table float[] width = new float[cols]; alIterator = alWidth.iterator(); int i = 0; while (alIterator.hasNext()) { float pixLength = ((Float) alIterator.next()).floatValue(); //alWidthRelativ.add( new Float( pixLength/totalWidth ) ); width[i] = pixLength / totalWidth; i++; } //needed for the shrink (enlarge?) float shrinkFactor; try { //1st: set correct outputstream PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream()); //1.5st: set content-stuff response.setContentType("application/pdf"); //2nd: set EventManager for PageEvents to Helper writer.setPageEvent( new CreateSubscriberListActionHelper(myForm.getHeaderLeft(), myForm.getHeaderRight())); //3rd: open document for editing the content document.open(); //4th: add content Phrase pInfoText = new Phrase(myForm.getInfoText(), new Font(bf, 12, Font.BOLD)); document.add(pInfoText); //PdfPTable( cols ) PdfPTable table = new PdfPTable(width); float documentWidth = document.right() - document.left(); if (documentWidth < totalWidth) { table.setTotalWidth(documentWidth); shrinkFactor = documentWidth / totalWidth; } else { table.setTotalWidth(totalWidth); shrinkFactor = 1; } table.setLockedWidth(true); Font headerFont = new Font(bf, TABLEHEADER_FONTSIZE * shrinkFactor, Font.BOLDITALIC); Font cellFont = new Font(bf, TABLECELL_FONTSIZE * shrinkFactor, Font.NORMAL); if (myForm.getShowNumber()) table.addCell(new Phrase(TABLEHEADER_NUMBER, headerFont)); if (myForm.getShowPersonalNumber()) table.addCell(new Phrase(TABLEHEADER_PERSONALNUMBER, headerFont)); if (myForm.getShowName()) table.addCell(new Phrase(TABLEHEADER_NAME, headerFont)); if (myForm.getShowEmail()) table.addCell(new Phrase(TABLEHEADER_EMAIL, headerFont)); if (myForm.getShowResult()) table.addCell(new Phrase(TABLEHEADER_RESULT, headerFont)); if (myForm.getAddInfoField()) table.addCell(new Phrase(TABLEHEADER_INFO, headerFont)); if (myForm.getAddSignField()) table.addCell(new Phrase(TABLEHEADER_SIGN, headerFont)); //fill table Iterator iSRL = cSubscriberResultList.iterator(); int counter = 1; while (iSRL.hasNext()) { UserResult curResult = (UserResult) iSRL.next(); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_RIGHT); if (myForm.getShowNumber()) table.addCell(new Phrase("" + counter++, cellFont)); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); if (myForm.getShowPersonalNumber()) table.addCell(new Phrase(curResult.getPersonalNumber(), cellFont)); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_LEFT); if (myForm.getShowName()) table.addCell(new Phrase(curResult.getSurname() + ", " + curResult.getFirstName(), cellFont)); if (myForm.getShowEmail()) table.addCell(new Phrase(curResult.getEmailAddress(), cellFont)); if (myForm.getShowResult()) table.addCell(new Phrase(curResult.getResult(), cellFont)); if (myForm.getAddInfoField()) table.addCell(new Phrase("", cellFont)); if (myForm.getAddSignField()) table.addCell(new Phrase("", cellFont)); } //set how many rows are header... table.setHeaderRows(1); document.add(table); //5th: close document, write the output to the stream... document.close(); } catch (Exception de) { Log.addLog("CreateSubscriberListAction: Error creating PDF: " + de); } //we dont need to return a forward, because we write directly to the outputstream! return null; }
From source file:de.tr1.cooperator.manager.web.CreateSubscriberListActionHelper.java
License:Open Source License
/** * After the content of the page is written, we put page X / Y * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, com.lowagie.text.Document) */// www . j a v a 2s .c om public void onEndPage(PdfWriter writer, Document document) { int pageN = writer.getPageNumber(); String text = "" + pageN; String separator = " / "; float text_len = bf.getWidthPoint(text, 8); float sep_len = bf.getWidthPoint(separator, 8); cb.beginText(); cb.setFontAndSize(bf, 8); float absMiddle = (document.right() - document.left()) / 2 + document.left(); cb.setTextMatrix(absMiddle - text_len - (sep_len / 2), 30); cb.showText(text); cb.setTextMatrix(absMiddle - (sep_len / 2), 30); cb.showText(separator); cb.endText(); cb.addTemplate(template, absMiddle + (sep_len / 2), 30); // write the headertable tHeader.setTotalWidth(document.right() - document.left()); tHeader.writeSelectedRows(0, -1, document.left(), document.getPageSize().height() - 20, cb); }
From source file:dinamica.PDFPageEvents.java
License:LGPL
/** * Create custom-made footer on every page, this * will place the empty template on every page *///from w w w . ja va 2 s .c o m public void onEndPage(PdfWriter writer, Document document) { // print (x of ...) on every page (bottom right) // append template at the end of the footer, like: // 1 of [template] - template is an empty box String footer = writer.getPageNumber() + this.pageXofY; float tWidth = bf.getWidthPoint(footer, 10); float extraSpace = bf.getWidthPoint("00", 10); float ty = document.bottom() - 12; //below bottom margin float tx = document.right() - tWidth - extraSpace; //x coordinate for the footer // print "X of " on right-side and footer + date on left side cb.beginText(); cb.setFontAndSize(bf, 10); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footer, tx, ty, 0); cb.showTextAligned(PdfContentByte.ALIGN_LEFT, footerLeft, document.left(), ty, 0); cb.endText(); // now append empty template after "X of " cb.addTemplate(tpl, document.right() - extraSpace, ty); }
From source file:domain.reports.menu.PDFReportMenu.java
License:LGPL
@Override protected void createPDF(GenericTransaction data, ByteArrayOutputStream buf) throws Throwable { //inicializar documento: tamano de pagina, orientacion, margenes Document doc = new Document(); PdfWriter docWriter = PdfWriter.getInstance(doc, buf); doc.setPageSize(PageSize.LETTER.rotate()); doc.setMargins(30, 30, 30, 40);/* www . ja v a 2 s .co m*/ doc.open(); //crear fonts por defecto tblHeaderFont = new Font(Font.HELVETICA, 10f, Font.BOLD); tblBodyFont = new Font(Font.HELVETICA, 10f, Font.NORMAL); //definir pie de pagina del lado izquierdo String footerText = this.getFooter(); //read it from config.xml or web.xml String reportDate = StringUtil.formatDate(new java.util.Date(), "dd-MM-yyyy HH:mm"); //crear template (objeto interno de IText) y manejador de evento //para imprimir el pie de pagina bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED); cb = docWriter.getDirectContent(); tpl = cb.createTemplate(20, 14); docWriter.setPageEvent(new PDFPageEvents(footerText, pageXofY, tpl, bf, cb, reportDate)); //titulo - lo lee de config.xml por defecto reportTitle = getReportTitle(); Paragraph t = new Paragraph(reportTitle, new Font(Font.HELVETICA, 14f, Font.BOLD)); t.setAlignment(Rectangle.ALIGN_RIGHT); doc.add(t); //logo img = Image.getInstance(getImage(this.getServerBaseURL() + logoPath, false)); img.scalePercent(100); float imgY = doc.top() - img.getHeight(); float imgX = doc.left(); img.setAbsolutePosition(imgX, imgY); doc.add(img); //blank line doc.add(new Paragraph(" ")); //blank line doc.add(new Paragraph(" ")); //blank line doc.add(new Paragraph(" ")); //for each master record print a master/detail section MasterDetailReader dataobj = (MasterDetailReader) data; Recordset master = dataobj.getRecordset("master"); master.top(); while (master.next()) { //blank line doc.add(new Paragraph(" ")); //print master section doc.add(getGroupMaster(master)); //print detail section doc.add(getGroupDetail(master, dataobj.getDetail(master))); } //print grand total doc.add(new Paragraph(" ")); doc.close(); docWriter.close(); }
From source file:fr.aliasource.webmail.server.export.ConversationPdfEventHandler.java
License:GNU General Public License
/** * @see com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter, * com.lowagie.text.Document)/*from ww w . j a va 2s . c o m*/ */ public void onEndPage(PdfWriter writer, Document document) { PdfContentByte cb = writer.getDirectContent(); cb.saveState(); // write the headertable table.setTotalWidth(document.right() - document.left()); table.writeSelectedRows(0, -1, document.left(), document.getPageSize().getHeight() - 20, cb); // compose the footer String text = writer.getPageNumber() + " / "; float textSize = helv.getWidthPoint(text, 12); float textBase = document.bottom() - 49; cb.beginText(); cb.setFontAndSize(helv, 12); float adjust = helv.getWidthPoint("0", 12); cb.setTextMatrix(document.right() - textSize - adjust - 5, textBase); cb.showText(text); cb.endText(); cb.addTemplate(tpl, document.right() - adjust, textBase); cb.saveState(); // draw a Rectangle around the page cb.setLineWidth(1); cb.rectangle(20, 20, document.getPageSize().getWidth() - 40, document.getPageSize().getHeight() - 40); cb.stroke(); cb.restoreState(); }
From source file:gov.noaa.pfel.coastwatch.sgt.SgtUtil.java
License:Open Source License
/** * This closes the pdf file created by createPDF, after you have * written things to g2D.//from w w w . j a v a 2s . co m * * @param oar the object[] returned from createPdf * @throwsException if trouble */ public static void closePdf(Object oar[]) throws Exception { Graphics2D g2D = (Graphics2D) oar[0]; Document document = (Document) oar[1]; PdfContentByte pdfContentByte = (PdfContentByte) oar[2]; PdfTemplate pdfTemplate = (PdfTemplate) oar[3]; g2D.dispose(); //center it if (verbose) String2.log("SgtUtil.closePdf" + " left=" + document.left() + " right=" + document.right() + " bottom=" + document.bottom() + " top=" + document.top() + " template.width=" + pdfTemplate.getWidth() + " template.height=" + pdfTemplate.getHeight()); //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f //x,y location in points 0.5f, 0, 0, 0.5f, document.left() + (document.right() - document.left() - pdfTemplate.getWidth() / 2) / 2, document.bottom() + (document.top() - document.bottom() - pdfTemplate.getHeight() / 2) / 2); /* //if boundingBox is small, center it //if boundingBox is large, shrink and center it //document.left/right/top/bottom include 1/2" margins float xScale = (document.right() - document.left()) / pdfTemplate.getWidth(); float yScale = (document.top() - document.bottom()) / pdfTemplate.getHeight(); float scale = Math.min(Math.min(xScale, yScale), 1); float xSize = pdfTemplate.getWidth() / scale; float ySize = pdfTemplate.getHeight() / scale; //x device = ax user + by user + e //y device = cx user + dy user + f pdfContentByte.addTemplate(pdfTemplate, //a,b,c,d,e,f scale, 0, 0, scale, document.left() + (document.right() - document.left() - xSize) / 2, document.bottom() + (document.top() - document.bottom() - ySize) / 2); */ document.close(); }
From source file:jm.web.Addons.java
License:GNU General Public License
public static void setEncabezado(PdfWriter writer, Document document, String texto) { if (writer.getPageNumber() > 1) { try {/*from w w w. j a v a 2s.co m*/ PdfPTable encabezado = new PdfPTable(1); encabezado.setTotalWidth(document.right() - document.left() - 120); encabezado.addCell(Addons.setCeldaPDF(texto, Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_LEFT, 0)); encabezado.writeSelectedRows(0, -1, 60, document.top() + 25, writer.getDirectContent()); PdfContentByte cb = writer.getDirectContent(); cb.setLineWidth(2); cb.moveTo(60, document.top() + 10); cb.lineTo(document.right() - document.left() - 58, document.top() + 10); } catch (Exception e) { throw new ExceptionConverter(e); } } }
From source file:jm.web.Addons.java
License:GNU General Public License
public static void setPie(PdfWriter writer, Document document, String rep_pie) { try {/*from www .ja v a 2 s .c o m*/ PdfContentByte cb = writer.getDirectContent(); /*cb.setLineWidth(2); cb.moveTo(60, document.bottomMargin()-5); cb.lineTo(document.right() - document.left()-70, document.bottomMargin()-5); */ PdfPTable pie = new PdfPTable(1); pie.setTotalWidth(document.right() - document.left() - 120); pie.addCell(Addons.setCeldaPDF(rep_pie, Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_CENTER, 0)); pie.addCell(Addons.setCeldaPDF("Pg " + String.valueOf(writer.getPageNumber()), Font.HELVETICA, 9, Font.BOLD, Element.ALIGN_RIGHT, 0)); pie.addCell(Addons.setCeldaPDF( "Reporte diseado por: Jorge Mueses Cevallos. Mvil: 095204832 mail:jorge_mueses@yahoo.com", Font.HELVETICA, 5, Font.BOLD, Element.ALIGN_LEFT, 0)); pie.writeSelectedRows(0, -1, 60, document.bottomMargin() - 10, cb); } catch (Exception e) { throw new ExceptionConverter(e); } }