List of usage examples for com.lowagie.text PageSize A4
Rectangle A4
To view the source code for com.lowagie.text PageSize A4.
Click Source Link
From source file:wagwaan.reports.UpdatingStockCountPdf.java
public void generatePdf() { try {//from w ww . j av a 2s.c o m java.io.File tempFile = java.io.File.createTempFile("REP" + label.getDateLable() + "_", ".pdf"); tempFile.deleteOnExit(); java.lang.Runtime rt = java.lang.Runtime.getRuntime(); java.lang.String debitTotal = null; java.lang.String creditTotal = null; com.lowagie.text.Document docPdf = new com.lowagie.text.Document(PageSize.A4.rotate()); try { try { com.lowagie.text.pdf.PdfWriter.getInstance(docPdf, new java.io.FileOutputStream(tempFile)); String date = null; try { java.sql.Statement st4 = connectDB.createStatement(); java.sql.ResultSet rset4 = st4.executeQuery("SELECT date(now()) as Date"); while (rset4.next()) date = rset4.getObject(1).toString(); // com.lowagie.text.HeaderFooter headerFoter = new com.lowagie.text.HeaderFooter(new Phrase(""+compName+""),false);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 14, Font.BOLDITALIC,java.awt.Color.blue))); // headerFoter.setAlignment(com.lowagie.text.HeaderFooter.ALIGN_CENTER); // headerFoter.setRight(5); // docPdf.setHeader(headerFoter); } catch (java.sql.SQLException SqlExec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), SqlExec.getMessage()); } com.lowagie.text.HeaderFooter footer = new com.lowagie.text.HeaderFooter( new Phrase("Updating Stock Count - Page: "), true);// FontFactory.getFont(com.lowagie.text.FontFactory.HELVETICA, 12, Font.BOLDITALIC,java.awt.Color.blue)); docPdf.setFooter(footer); docPdf.open(); ReportUtil.addCenteredTitlePage(docPdf, connectDB); try { com.lowagie.text.pdf.PdfPTable table = new com.lowagie.text.pdf.PdfPTable(12); int headerwidths[] = { 6, 9, 8, 8, 10, 8, 9, 9, 9, 9, 9, 9 }; table.setWidths(headerwidths); table.setWidthPercentage((100)); table.getDefaultCell().setBorder(Rectangle.BOTTOM); table.getDefaultCell().setColspan(6); Phrase phrase; java.text.DateFormat dateFormat = java.text.DateFormat .getDateInstance(java.text.DateFormat.MEDIUM); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase("Updating Stock Count"); table.addCell(phrase); table.getDefaultCell().setColspan(6); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT); phrase = new Phrase("Printed On :" + date, pFontHeader); table.addCell(phrase); String receive_nuos = null; Statement sta = connectDB.createStatement(); ResultSet rst = sta .executeQuery("select receiving_no from st_receiving_issuing where receiving_no='" + receive + "'"); while (rst.next()) { receive_nuos = rst.getString(1); } table.getDefaultCell().setColspan(12); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(receive_nuos, pFontHeader); table.addCell(phrase); table.getDefaultCell().setColspan(1); table.getDefaultCell() .setBorder(PdfCell.TOP | PdfCell.BOTTOM | PdfCell.LEFT | PdfCell.RIGHT); phrase = new Phrase("Count", pFontHeader); table.addCell(phrase); phrase = new Phrase("Item Code", pFontHeader); table.addCell(phrase); phrase = new Phrase("Item Name", pFontHeader); table.addCell(phrase); phrase = new Phrase("Qty ordered", pFontHeader); table.addCell(phrase); phrase = new Phrase("Qty_received", pFontHeader); table.addCell(phrase); phrase = new Phrase("Deficit", pFontHeader); table.addCell(phrase); phrase = new Phrase("Price", pFontHeader); table.addCell(phrase); phrase = new Phrase("Total", pFontHeader); table.addCell(phrase); phrase = new Phrase("Supplier", pFontHeader); table.addCell(phrase); phrase = new Phrase("Delivery Date", pFontHeader); table.addCell(phrase); phrase = new Phrase("Order ID", pFontHeader); table.addCell(phrase); phrase = new Phrase("Fully Delivered", pFontHeader); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT); table.getDefaultCell().setBackgroundColor(java.awt.Color.WHITE); try { Statement s = connectDB.createStatement(); ResultSet r = s.executeQuery( "SELECT sum(quantity_received*o.price) from stock_orders o,st_receiving_issuing t,stock_items s,stock_suppliers st\n" + "where t.item_code=o.item_code and t.order_id=o.order_id and t.item_code=s.item_code and st.supplier_id=t.supplier_id and receiving_no='" + receive + "'"); java.sql.Statement st = connectDB.createStatement(); java.sql.ResultSet rset = st.executeQuery( "SELECT t.item_code,s.item_name, quantity_ordered,sum(quantity_received) , " + "(quantity_ordered-sum(quantity_received)) as def, o.price , (o.price*sum(quantity_received)) as total, st.supplier_names , receiving_date, \n" + "\n" + "t.order_id , delivered from stock_orders o,st_receiving_issuing t,stock_items s,stock_suppliers st where t.item_code=o.item_code " + "and t.order_id=o.order_id and t.item_code=s.item_code and st.supplier_id=t.supplier_id AND RECEIVING_NO='" + receive + "' group by 1,2,3,6, 8,9, 10, 11"); int count = 1; while (rset.next()) { table.getDefaultCell().setColspan(1); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_CENTER); table.getDefaultCell().setColspan(1); numberSeq += 1; phrase = new Phrase("" + count + " ", pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(1), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(2), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(3), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(4), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(5), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(6), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(7), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(8), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(9), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(10), "-"), pFontHeader1); table.addCell(phrase); table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_LEFT); phrase = new Phrase(dbObject.getDBObject(rset.getObject(11), "-"), pFontHeader1); table.addCell(phrase); count++; } table.getDefaultCell().setColspan(12); phrase = new Phrase(" "); table.addCell(phrase); String gt = null; while (r.next()) { gt = dbObject.getDBObject(r.getObject(1), "-"); } table.getDefaultCell().setHorizontalAlignment(PdfCell.ALIGN_RIGHT); phrase = new Phrase("GRAND TOTAL: " + gt, pFontHeader1); table.addCell(phrase); System.err.println("grand total"); docPdf.add(table); } catch (java.sql.SQLException SqlExec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), SqlExec.getMessage()); } } catch (com.lowagie.text.BadElementException BadElExec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), BadElExec.getMessage()); } catch (SQLException ex) { Logger.getLogger(OrdersPdf.class.getName()).log(Level.SEVERE, null, ex); } } catch (java.io.FileNotFoundException fnfExec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), fnfExec.getMessage()); } catch (SQLException ex) { Logger.getLogger(UpdatingStockCountPdf.class.getName()).log(Level.SEVERE, null, ex); } catch (BadElementException ex) { Logger.getLogger(UpdatingStockCountPdf.class.getName()).log(Level.SEVERE, null, ex); } } catch (com.lowagie.text.DocumentException lwDocexec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), lwDocexec.getMessage()); } docPdf.close(); deskTop.open(tempFile); } catch (java.io.IOException IOexec) { javax.swing.JOptionPane.showMessageDialog(new javax.swing.JFrame(), IOexec.getMessage()); } }
From source file:webBoltOns.server.servletUtil.Text2PDF.java
License:Mozilla Public License
public static void convert2PDFfile(String inputTextFile, String outputPDFfile) throws Exception { BufferedReader r = new BufferedReader(new FileReader(inputTextFile)); Document document = new Document(PageSize.A4.rotate()); PdfWriter.getInstance(document, new FileOutputStream(outputPDFfile)); document.open();/*from w w w. ja v a 2s . c o m*/ Font courier10pt = FontFactory.getFont(FontFactory.COURIER, 10); String str; while ((str = r.readLine()) != null) { document.add(new Paragraph(str.equals("") ? " " : str, courier10pt)); } document.close(); r.close(); }
From source file:za.co.equalpay.web.utils.PDFExportUtility.java
/** * Perform the standard PDF PreProcessing: <br> * Add Customer logo image and Phrase as header to the first page, <br> * Add Line Separator to the bottom of the Image <br> * and add the standard footer message to the PDF document<br> * * @throws MalformedURLException/*from w ww . j a v a2 s . c om*/ * @throws IOException * @throws DocumentException */ public void preProcess() throws MalformedURLException, IOException, DocumentException { document.setMargins(50f, 50f, 10f, 20f); BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false); // Font font = new Font(bf_helv, 8); ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext() .getContext(); String fontPath = LogoPathFinder.getFontPath(servletContext, "Tahoma"); BaseFont bf = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, true); Font tahoma = new Font(bf, 16, Font.BOLD); tahoma.setColor(Color.GRAY); Font fontBold = new Font(bf, 8, Font.BOLD); fontBold.setColor(Color.GRAY); Font f = new Font(bf, 8, Font.NORMAL); f.setColor(Color.GRAY); Font sf = new Font(bf, 6, Font.NORMAL); sf.setColor(Color.LIGHT_GRAY); // image.setIndentationLeft(360f); // PdfPCell imgCell = new PdfPCell(image, false); // imgCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); // imgCell.setBorder(0); // // PdfPCell emptyCell = new PdfPCell(new Phrase("TESTING...", tahoma)); // emptyCell.setBorder(0); // PdfPTable footerTable = new PdfPTable(1); // footerTable.setWidthPercentage(100); // // // phrase = new Phrase(PDF_FOOTER_MESSAGE, f); // // PdfPCell cell = new PdfPCell(phrase); // cell.setBorder(0); // footerTable.addCell(cell); // table.setWidths(new int[] { 1, 2 }); // table.addCell(emptyCell); // footerTable.addCell(emptyCell); // phrase.add(footerTable); // phrase.add(new Chunk(image, 475f, 0)); // Phrase subPhrase = new Phrase(); // subPhrase.add(new Phrase("\nwww.meddev.co.za\n", sf)); // subPhrase.add(new Phrase(PDF_FOOTER_MESSAGE, f)); // phrase.add(subPhrase); // phrase.add(new Chunk("www.meddev.co.za", f)); // phrase.add(new Phrase(PDF_FOOTER_MESSAGE, f)); // load document footer HeaderFooter footer = new HeaderFooter(new Phrase(PDF_FOOTER_MESSAGE, f), false); // HeaderFooter footer = new HeaderFooter(paragraph, false); // HeaderFooter footer = new HeaderFooter(phrase, false); footer.setAlignment(Element.ALIGN_LEFT); footer.setBorder(Rectangle.NO_BORDER); document.setFooter(footer); // document.open(); // // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // PdfWriter writer = PdfWriter.getInstance(document, baos); // // writer.open(); // PdfContentByte cb = writer.getDirectContent(); // cb.addImage(image); // ColumnText columnText = new ColumnText(cb); // load the customer logo String logoPath = LogoPathFinder.getPath(servletContext, "meddev-logo2"); Image image = Image.getInstance(logoPath); image.scaleToFit(149, 55); image.setIndentationLeft(360f); PdfPCell imageCell = new PdfPCell(image, false); imageCell.setHorizontalAlignment(PdfPCell.ALIGN_RIGHT); imageCell.setBorder(0); PdfPCell headerTextCell = new PdfPCell(new Phrase(header, tahoma)); headerTextCell.setBorder(0); PdfPTable table = new PdfPTable(2); table.setWidthPercentage(100); table.setWidths(new int[] { 1, 2 }); table.addCell(headerTextCell); table.addCell(imageCell); phrase = new Phrase(); phrase.add(table); // load document header HeaderFooter header = new HeaderFooter(phrase, false); header.setAlignment(Element.ALIGN_CENTER); header.setBorder(Rectangle.NO_BORDER); document.setHeader(header); // create gray line separator // Chunk lineSeparator = new Chunk(new LineSeparator(1, 100, Color.GRAY, // Element.ALIGN_CENTER, -2)); // document.add(lineSeparator); Font headerFont = new Font(bf_helv, 9); // write the header lines for this statement phrase = new Phrase(12, "\n", headerFont); phrase.add("\n\n"); // open the pdf document for editing document.open(); document.setPageSize(PageSize.A4); // Meddev Information Block table = new PdfPTable(4); table.setWidthPercentage(100); // table.setWidths(new int[] { 25, 25, 30, 20 }); Phrase phrase1 = new Phrase(); phrase1.add(new Phrase("MED DEV cc", fontBold)); phrase1.add(new Phrase("\nUNIT 10, THE CORNER", f)); phrase1.add(new Phrase("\nc/o Theuns & Hilde Ave", f)); phrase1.add(new Phrase("\nHennopspark, 0157", f)); phrase1.add(new Phrase("\nTel: +27 (0) 12 653 3063", f)); phrase1.add(new Phrase("\nReg No: 2006/166603/23", f)); phrase1.add(new Phrase("\n", f)); Phrase phrase2 = new Phrase(); phrase2.add(new Phrase("VAT No: ", fontBold)); phrase2.add(new Phrase("4150231498", f)); phrase2.add(new Phrase("\nImport/Export #: ", fontBold)); phrase2.add(new Phrase("20544748", f)); phrase2.add(new Phrase("\n", f)); Phrase phrase3 = new Phrase(); phrase3.add(new Phrase("QUOTE NO ", fontBold)); phrase3.add(new Phrase("\nDATE ", fontBold)); phrase3.add(new Phrase("\nREFERNCE ", fontBold)); phrase3.add(new Phrase("\nSUPPLIER CODE ", fontBold)); phrase3.add(new Phrase("\nEXPIRY DATE ", fontBold)); Phrase phrase4 = new Phrase(); // phrase4.add(new Phrase(quotation.getQuotationNumber(), f)); // phrase4.add(new Phrase("\n" + DateFormatter.formatMonthDate(quotation.getQuotationDate()), f)); // phrase4.add(new Phrase("\n" + (quotation.getClient().getReference() == null ? "-" : quotation.getClient().getReference()), f)); // phrase4.add(new Phrase("\nMEDDEV1", f)); // phrase4.add(new Phrase("\n" + DateFormatter.formatMonthDate(quotation.calculateExpiryDate()), f)); PdfPCell cell1 = new PdfPCell(phrase1); cell1.setBorder(0); PdfPCell cell2 = new PdfPCell(phrase2); cell2.setBorder(0); PdfPCell cell3 = new PdfPCell(phrase3); cell3.setBorder(0); PdfPCell cell4 = new PdfPCell(phrase4); cell4.setBorder(0); table.addCell(cell1); table.addCell(cell2); table.addCell(cell3); table.addCell(cell4); phrase.add(table); // create gray line separator Chunk lineSeparator = new Chunk(new LineSeparator(1, 100, Color.GRAY, Element.ALIGN_CENTER, -2)); phrase.add(lineSeparator); // Customer Information Block table = new PdfPTable(3); table.setWidthPercentage(100); table.setWidths(new int[] { 5, 2, 3 }); // Phrase phrase11 = new Phrase(); // phrase11.add(new Phrase(quotation.getClient().getCompany().toUpperCase() + "(CLIENT)", fontBold)); // if (quotation.getClient().getVatRegNo() != null && quotation.getClient().getVatRegNo().trim().length() > 0) { // phrase11.add(new Phrase("\nCustomer VAT No: ", fontBold)); // phrase11.add(new Phrase(quotation.getClient().getVatRegNo(), f)); // } // if (quotation.getClient().getResAddress1() != null && quotation.getClient().getResAddress1().trim().length() > 0) { // phrase11.add(new Phrase("\n" + quotation.getClient().getResAddress1(), f)); // } // if (quotation.getClient().getResAddress2() != null && quotation.getClient().getResAddress2().trim().length() > 0) { // phrase11.add(new Phrase("\n" + quotation.getClient().getResAddress2(), f)); // } // if (quotation.getClient().getResCity() != null && quotation.getClient().getResCity().trim().length() > 0) { // phrase11.add(new Phrase("\n" + quotation.getClient().getResCity(), f)); // } // if (quotation.getClient().getPhone() != null && quotation.getClient().getPhone().trim().length() > 0) { // phrase11.add(new Phrase("\nTel: " + quotation.getClient().getPhone(), f)); // } //PdfPCell cell11 = new PdfPCell(phrase11); //cell11.setBorder(0); phrase2 = new Phrase(); phrase2.add(new Phrase("CURRENCY: ", fontBold)); PdfPCell cell12 = new PdfPCell(phrase2); cell12.setBorder(0); phrase3 = new Phrase(); //phrase3.add(new Phrase(quotation.getClient().getCurrencyCode(), f)); PdfPCell cell13 = new PdfPCell(phrase3); cell13.setBorder(0); // table.addCell(cell11); table.addCell(cell12); table.addCell(cell13); phrase.add(table); // create gray line separator phrase.add("\n"); document.add(phrase); String footerImagePath = LogoPathFinder.getPath(servletContext, "footer-image"); Image footerImage = Image.getInstance(footerImagePath); footerImage.scaleToFit(90, 50); footerImage.setAbsolutePosition((PageSize.A4.getWidth() - (footerImage.getScaledWidth() + 40)), (PageSize.A4.getHeight() - (PageSize.A4.getHeight() - (footerImage.getScaledHeight() - 10)))); document.add(footerImage); }