List of usage examples for com.lowagie.text.pdf BaseFont createFont
public static BaseFont createFont(String name, String encoding, boolean embedded, boolean cached, byte ttfAfm[], byte pfb[]) throws DocumentException, IOException
From source file:buckley.compile.ITextFontFactory.java
License:Apache License
public BaseFont build(String name, String encoding, byte[] fontFileBytes) { try {/*www. j a v a 2 s . c o m*/ return BaseFont.createFont(name, encoding, true, true, fontFileBytes, new byte[0]); } catch (Exception e) { throw new IllegalStateException("Problem build the iText font", e); } }
From source file:com.develog.utils.report.engine.export.JRPdfExporter.java
License:Open Source License
/** * *//*from w ww . j a va 2s. co m*/ protected Chunk getChunk(Map attributes, String text) throws JRException, DocumentException, IOException { JRFont jrFont = new JRBaseFont(attributes); BaseFont baseFont = null; Exception initialException = null; try { baseFont = BaseFont.createFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded(), true, null, null); } catch (Exception e) { initialException = e; } if (baseFont == null) { byte[] bytes = null; try { bytes = JRLoader.loadBytesFromLocation(jrFont.getPdfFontName()); } catch (JRException e) { throw new JRException("Could not load the following font : " + "\npdfFontName : " + jrFont.getPdfFontName() + "\npdfEncoding : " + jrFont.getPdfEncoding() + "\nisPdfEmbedded : " + jrFont.isPdfEmbedded(), initialException); } baseFont = BaseFont.createFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded(), true, bytes, null); } Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND); Color backcolor = (Color) attributes.get(TextAttribute.BACKGROUND); /* if (forecolor == null) { forecolor = Color.black; } */ Font font = new Font(baseFont, (float) jrFont.getSize(), //((jrFont.isBold())?Font.BOLD:0) + //((jrFont.isItalic())?Font.ITALIC:0) + (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0), forecolor); Chunk chunk = new Chunk(text, font); if (backcolor != null) { chunk.setBackground(backcolor); } return chunk; }
From source file:com.dlya.facturews.DlyaPdfExporter2.java
License:Open Source License
/** * Creates a PDF font./*from w w w .java2 s . c o m*/ * * @param attributes the text attributes of the font * @param locale the locale for which to create the font * @param setFontLines whether to set underline and strikethrough as font style * @return the PDF font for the specified attributes */ @SuppressWarnings("deprecation") protected Font getFont(Map<Attribute, Object> attributes, Locale locale, boolean setFontLines) { JRFont jrFont = new JRBaseFont(attributes); Exception initialException = null; Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND); // use the same font scale ratio as in JRStyledText.getAwtAttributedString float fontSizeScale = 1f; Integer scriptStyle = (Integer) attributes.get(TextAttribute.SUPERSCRIPT); if (scriptStyle != null && (TextAttribute.SUPERSCRIPT_SUB.equals(scriptStyle) || TextAttribute.SUPERSCRIPT_SUPER.equals(scriptStyle))) { fontSizeScale = 2f / 3; } Font font = null; PdfFont pdfFont = null; FontKey key = new FontKey(jrFont.getFontName(), jrFont.isBold(), jrFont.isItalic()); if (fontMap != null && fontMap.containsKey(key)) { pdfFont = pdfFontMap.get(key); } else { FontInfo fontInfo = FontUtil.getInstance(jasperReportsContext).getFontInfo(jrFont.getFontName(), locale); if (fontInfo == null) { //fontName NOT found in font extensions pdfFont = new PdfFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded()); } else { //fontName found in font extensions FontFamily family = fontInfo.getFontFamily(); FontFace face = fontInfo.getFontFace(); int faceStyle = java.awt.Font.PLAIN; if (face == null) { //fontName matches family name in font extension if (jrFont.isBold() && jrFont.isItalic()) { face = family.getBoldItalicFace(); faceStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC; } if (face == null && jrFont.isBold()) { face = family.getBoldFace(); faceStyle = java.awt.Font.BOLD; } if (face == null && jrFont.isItalic()) { face = family.getItalicFace(); faceStyle = java.awt.Font.ITALIC; } if (face == null) { face = family.getNormalFace(); faceStyle = java.awt.Font.PLAIN; } // if (face == null) // { // throw new JRRuntimeException("Font family '" + family.getName() + "' does not have the normal font face."); // } } else { //fontName matches face name in font extension; not family name faceStyle = fontInfo.getStyle(); } String pdfFontName = null; int pdfFontStyle = java.awt.Font.PLAIN; if (jrFont.isBold() && jrFont.isItalic()) { pdfFontName = family.getBoldItalicPdfFont(); pdfFontStyle = java.awt.Font.BOLD | java.awt.Font.ITALIC; } if (pdfFontName == null && jrFont.isBold()) { pdfFontName = family.getBoldPdfFont(); pdfFontStyle = java.awt.Font.BOLD; } if (pdfFontName == null && jrFont.isItalic()) { pdfFontName = family.getItalicPdfFont(); pdfFontStyle = java.awt.Font.ITALIC; } if (pdfFontName == null) { pdfFontName = family.getNormalPdfFont(); pdfFontStyle = java.awt.Font.PLAIN; } if (pdfFontName == null) { //in theory, face file cannot be null here pdfFontName = (face == null || face.getFile() == null ? jrFont.getPdfFontName() : face.getFile()); pdfFontStyle = faceStyle;//FIXMEFONT not sure this is correct, in case we inherit pdfFontName from default properties } // String ttf = face.getFile(); // if (ttf == null) // { // throw new JRRuntimeException("The '" + face.getName() + "' font face in family '" + family.getName() + "' returns a null file."); // } pdfFont = new PdfFont(pdfFontName, family.getPdfEncoding() == null ? jrFont.getPdfEncoding() : family.getPdfEncoding(), family.isPdfEmbedded() == null ? jrFont.isPdfEmbedded() : family.isPdfEmbedded().booleanValue(), jrFont.isBold() && ((pdfFontStyle & java.awt.Font.BOLD) == 0), jrFont.isItalic() && ((pdfFontStyle & java.awt.Font.ITALIC) == 0)); } } int pdfFontStyle = (pdfFont.isPdfSimulatedBold() ? Font.BOLD : 0) | (pdfFont.isPdfSimulatedItalic() ? Font.ITALIC : 0); if (setFontLines) { pdfFontStyle |= (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0); } try { font = FontFactory.getFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), jrFont.getFontSize() * fontSizeScale, pdfFontStyle, forecolor); // check if FontFactory didn't find the font if (font.getBaseFont() == null && font.family() == Font.UNDEFINED) { font = null; } } catch (Exception e) { initialException = e; } if (font == null) { byte[] bytes = null; try { bytes = RepositoryUtil.getInstance(jasperReportsContext) .getBytesFromLocation(pdfFont.getPdfFontName()); } catch (JRException e) { throw //NOPMD new JRRuntimeException("Could not load the following font : " + "\npdfFontName : " + pdfFont.getPdfFontName() + "\npdfEncoding : " + pdfFont.getPdfEncoding() + "\nisPdfEmbedded : " + pdfFont.isPdfEmbedded(), initialException); } BaseFont baseFont = null; try { baseFont = BaseFont.createFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), true, bytes, null); } catch (DocumentException e) { throw new JRRuntimeException(e); } catch (IOException e) { throw new JRRuntimeException(e); } font = new Font(baseFont, jrFont.getFontSize() * fontSizeScale, pdfFontStyle, forecolor); } return font; }
From source file:com.gtdfree.test.PDFTestFont.java
License:Open Source License
/** * Fonts and encoding./*from w w w. j av a 2 s . c o m*/ * @param args no arguments needed */ public static void main(String[] args) { System.out.println("Encodings"); String[] names = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); System.out.println(Arrays.toString(names)); /*System.out.println("---"); try { System.getProperties().store(System.out, ""); } catch (IOException e1) { e1.printStackTrace(); } System.out.println("---");*/ //System.out.println(System.getenv()); //System.out.println("---"); //String font= System.getProperty("java.home")+"/lib/fonts/LucidaBrightRegular.ttf"; //String font= "fonts/DejaVuSans.ttf"; //byte[] ttf= ApplicationHelper.loadResource(font); try { // step 1 Document document = new Document(PageSize.A4, 50, 50, 50, 50); // step 2 PdfWriter.getInstance(document, new FileOutputStream("encodingfont.pdf")); // step 3 document.open(); // step 4 String all[] = { "Symbol", "ZapfDingbats" }; Font hex = new Font(Font.HELVETICA, 5); for (int z = 0; z < all.length; ++z) { String file = all[z]; document.add(new Paragraph( "Unicode equivalence for the font \"" + file + "\" with the encoding \"" + file + "\"\n")); /*char tb[]; if (z == 0) tb = SYMBOL_TABLE; else tb = DINGBATS_TABLE;*/ BaseFont bf; if (z == 2) { bf = BaseFont.createFont(file, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, true, null, null); ; } else { bf = BaseFont.createFont(file, file, true); } Font f = new Font(bf, 12); PdfPTable table = new PdfPTable(16); table.setWidthPercentage(100); table.getDefaultCell().setBorderWidth(1); table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); for (char k = 0; k < Character.MAX_VALUE; ++k) { char c = k; if (bf.charExists(c)) { Phrase ph = new Phrase(12, new String(new char[] { c }), f); ph.add(new Phrase(12, "\n" + Integer.toString(c) + "\n" + cst(c), hex)); table.addCell(ph); } /* else { Phrase ph = new Phrase("\u00a0"); ph.add(new Phrase(12, "\n\n" + cst(c), hex)); table.addCell(ph); }*/ } document.add(table); document.newPage(); } // step 5 document.close(); FontFactory.registerDirectories(); Set<?> s = FontFactory.getRegisteredFonts(); System.out.println("Fonts: " + s); s = FontFactory.getRegisteredFamilies(); System.out.println("Families: " + s); ArrayList<Font> f = new ArrayList<Font>(s.size()); for (Object name : s) { try { f.add(FontFactory.getFont(name.toString(), "UTF-8", true, 12, Font.NORMAL, Color.BLACK, true)); } catch (Exception e) { f.add(FontFactory.getFont(name.toString(), "UTF-8", false, 12, Font.NORMAL, Color.BLACK, true)); } } Collections.sort(f, new Comparator<Font>() { @Override public int compare(Font o1, Font o2) { return o1.getFamilyname().compareTo(o2.getFamilyname()); } }); for (Font ff : f) { if (ff.getBaseFont() == null) { continue; } System.out.println(ff.getFamilyname() + " " + ff.getBaseFont().isEmbedded()); } } catch (Exception de) { de.printStackTrace(); } }
From source file:com.qubit.terra.docs.util.FontManager.java
License:Open Source License
protected FontEntry createFontEntry(File ttfFile) throws FontFormatException, IOException, FileNotFoundException, DocumentException { InputStream is = null;// ww w. ja v a 2s . c o m try { is = new FileInputStream(ttfFile); byte[] fontData = RandomAccessFileOrArray.InputStreamToArray(is); BaseFont baseFont = BaseFont.createFont(ttfFile.getName(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, fontData, null); FontEntry fontEntry = new FontEntry(ttfFile.getName(), baseFont); return fontEntry; } finally { if (is != null) { is.close(); } } }
From source file:corner.orm.tapestry.jasper.exporter.CornerPdfExporter.java
License:Apache License
/** * ?,,./*from w ww . j ava2s .c o m*/ * @see net.sf.jasperreports.engine.export.JRPdfExporter#getFont(java.util.Map) */ protected Font getFont(Map attributes) { JRFont jrFont = new JRBaseFont(attributes); Exception initialException = null; Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND); /* if (forecolor == null) { forecolor = Color.black; } */ Font font = null; PdfFont pdfFont = null; FontKey key = new FontKey(jrFont.getFontName(), jrFont.isBold(), jrFont.isItalic()); if (fontMap != null && fontMap.containsKey(key)) { pdfFont = (PdfFont) fontMap.get(key); } else { pdfFont = new PdfFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded(), jrFont.isBold(), jrFont.isItalic()); } try { font = FontFactory.getFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), jrFont.getFontSize(), (pdfFont.isPdfSimulatedBold() ? Font.BOLD : 0) | (pdfFont.isPdfSimulatedItalic() ? Font.ITALIC : 0) | (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0), forecolor); // check if FontFactory didn't find the font if (font.getBaseFont() == null && font.family() == Font.UNDEFINED) { font = null; } } catch (Exception e) { initialException = e; } if (font == null) { byte[] bytes = null; try { bytes = JRLoader.loadBytesFromLocation(pdfFont.getPdfFontName(), classLoader, urlHandlerFactory); } catch (JRException e) { throw new JRRuntimeException("Could not load the following font : " + "\npdfFontName : " + pdfFont.getPdfFontName() + "\npdfEncoding : " + pdfFont.getPdfEncoding() + "\nisPdfEmbedded : " + pdfFont.isPdfEmbedded(), initialException); } BaseFont baseFont = null; try { baseFont = BaseFont.createFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), true, bytes, null); } catch (DocumentException e) { throw new JRRuntimeException(e); } catch (IOException e) { throw new JRRuntimeException(e); } font = new Font(baseFont, jrFont.getFontSize(), ((pdfFont.isPdfSimulatedBold()) ? Font.BOLD : 0) | ((pdfFont.isPdfSimulatedItalic()) ? Font.ITALIC : 0) | (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0), forecolor); } return font; }
From source file:jdbreport.model.io.pdf.itext2.ReportFontMapper.java
License:Apache License
public BaseFont awtToPdf(String fontName) { try {/*w w w. j av a 2s . c o m*/ BaseFontParameters p = getBaseFontParameters(fontName); if (p != null) { return BaseFont.createFont(p.fontName, BaseFont.IDENTITY_H, p.embedded, p.cached, p.ttfAfm, p.pfb); } } catch (DocumentException | IOException e) { throw new ExceptionConverter(e); } return null; }
From source file:net.sf.jasperreports.engine.export.JRPdfExporter.java
License:LGPL
/** * *///from ww w .java 2s .c o m protected Font getFont(Map attributes) { JRFont jrFont = new JRBaseFont(attributes); Exception initialException = null; Color forecolor = (Color) attributes.get(TextAttribute.FOREGROUND); Font font = null; PdfFont pdfFont = null; FontKey key = new FontKey(jrFont.getFontName(), jrFont.isBold(), jrFont.isItalic()); if (fontMap != null && fontMap.containsKey(key)) { pdfFont = (PdfFont) fontMap.get(key); } else { pdfFont = new PdfFont(jrFont.getPdfFontName(), jrFont.getPdfEncoding(), jrFont.isPdfEmbedded()); } try { font = FontFactory.getFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), jrFont.getFontSize(), (pdfFont.isPdfSimulatedBold() ? Font.BOLD : 0) | (pdfFont.isPdfSimulatedItalic() ? Font.ITALIC : 0) | (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0), forecolor); // check if FontFactory didn't find the font if (font.getBaseFont() == null && font.family() == Font.UNDEFINED) { font = null; } } catch (Exception e) { initialException = e; } if (font == null) { byte[] bytes = null; try { bytes = JRLoader.loadBytesFromLocation(pdfFont.getPdfFontName(), classLoader, urlHandlerFactory, fileResolver); } catch (JRException e) { throw new JRRuntimeException("Could not load the following font : " + "\npdfFontName : " + pdfFont.getPdfFontName() + "\npdfEncoding : " + pdfFont.getPdfEncoding() + "\nisPdfEmbedded : " + pdfFont.isPdfEmbedded(), initialException); } BaseFont baseFont = null; try { baseFont = BaseFont.createFont(pdfFont.getPdfFontName(), pdfFont.getPdfEncoding(), pdfFont.isPdfEmbedded(), true, bytes, null); } catch (DocumentException e) { throw new JRRuntimeException(e); } catch (IOException e) { throw new JRRuntimeException(e); } font = new Font(baseFont, jrFont.getFontSize(), ((pdfFont.isPdfSimulatedBold()) ? Font.BOLD : 0) | ((pdfFont.isPdfSimulatedItalic()) ? Font.ITALIC : 0) | (jrFont.isUnderline() ? Font.UNDERLINE : 0) | (jrFont.isStrikeThrough() ? Font.STRIKETHRU : 0), forecolor); } return font; }
From source file:net.sf.jsignpdf.utils.FontUtils.java
License:Mozilla Public License
/** * Returns BaseFont for text of visible signature; * //ww w. jav a 2s .c om * @return */ public static synchronized BaseFont getL2BaseFont() { if (l2baseFont == null) { final ConfigProvider conf = ConfigProvider.getInstance(); try { final ByteArrayOutputStream tmpBaos = new ByteArrayOutputStream(); String fontPath = conf.getNotEmptyProperty("font.path", null); String fontName; String fontEncoding; InputStream tmpIs; if (fontPath != null) { fontName = conf.getNotEmptyProperty("font.name", null); if (fontName == null) { fontName = new File(fontPath).getName(); } fontEncoding = conf.getNotEmptyProperty("font.encoding", null); if (fontEncoding == null) { fontEncoding = BaseFont.WINANSI; } tmpIs = new FileInputStream(fontPath); } else { fontName = Constants.L2TEXT_FONT_NAME; fontEncoding = BaseFont.IDENTITY_H; tmpIs = FontUtils.class.getResourceAsStream(Constants.L2TEXT_FONT_PATH); } IOUtils.copy(tmpIs, tmpBaos); tmpIs.close(); tmpBaos.close(); l2baseFont = BaseFont.createFont(fontName, fontEncoding, BaseFont.EMBEDDED, BaseFont.CACHED, tmpBaos.toByteArray(), null); } catch (Exception e) { e.printStackTrace(); try { l2baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.EMBEDDED); } catch (Exception ex) { // where is the problem, dear Watson? } } } return l2baseFont; }
From source file:org.pentaho.reporting.engine.classic.core.modules.output.support.itext.BaseFontResourceFactory.java
License:Open Source License
public Resource create(final ResourceManager manager, final ResourceData data, final ResourceKey context) throws ResourceCreationException, ResourceLoadingException { final ResourceKey key = data.getKey(); final Map factoryParameters = key.getFactoryParameters(); final boolean embedded = Boolean.TRUE.equals(factoryParameters.get(BaseFontResourceFactory.EMBEDDED)); final String encoding = String.valueOf(factoryParameters.get(BaseFontResourceFactory.ENCODING)); final String fontType = String.valueOf(factoryParameters.get(BaseFontResourceFactory.FONTNAME)); final DependencyCollector dc = new DependencyCollector(key, data.getVersion(manager)); final byte[] ttfAfm = data.getResource(manager); byte[] pfb = null; if (embedded && (fontType.endsWith(".afm") || fontType.endsWith(".pfm"))) { final String pfbFileName = fontType.substring(0, fontType.length() - 4) + ".pfb"; try {/* w w w. ja v a 2 s . c om*/ final ResourceKey pfbKey = manager.deriveKey(key, pfbFileName); final ResourceData res = manager.load(pfbKey); pfb = res.getResource(manager); dc.add(pfbKey, res.getVersion(manager)); } catch (ResourceException e) { // ignore .. } } try { final BaseFont baseFont = BaseFont.createFont(fontType, encoding, embedded, false, ttfAfm, pfb); return new CompoundResource(key, dc, baseFont, getFactoryType()); } catch (Exception e) { throw new ResourceCreationException("Failed to create the font " + fontType, e); } }