List of usage examples for com.itextpdf.text Font Font
public Font(final BaseFont bf, final float size, final int style, final BaseColor color)
From source file:org.jfree.chart.swt.ChartPdf.java
License:Open Source License
public static void saveChartAsPDF(File file, JFreeChart chart, int width, int height) throws DocumentException, FileNotFoundException, IOException { if (chart != null) { boolean success = false; String old = null;/*from w ww.j av a 2 s.c o m*/ File oldFile = null; boolean append = file.exists(); if (append) { old = file.getAbsolutePath() + ".old"; //$NON-NLS-1$ oldFile = new File(old); oldFile.delete(); file.renameTo(oldFile); } try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file))) { // convert chart to PDF with iText: Rectangle pagesize = new Rectangle(width, height); if (append) { PdfReader reader = new PdfReader(old); PdfStamper stamper = new PdfStamper(reader, out); try { int n = reader.getNumberOfPages() + 1; stamper.insertPage(n, pagesize); PdfContentByte overContent = stamper.getOverContent(n); writeChart(chart, width, height, overContent); ColumnText ct = new ColumnText(overContent); ct.setSimpleColumn(width - 50, 50, width - 12, height, 150, Element.ALIGN_RIGHT); Paragraph paragraph = new Paragraph(String.valueOf(n), new Font(FontFamily.HELVETICA, 9, Font.NORMAL, BaseColor.DARK_GRAY)); paragraph.setAlignment(Element.ALIGN_RIGHT); ct.addElement(paragraph); ct.go(); success = true; } finally { stamper.close(); reader.close(); oldFile.delete(); } } else { Document document = new Document(pagesize, 50, 50, 50, 50); document.addCreationDate(); document.addCreator(Constants.APPLICATION_NAME); document.addAuthor(System.getProperty("user.name")); //$NON-NLS-1$ try { PdfWriter writer = PdfWriter.getInstance(document, out); document.open(); writeChart(chart, width, height, writer.getDirectContent()); success = true; } finally { document.close(); } } } if (!success) { file.delete(); if (oldFile != null) oldFile.renameTo(file); } } }
From source file:org.restate.project.controller.PaymentReceiptController.java
License:Open Source License
@RequestMapping(method = RequestMethod.GET, value = "receipt.print") public void downloadDocument(HttpServletResponse response, @RequestParam(value = "id", required = false) Integer id) throws Exception { if (id == null) { return;/*from www. j a va 2 s. c o m*/ } // creation of the document with a certain size and certain margins // may want to use PageSize.LETTER instead // Document document = new Document(PageSize.A4, 50, 50, 50, 50); Document doc = new Document(); PdfWriter docWriter = null; DecimalFormat df = new DecimalFormat("0.00"); File tmpFile = File.createTempFile("paymentReceipt", ".pdf"); try { //special font sizes Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0)); Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12); //file path docWriter = PdfWriter.getInstance(doc, new FileOutputStream(tmpFile)); //document header attributes doc.addAuthor("betterThanZero"); doc.addCreationDate(); doc.addProducer(); doc.addCreator("MySampleCode.com"); doc.addTitle("Report with Column Headings"); doc.setPageSize(PageSize.LETTER); //open document doc.open(); //create a paragraph Paragraph paragraph = new Paragraph("iText is a library that allows you to create and " + "manipulate PDF documents. It enables developers looking to enhance web and other " + "applications with dynamic PDF document generation and/or manipulation."); //specify column widths float[] columnWidths = { 1.5f, 2f, 5f, 2f }; //create PDF table with the given widths PdfPTable table = new PdfPTable(columnWidths); // set table width a percentage of the page width table.setWidthPercentage(90f); //insert column headings insertCell(table, "Order No", Element.ALIGN_RIGHT, 1, bfBold12); insertCell(table, "Account No", Element.ALIGN_LEFT, 1, bfBold12); insertCell(table, "Account Name", Element.ALIGN_LEFT, 1, bfBold12); insertCell(table, "Order Total", Element.ALIGN_RIGHT, 1, bfBold12); table.setHeaderRows(1); //insert an empty row insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12); //create section heading by cell merging insertCell(table, "New York Orders ...", Element.ALIGN_LEFT, 4, bfBold12); double orderTotal, total = 0; //just some random data to fill for (int x = 1; x < 5; x++) { insertCell(table, "10010" + x, Element.ALIGN_RIGHT, 1, bf12); insertCell(table, "ABC00" + x, Element.ALIGN_LEFT, 1, bf12); insertCell(table, "This is Customer Number ABC00" + x, Element.ALIGN_LEFT, 1, bf12); orderTotal = Double.valueOf(df.format(Math.random() * 1000)); total = total + orderTotal; insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12); } //merge the cells to create a footer for that section insertCell(table, "New York Total...", Element.ALIGN_RIGHT, 3, bfBold12); insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12); //repeat the same as above to display another location insertCell(table, "", Element.ALIGN_LEFT, 4, bfBold12); insertCell(table, "California Orders ...", Element.ALIGN_LEFT, 4, bfBold12); orderTotal = 0; for (int x = 1; x < 7; x++) { insertCell(table, "20020" + x, Element.ALIGN_RIGHT, 1, bf12); insertCell(table, "XYZ00" + x, Element.ALIGN_LEFT, 1, bf12); insertCell(table, "This is Customer Number XYZ00" + x, Element.ALIGN_LEFT, 1, bf12); orderTotal = Double.valueOf(df.format(Math.random() * 1000)); total = total + orderTotal; insertCell(table, df.format(orderTotal), Element.ALIGN_RIGHT, 1, bf12); } insertCell(table, "California Total...", Element.ALIGN_RIGHT, 3, bfBold12); insertCell(table, df.format(total), Element.ALIGN_RIGHT, 1, bfBold12); //add the PDF table to the paragraph paragraph.add(table); // add the paragraph to the document doc.add(paragraph); } catch (DocumentException dex) { dex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (doc != null) { //close the document doc.close(); } if (docWriter != null) { //close the writer docWriter.close(); } response.setHeader("Content-disposition", "attachment; filename=" + "sampleDoc" + ".pdf"); response.setContentType("application/pdf"); OutputStream outputStream = response.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(tmpFile); IOUtils.copy(fileInputStream, outputStream); fileInputStream.close(); outputStream.flush(); tmpFile.delete(); } }
From source file:pdf.PdfCreator.java
public String createPdf(String path, String ingredients, String recipeTitle, String recipeDescription) throws DocumentException, IOException { path += "shoppingList.pdf"; Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(path)); document.open();/* ww w .j ava2 s .c o m*/ String[] ings = ingredients.split(";"); PdfContentByte cb = writer.getDirectContent(); BaseFont bf = BaseFont.createFont(); cb.beginText(); Paragraph p = new Paragraph("Ingredients you'll need to buy:", new Font(FontFamily.HELVETICA, 20, Font.BOLD, new BaseColor(38, 165, 154))); p.add(Chunk.NEWLINE); document.add(p); cb.moveText(36, 750); cb.setFontAndSize(bf, 15); cb.endText(); p = new Paragraph(); for (String str : ings) { p.add(" "); p.add(str); p.add(Chunk.NEWLINE); } p.add(Chunk.NEWLINE); p.add(Chunk.NEWLINE); document.add(p); p = new Paragraph(recipeTitle, new Font(FontFamily.HELVETICA, 20, Font.BOLD, new BaseColor(38, 165, 154))); p.add(Chunk.NEWLINE); document.add(p); p = new Paragraph(recipeDescription, new Font(bf, 12)); p.add(Chunk.NEWLINE); document.add(p); document.close(); return path; }
From source file:pdf.PDFDesign.java
public PDFDesign(String s, String c, String sn, Map<String, String> fontMap) { size = s;//w w w.j a v a 2 s. c om titleLineFixedHeight = 15.0f; priceFontInc = 0; textFontInc = 0; styleName = sn; fontPaths = new FontPaths(fontMap); nColumns = 6; switch (c) { case "black": color = BaseColor.BLACK; break; case "gray": color = BaseColor.DARK_GRAY; break; case "red": color = BaseColor.RED.darker(); break; case "green": color = BaseColor.GREEN.darker().darker().darker(); break; case "blue": color = BaseColor.BLUE.darker().darker().darker(); break; default: this.color = BaseColor.BLACK; } switch (size) { case "3x5": cellHeight = 83.8f; break; case "3.5x5": cellHeight = 97.8f; break; case "3.5x6": priceFontInc = 8; cellHeight = 97.8f; nColumns = 5; break; case "3.5x7": cellHeight = 97.8f; nColumns = 4; break; case "4x6": titleLineFixedHeight = 26.0f; priceFontInc = 8; cellHeight = 117.0f; nColumns = 5; break; case "4x7": titleLineFixedHeight = 26.0f; priceFontInc = 8; cellHeight = 117.0f; nColumns = 4; break; case "5x6": titleLineFixedHeight = 30.0f; priceFontInc = 13; textFontInc = 2; cellHeight = 145.0f; nColumns = 5; break; case "5x7": titleLineFixedHeight = 30.0f; priceFontInc = 18; textFontInc = 3; cellHeight = 145.0f; nColumns = 4; break; case "6x7": titleLineFixedHeight = 34.0f; priceFontInc = 26; textFontInc = 5; cellHeight = 175.0f; nColumns = 4; break; } try { int normal = Font.NORMAL; int bold = Font.BOLD; droidsans = BaseFont.createFont(fontPaths.getPath("droidsans"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); BaseFont impact = BaseFont.createFont(fontPaths.getPath("impact"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); switch (styleName) { case "style1": BaseFont courier = BaseFont.createFont(fontPaths.getPath("courier"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(42f, new Font(courier, 12 + textFontInc, normal, color), new Font(courier, 9 + textFontInc, normal, color), new Font(impact, 43 + priceFontInc + textFontInc, normal, color), new Font(courier, 20 + textFontInc, normal, color), new Font(courier, 8 + textFontInc, normal, color)); break; case "style2": BaseFont digital7 = BaseFont.createFont(fontPaths.getPath("digital7"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(42f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(digital7, 55 + priceFontInc + textFontInc, normal, color), new Font(digital7, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style3": BaseFont modern = BaseFont.createFont(fontPaths.getPath("modern"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(modern, 55 + priceFontInc + textFontInc, normal, color), new Font(modern, 30, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style4": BaseFont gothic = BaseFont.createFont(fontPaths.getPath("gothic"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(40f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(gothic, 48 + priceFontInc + textFontInc, bold, color), new Font(gothic, 30, bold, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style5": BaseFont bookman = BaseFont.createFont(fontPaths.getPath("bookman"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED); initValues(35f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(bookman, 45 + priceFontInc + textFontInc, bold, color), new Font(bookman, 25, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; case "style6": initValues(45f, new Font(droidsans, 10 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color), new Font(impact, 45 + priceFontInc + textFontInc, normal, color), new Font(droidsans, 20 + textFontInc, normal, color), new Font(droidsans, 7 + textFontInc, normal, color)); break; default: System.out.println("[*] Unknown Style"); break; } } catch (DocumentException | IOException ex) { System.out.println("[E] " + ex.getMessage()); } }
From source file:pdf.PDFDesign.java
private PdfPCell createEuroCell(Item item) throws DocumentException, IOException { Font descFont = new Font(droidsans, 12, Font.NORMAL, color); Font unitPriceFont = new Font(droidsans, 10, Font.NORMAL, color); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100f);// w w w . j a va 2 s.com PdfPCell cell; Paragraph p; cell = new PdfPCell(); p = new Paragraph(); p.setFont(descFont); p.add(item.getTitle()); cell.setFixedHeight(28f); cell.setPhrase(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBorder(0); table.addCell(cell); cell = new PdfPCell(); p = new Paragraph(); p.setFont(descFont); p.add(item.getAmount() + item.getUnit()); cell.setFixedHeight(16f); cell.setPhrase(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBorder(0); table.addCell(cell); table.addCell(createEuroInnerPrice(item)); cell = new PdfPCell(); p = new Paragraph(); p.setFont(unitPriceFont); p.add("Cena za 1" + item.getXUnit() + ": " + item.getUnitPrice() + item.getCurrency() + "/" + item.getSecondUnitPrice() + item.getSecondCurrency()); cell.setPhrase(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBorder(0); cell.setLeading(2f, 0f); table.addCell(cell); PdfPCell finalCell = new PdfPCell(table); finalCell.setFixedHeight(117.4f); finalCell.setBorderColor(BaseColor.GRAY); return finalCell; }
From source file:pdf.PDFDesign.java
private PdfPCell createEuroInnerInnerPrice(String price, String curr, boolean isFirst) throws DocumentException { Font bigPriceFont = new Font(droidsans, 40, Font.BOLD, color); Font smallPriceFont = new Font(droidsans, 18, Font.NORMAL, color); Font unitFont = new Font(droidsans, 12, Font.NORMAL, color); PdfPTable table = new PdfPTable(2); table.setWidths(new float[] { 3f, 1f }); PdfPCell cell;/*from www.j a va2 s.c o m*/ Paragraph para; para = new Paragraph(""); para.setFont(bigPriceFont); if (price.length() >= 4) { para.add(price.substring(0, price.length() - 3)); } cell = new PdfPCell(para); cell.setRowspan(2); cell.setFixedHeight(57f); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setBorder(0); table.addCell(cell); para = new Paragraph(""); para.setFont(smallPriceFont); if (price.length() >= 4) { para.add(price.substring(price.length() - 2, price.length())); } cell = new PdfPCell(para); cell.setRowspan(1); cell.setFixedHeight(35f); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setBorder(0); table.addCell(cell); para = new Paragraph(); para.setFont(unitFont); para.add(curr); cell = new PdfPCell(para); cell.setRowspan(1); cell.setFixedHeight(22f); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorder(0); table.addCell(cell); PdfPCell finalCell = new PdfPCell(table); finalCell.setBorder(0); /*if (isFirst) { finalCell.setBorder(Rectangle.RIGHT); }*/ return finalCell; }
From source file:pdf.PDFDesign.java
private PdfPCell createEuro_6_Cell(Item item) throws DocumentException { PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100f);// www .java2 s.c o m PdfPCell cell; Paragraph p; cell = new PdfPCell(); p = new Paragraph(); p.setFont(new Font(droidsans, 13, Font.NORMAL, color)); String t = item.getTitle(); String[] split = t.split("//"); for (String split1 : split) { p.add(split1 + "\n"); } cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setPhrase(p); cell.setColspan(2); cell.setFixedHeight(30f); cell.setBorder(0); table.addCell(cell); cell = new PdfPCell(); p = new Paragraph(); p.setFont(new Font(droidsans, 11, Font.NORMAL, color)); p.add(item.getAmount() + "" + item.getUnit()); cell.setPhrase(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setBorder(0); table.addCell(cell); table.addCell(createEuro_6_InnerPrice(item)); cell = new PdfPCell(); p = new Paragraph(); p.setFont(new Font(droidsans, 11, Font.NORMAL, color)); p.add("Cena za 1" + item.getXUnit() + ": " + item.getUnitPrice() + item.getCurrency() + "/" + item.getSecondUnitPrice() + item.getSecondCurrency()); cell.setPhrase(p); cell.setHorizontalAlignment(Element.ALIGN_CENTER); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setBorder(0); table.addCell(cell); PdfPCell finalCell = new PdfPCell(table); finalCell.setFixedHeight(cellHeight);//175f finalCell.setBorderColor(BaseColor.GRAY); return finalCell; }
From source file:pdf.PDFDesign.java
private PdfPCell createEuro_6_InnerInnerPrice(String price, String curr, boolean isFirst) throws DocumentException { PdfPTable table = new PdfPTable(2); table.setWidths(new float[] { 3f, 1.2f }); PdfPCell cell;/*from w w w. j a v a 2s . com*/ Paragraph para; para = new Paragraph(""); para.setFont(new Font(droidsans, 60, Font.BOLD, color)); if (price.length() >= 4) { para.add(price.substring(0, price.length() - 3)); } cell = new PdfPCell(para); cell.setRowspan(2); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); cell.setHorizontalAlignment(Element.ALIGN_RIGHT); cell.setLeading(50f, 0f); cell.setBorder(0); table.addCell(cell); para = new Paragraph(""); para.setFont(new Font(droidsans, 28, Font.NORMAL, color)); if (price.length() >= 4) { para.add(price.substring(price.length() - 2, price.length())); } cell = new PdfPCell(para); cell.setRowspan(1); cell.setFixedHeight(32f); cell.setVerticalAlignment(Element.ALIGN_BOTTOM); cell.setBorder(0); table.addCell(cell); para = new Paragraph(); para.setFont(new Font(droidsans, 16, Font.NORMAL, color)); para.add(curr); cell = new PdfPCell(para); cell.setRowspan(1); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorder(0); table.addCell(cell); PdfPCell finalCell = new PdfPCell(table); finalCell.setBorder(0); /*if (isFirst) { finalCell.setBorder(Rectangle.RIGHT); }*/ return finalCell; }
From source file:printom.PDFCreator.java
public static void createLabel(int aLabelType, int aJobNum, char aJobIdentifier, String aCustName, String aItemName, String aItemCode, String aDate, String aPOrderNum, int aInputPcs) { String myJobNum = String.valueOf(aJobNum); char myJobIdentifier = aJobIdentifier; String myCustName = aCustName; String myItemName = aItemName; String myItemCode = aItemCode; String myDate = aDate;/*from www.j a v a 2 s . co m*/ String myPOrderNum = aPOrderNum; String myInputPcs = String.valueOf(aInputPcs); try { String src = ""; if (aLabelType == 1) { src = CTNLABEL; } String dest = RESULTLABEL; Font timesJob = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.BOLD, BaseColor.WHITE); Font timesDef = new Font(Font.FontFamily.TIMES_ROMAN, 16, Font.NORMAL, BaseColor.BLACK); PdfReader reader = new PdfReader(src); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest)); PdfContentByte canvas = stamper.getOverContent(1); for (int k = 0; k < 3; k++) { //Positions int[] x = { 298, 350, 125, 80, 80, 80, 80, 120 }; int[] y = { 562, 562, 518, 498, 479, 459, 440, 420 }; if (k == 1) { for (int j = 0; j < 8; j++) { y[j] = y[j] - 186; } } if (k == 2) { for (int j = 0; j < 8; j++) { y[j] = y[j] - 372; } } for (int i = 0; i < 2; i++) { if (i == 1) { for (int j = 0; j < 8; j++) { x[j] = x[j] + 372; } } ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myJobNum, timesJob), x[0], y[0], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(String.valueOf(myJobIdentifier), timesDef), x[1], y[1], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myCustName, timesDef), x[2], y[2], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemName, timesDef), x[3], y[3], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myItemCode, timesDef), x[4], y[4], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myDate, timesDef), x[5], y[5], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myPOrderNum, timesDef), x[6], y[6], 0); ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase(myInputPcs, timesDef), x[7], y[7], 0); } } stamper.close(); reader.close(); } catch (IOException | DocumentException ex) { Logger.getLogger(PDFCreator.class.getName()).log(Level.SEVERE, null, ex); } }