List of usage examples for com.lowagie.text.pdf BaseFont IDENTITY_H
String IDENTITY_H
To view the source code for com.lowagie.text.pdf BaseFont IDENTITY_H.
Click Source Link
From source file:questions.graphics2D.SwingForceArialUni.java
public static void main(String[] args) { Document document = new Document(new Rectangle(210, 25)); try {/*from w ww. j a v a2 s . c o m*/ PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT)); document.open(); PdfContentByte cb = writer.getDirectContent(); FontMapper arialuni = new FontMapper() { public BaseFont awtToPdf(Font font) { try { return BaseFont.createFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } public Font pdfToAwt(BaseFont font, int size) { return null; } }; Graphics2D g2 = cb.createGraphics(200, 50, arialuni); g2.setFont(null); g2.drawString("Greek mu: \u03bc - \u039c; degree symbol: \u00b0", 0, 40); g2.dispose(); } catch (DocumentException de) { System.err.println(de.getMessage()); } catch (IOException ioe) { System.err.println(ioe.getMessage()); } // step 5: we close the document document.close(); }
From source file:ro.nextreports.server.report.jasper.JasperReportsUtil.java
License:Apache License
@SuppressWarnings("deprecation") private static byte[] getBytes(JRAbstractExporter exporter, ByteArrayOutputStream baos, JasperPrint jasperPrint) throws JRException { printNextReportsParameters();/*from w w w.j a v a 2 s .c o m*/ // for csv delimiter //exporter.setParameter(JRCsvExporterParameter.FIELD_DELIMITER, ";"); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); if (exporter instanceof JRPdfExporter) { exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, encoding); // create embedded pdf font (like in nextreports) if (embeddedFont != null) { HashMap<FontKey, PdfFont> fontMap = new HashMap<FontKey, PdfFont>(); FontKey key = new FontKey("Arial", false, false); PdfFont font = new PdfFont(embeddedFont, BaseFont.IDENTITY_H, true); fontMap.put(key, font); exporter.setParameter(JRPdfExporterParameter.FONT_MAP, fontMap); } } else { exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); } exporter.exportReport(); return baos.toByteArray(); }
From source file:test.poi.ConvertDocxResumeToPDF.java
License:LGPL
private static void create() { long startTime = System.currentTimeMillis(); try {/*from w w w .ja v a2 s . c o m*/ // 1) Load docx with POI XWPFDocument XWPFDocument document = new XWPFDocument( ConvertDocxResumeToPDF.class.getClassLoader().getResourceAsStream("DocxResume.docx")); // 2) Convert POI XWPFDocument 2 PDF with iText File outFile = new File("d:/DocxResume.pdf"); outFile.getParentFile().mkdirs(); OutputStream out = new FileOutputStream(outFile); PdfOptions options = PdfOptions.create(); // options.fontProvider(new IFontProvider() { @Override public Font getFont(String familyName, String encoding, float size, int style, Color color) { try { BaseFont bfChinese = BaseFont.createFont("c:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font fontChinese = new Font(bfChinese, size, style, color); if (familyName != null) fontChinese.setFamily(familyName); return fontChinese; } catch (Throwable e) { e.printStackTrace(); return ITextFontRegistry.getRegistry().getFont(familyName, encoding, size, style, color); } } }); PdfConverter.getInstance().convert(document, out, options); } catch (Throwable e) { e.printStackTrace(); } System.out.println("Generate DocxResume.pdf with " + (System.currentTimeMillis() - startTime) + " ms."); }
From source file:textdisplay.TagFilter.java
License:Educational Community License
public void replaceTagsWithPDFEncoding(String[] tags, styles[] tagStyles, OutputStream os) throws DocumentException { // FileWriter w = null; try {/* w ww . j av a2s .c o m*/ BaseFont bf = BaseFont.createFont("/usr/Junicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Document doc = new Document(); PdfWriter p = PdfWriter.getInstance(doc, os); doc.open(); Paragraph para = new Paragraph(); para.setFont(new Font(bf, 12, Font.NORMAL)); //doc.add(para); Font italic = new Font(bf, 12, Font.ITALIC); Font bold = new Font(bf, 12, Font.BOLD); Font underlined = new Font(bf, 12, Font.UNDERLINE); StringBuilder chunkBuffer = new StringBuilder(""); //holds the next bit of content that will be added to the pdf as a chunk styles chunkStyle = null; //the style to be applied to chunkBuffer when it gets added to the document String chunkTag = ""; Stack<String> wrappingTags = new Stack(); Stack<styles> wrappingStyles = new Stack(); String content = text; Boolean inTag = false; //is this inside a tag, meaning between the < and > String tagTextBuffer = ""; //the text of the tag, including name and any parameters Boolean beingTagged = false; //Is the parser currently reading character data that is surrounded by a tag that demands styling for (int charCounter = 0; charCounter < this.text.length(); charCounter++) { if (text.charAt(charCounter) == '>') { inTag = false; //if this was a self closing tag, dont do anything if (tagTextBuffer.contains("/>")) { tagTextBuffer = ""; } else { //this is a closing tag, save the chunk and pop the tag and style off of the stack if (tagTextBuffer.startsWith("/")) { if (chunkStyle != null) System.out.print(" closing tag " + tagTextBuffer + " with style " + chunkStyle.name() + "\n"); else System.out.print(" closing tag " + tagTextBuffer + " with style null" + "\n"); if (chunkStyle == styles.paragraph) chunkBuffer = new StringBuilder("\n" + chunkBuffer); Chunk c = new Chunk(chunkBuffer.toString()); styleChunk(c, chunkStyle); if (chunkStyle != styles.remove) para.add(c); chunkBuffer = new StringBuilder(""); chunkStyle = null; chunkTag = ""; if (!wrappingStyles.empty()) { chunkStyle = wrappingStyles.pop(); chunkTag = wrappingTags.pop(); } tagTextBuffer = ""; } else { //this is the closing bracket of an opening tag String tagName = tagTextBuffer.split(" ")[0]; System.out.print("tag is " + tagName + "\n"); for (int i = 0; i < tags.length; i++) { if (tags[i].compareTo(tagName) == 0) { // this is a tag that is suposed to be styled in the pdf if (chunkStyle != null) { //this tag is nested in a tag that was already applying styling. Add this chunk to the pdf and put the tag/style //for the previous tag on the stack, so when this new tag ends, the previous styling will resume. if (chunkStyle == styles.paragraph) chunkBuffer = new StringBuilder("\n" + chunkBuffer); Chunk c = new Chunk(chunkBuffer.toString()); styleChunk(c, chunkStyle); if (chunkStyle != styles.remove) para.add(c); wrappingStyles.add(chunkStyle); wrappingTags.add(chunkTag); chunkTag = tagName; chunkStyle = tagStyles[i]; chunkBuffer = new StringBuilder(""); } else { Chunk c = new Chunk(chunkBuffer.toString()); para.add(c); chunkTag = tagName; chunkStyle = tagStyles[i]; chunkBuffer = new StringBuilder(""); } } } tagTextBuffer = ""; } } } if (inTag) { tagTextBuffer += text.charAt(charCounter); } if (text.charAt(charCounter) == '<') { if (inTag) { //if we hit another < before hitting a > this was not a tag, so add the tagTextBuffer to the chunk. It was simply conent. chunkBuffer.append(tagTextBuffer); tagTextBuffer = ""; } inTag = true; } if (!inTag && text.charAt(charCounter) != '>') { chunkBuffer.append(text.charAt(charCounter)); } } Chunk c = new Chunk(chunkBuffer.toString()); para.add(c); doc.newPage(); doc.add(para); doc.newPage(); doc.close(); } catch (IOException ex) { Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex); } finally { } }
From source file:textdisplay.TagFilter.java
License:Educational Community License
/**Apply a style (font) to a chunk of pdf text*/ private void styleChunk(Chunk c, styles s) { try {/* ww w . j av a2 s .co m*/ BaseFont bf = BaseFont.createFont("/usr/Junicode.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); BaseFont ita = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); BaseFont bol = BaseFont.createFont("/usr/Junicode-Italic.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); if (bf.charExists(540)) { System.out.print("font cant do 540\n"); } Font italic = new Font(ita, 12, Font.ITALIC); Font bold = new Font(bol, 12, Font.BOLD); Font underlined = new Font(bf, 12, Font.UNDERLINE); Font superscript = new Font(bf, 9, Font.NORMAL); if (s == styles.bold) { c.setFont(bold); } if (s == styles.italic) { c.setFont(italic); } if (s == styles.underlined) { c.setFont(underlined); } if (s == styles.superscript) { c.setTextRise(7.0f); c.setFont(superscript); } //wipe out that content } catch (DocumentException ex) { Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TagFilter.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:ua.karazin.math.deansoffice.business.pdf.PDFCreate.java
public byte[] PDFCreate(String WriteInFile, String FileName) throws DocumentException, IOException, ParserConfigurationException, SAXException { ITextRenderer renderer = new ITextRenderer(); renderer.getFontResolver().addFont("c:\\windows\\Fonts\\VERDANA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);/*from w w w . j a v a 2 s. com*/ renderer.getFontResolver().addFont("c:\\windows\\Fonts\\TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); OutputStream file = new FileOutputStream( new File(System.getProperty("com.sun.aas.instanceRoot") + "/docroot/" + FileName + ".pdf")); final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(false); DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); builder.setEntityResolver(FSEntityResolver.instance()); org.w3c.dom.Document document = builder .parse(new ByteArrayInputStream(WriteInFile.toString().getBytes("UTF-8"))); renderer.setDocument(document, null); renderer.layout(); renderer.createPDF(file); Path path = Paths.get(System.getProperty("com.sun.aas.instanceRoot") + "/docroot/" + FileName + ".pdf"); byte[] data = Files.readAllBytes(path); System.out.println(data); return data; }
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 ww w . ja v a2s. com * @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); }
From source file:za.co.equalpay.web.utils.PDFExportUtility.java
public void postProcess() throws MalformedURLException, IOException, DocumentException { // document.open(); ////from ww w.j a va 2 s . co m // ByteArrayOutputStream baos = new ByteArrayOutputStream(); // PdfWriter writer = PdfWriter.getInstance(document, baos); // // writer.open(); // PdfContentByte cb = writer.getDirectContent(); // cb.addImage(image); 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.BOLDITALIC); tahoma.setColor(Color.GRAY); Font boldFont8 = new Font(bf, 8, Font.BOLD, Color.GRAY); Font normalFont8 = new Font(bf, 8, Font.NORMAL, Color.GRAY); Font boldFont10 = new Font(bf, 10, Font.BOLDITALIC, Color.GRAY); Font normalFont10 = new Font(bf, 10, Font.ITALIC, Color.GRAY); phrase = new Phrase(); phrase.add(new Phrase("Exclusive Distributors of ", normalFont10)); phrase.add(new Phrase("SAM Medical, CONTERRA, TACMED Solutions ", boldFont10)); phrase.add(new Phrase("and ", normalFont10)); phrase.add(new Phrase("NARP ", boldFont10)); phrase.add(new Phrase("in South Africa.", normalFont10)); Paragraph paragraph = new Paragraph(phrase); paragraph.setAlignment(Paragraph.ALIGN_CENTER); document.add(paragraph); PdfPTable table = new PdfPTable(1); table.setWidthPercentage(100); // Terms & Conditions phrase = new Phrase(); phrase.add(new Phrase("\n\n\n")); phrase.add(new Phrase("Terms and Conditions: ", boldFont8)); phrase.add(new Phrase("\n")); phrase.add(new Phrase( "\n1. Full Payment in Advance, unless arranged otherwise. Orders will only be processed once payment reflects in our Bank Account.", normalFont8)); phrase.add(new Phrase("\n2. Prices are net.", normalFont8)); phrase.add(new Phrase("\n3. Delivery will be ex stock, alternatively 4 6 weeks from date of order.", normalFont8)); phrase.add( new Phrase("\n4. Shipping Lead Time will depend on clients mode of Transport.", normalFont8)); phrase.add(new Phrase("\n5. Prices and Supply of Goods are Subject to availability of stock.", normalFont8)); phrase.add(new Phrase("\n6. Prices are subject to exchange rate and brand.", normalFont8)); phrase.add(new Phrase("\n7. Quotation is valid for 30 Days", boldFont8)); phrase.add(new Phrase( "\n8. All capital equipment carries a one year guarantee against defective material and workmanship", normalFont8)); phrase.add(new Phrase("\n9. E & OA accepted.", normalFont8)); phrase.add(new Phrase("\n10. Excluding Postage, Packaging or Freight Forwarding to relevant Country.", normalFont8)); PdfPCell cell = new PdfPCell(phrase); cell.setBorder(0); table.addCell(cell); document.add(table); }