public static void main(String[] args) throws IOException, DocumentException { GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontFamily = ge.getAvailableFontFamilyNames(); PrintStream out1 = new PrintStream(new FileOutputStream(RESULT1)); for (int i = 0; i < fontFamily.length; i++) { out1.println(fontFamily[i]); } out1.flush(); out1.close(); DefaultFontMapper mapper = new DefaultFontMapper(); mapper.insertDirectory("c:/windows/fonts/"); PrintStream out2 = new PrintStream(new FileOutputStream(RESULT2)); for (Entry<String, BaseFontParameters> entry : mapper.getMapper().entrySet()) { out2.println(String.format("%s: %s", entry.getKey(), entry.getValue().fontName)); } out2.flush(); out2.close(); float width = 150; float height = 150; Document document = new Document(new Rectangle(width, height)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT3)); document.open(); PdfContentByte cb = writer.getDirectContent(); Graphics2D g2d = cb.createGraphics(width, height, mapper); for (int i = 0; i < FONTS.length; ) { g2d.setFont(FONTS[i++]); g2d.drawString("Hello world", 5, 24 * i); } g2d.dispose(); document.close(); }
/** * Creates a PDF document. * * @param filename the path to the new PDF document * @throws DocumentException * @throws IOException * @throws BadLocationException */ public void createPdf(String filename) throws IOException, DocumentException, BadLocationException { Document document = new Document(new Rectangle(300, 150)); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filename)); document.open(); PdfContentByte canvas = writer.getDirectContent(); DefaultFontMapper mapper = new DefaultFontMapper(); BaseFontParameters parameters = new BaseFontParameters("c:/windows/fonts/msgothic.ttc,1"); parameters.encoding = BaseFont.IDENTITY_H; mapper.putName("MS PGothic", parameters); Graphics2D g2 = canvas.createGraphics(300, 150, mapper); JTextPane text = TextExample4.createTextPane(); text.setSize(new Dimension(300, 150)); text.print(g2); g2.dispose(); document.close(); }