1 31 package org.pdfbox.examples.pdmodel; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.exceptions.COSVisitorException; 36 37 import org.pdfbox.pdmodel.PDDocument; 38 import org.pdfbox.pdmodel.PDPage; 39 40 import org.pdfbox.pdmodel.edit.PDPageContentStream; 41 42 import org.pdfbox.pdmodel.font.PDFont; 43 import org.pdfbox.pdmodel.font.PDType1Font; 44 45 46 54 public class HelloWorld 55 { 56 59 public HelloWorld() 60 { 61 super(); 62 } 63 64 73 public void doIt( String file, String message) throws IOException , COSVisitorException 74 { 75 PDDocument doc = null; 77 try 78 { 79 doc = new PDDocument(); 80 81 PDPage page = new PDPage(); 82 doc.addPage( page ); 83 PDFont font = PDType1Font.HELVETICA_BOLD; 84 85 PDPageContentStream contentStream = new PDPageContentStream(doc, page); 86 contentStream.beginText(); 87 contentStream.setFont( font, 12 ); 88 contentStream.moveTextPositionByAmount( 100, 700 ); 89 contentStream.drawString( message ); 90 contentStream.endText(); 91 contentStream.close(); 92 doc.save( file ); 93 } 94 finally 95 { 96 if( doc != null ) 97 { 98 doc.close(); 99 } 100 } 101 } 102 103 110 public static void main(String [] args) 111 { 112 HelloWorld app = new HelloWorld(); 113 try 114 { 115 if( args.length != 2 ) 116 { 117 app.usage(); 118 } 119 else 120 { 121 app.doIt( args[0], args[1] ); 122 } 123 } 124 catch (Exception e) 125 { 126 e.printStackTrace(); 127 } 128 } 129 130 133 private void usage() 134 { 135 System.err.println( "usage: " + this.getClass().getName() + " <output-file> <Message>" ); 136 } 137 } | Popular Tags |