1 31 32 package org.pdfbox.examples.pdmodel; 33 34 import java.io.IOException ; 35 36 import org.pdfbox.exceptions.COSVisitorException; 37 import org.pdfbox.pdmodel.PDDocument; 38 import org.pdfbox.pdmodel.PDPage; 39 import org.pdfbox.pdmodel.edit.PDPageContentStream; 40 import org.pdfbox.pdmodel.font.PDFont; 41 import org.pdfbox.pdmodel.font.PDTrueTypeFont; 42 43 50 public class HelloWorldTTF 51 { 52 53 63 public void doIt(final String file, final String message, 64 final String fontfile) throws IOException , COSVisitorException 65 { 66 67 PDDocument doc = null; 69 try 70 { 71 doc = new PDDocument(); 72 73 PDPage page = new PDPage(); 74 doc.addPage(page); 75 PDFont font = PDTrueTypeFont.loadTTF(doc, fontfile); 76 77 PDPageContentStream contentStream = new PDPageContentStream(doc, 78 page); 79 contentStream.beginText(); 80 contentStream.setFont(font, 12); 81 contentStream.moveTextPositionByAmount(100, 700); 82 contentStream.drawString(message); 83 contentStream.endText(); 84 contentStream.close(); 85 doc.save(file); 86 System.out.println(file + " created!"); 87 } 88 finally 89 { 90 if (doc != null) 91 { 92 doc.close(); 93 } 94 } 95 } 96 97 105 public static void main(String [] args) 106 { 107 108 HelloWorldTTF app = new HelloWorldTTF(); 109 try 110 { 111 if (args.length != 3) 112 { 113 app.usage(); 114 } 115 else 116 { 117 app.doIt(args[0], args[1], args[2]); 118 } 119 } 120 catch (Exception e) 121 { 122 e.printStackTrace(); 123 } 124 } 125 126 129 private void usage() 130 { 131 System.err.println("usage: " + this.getClass().getName() 132 + " <output-file> <Message> <ttf-file>"); 133 } 134 } | Popular Tags |