1 31 package org.pdfbox; 32 33 import java.io.BufferedReader ; 34 import java.io.File ; 35 import java.io.FileReader ; 36 import java.io.IOException ; 37 import java.io.Reader ; 38 39 import org.pdfbox.pdmodel.PDDocument; 40 import org.pdfbox.pdmodel.PDPage; 41 42 import org.pdfbox.pdmodel.edit.PDPageContentStream; 43 import org.pdfbox.pdmodel.font.PDSimpleFont; 44 import org.pdfbox.pdmodel.font.PDTrueTypeFont; 45 import org.pdfbox.pdmodel.font.PDType1Font; 46 47 48 54 public class TextToPDF 55 { 56 private int fontSize = 10; 57 private PDSimpleFont font = PDType1Font.HELVETICA; 58 59 68 public PDDocument createPDFFromText( Reader text ) throws IOException 69 { 70 PDDocument doc = null; 71 try 72 { 73 74 int margin = 40; 75 float height = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000; 76 77 height = height*fontSize*1.05f; 79 doc = new PDDocument(); 80 BufferedReader data = new BufferedReader ( text ); 81 String nextLine = null; 82 PDPage page = new PDPage(); 83 PDPageContentStream contentStream = null; 84 float y = -1; 85 float maxStringLength = page.getMediaBox().getWidth() - 2*margin; 86 while( (nextLine = data.readLine()) != null ) 87 { 88 89 String [] lineWords = nextLine.trim().split( " " ); 90 int lineIndex = 0; 91 while( lineIndex < lineWords.length ) 92 { 93 StringBuffer nextLineToDraw = new StringBuffer (); 94 float lengthIfUsingNextWord = 0; 95 do 96 { 97 nextLineToDraw.append( lineWords[lineIndex] ); 98 nextLineToDraw.append( " " ); 99 lineIndex++; 100 if( lineIndex < lineWords.length ) 101 { 102 String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex]; 103 lengthIfUsingNextWord = 104 (font.getStringWidth( lineWithNextWord )/1000) * fontSize; 105 } 106 } 107 while( lineIndex < lineWords.length && 108 lengthIfUsingNextWord < maxStringLength ); 109 if( y < margin ) 110 { 111 page = new PDPage(); 112 doc.addPage( page ); 113 if( contentStream != null ) 114 { 115 contentStream.endText(); 116 contentStream.close(); 117 } 118 contentStream = new PDPageContentStream(doc, page); 119 contentStream.setFont( font, fontSize ); 120 contentStream.beginText(); 121 y = page.getMediaBox().getHeight() - margin + height; 122 contentStream.moveTextPositionByAmount( 123 margin, y ); 124 125 } 126 128 if( contentStream == null ) 129 { 130 throw new IOException ( "Error:Expected non-null content stream." ); 131 } 132 contentStream.moveTextPositionByAmount( 0, -height); 133 y -= height; 134 contentStream.drawString( nextLineToDraw.toString() ); 135 } 136 137 138 } 139 if( contentStream != null ) 140 { 141 contentStream.endText(); 142 contentStream.close(); 143 } 144 } 145 catch( IOException io ) 146 { 147 if( doc != null ) 148 { 149 doc.close(); 150 } 151 throw io; 152 } 153 return doc; 154 } 155 156 165 public static void main(String [] args) throws IOException 166 { 167 TextToPDF app = new TextToPDF(); 168 PDDocument doc = null; 169 try 170 { 171 if( args.length < 2 ) 172 { 173 app.usage(); 174 } 175 else 176 { 177 for( int i=0; i<args.length-2; i++ ) 178 { 179 if( args[i].equals( "-standardFont" )) 180 { 181 i++; 182 app.setFont( PDType1Font.getStandardFont( args[i] )); 183 } 184 else if( args[i].equals( "-ttf" )) 185 { 186 i++; 187 PDTrueTypeFont font = PDTrueTypeFont.loadTTF( doc, new File ( args[i])); 188 app.setFont( font ); 189 } 190 else if( args[i].equals( "-fontSize" )) 191 { 192 i++; 193 app.setFontSize( Integer.parseInt( args[i] ) ); 194 } 195 else 196 { 197 throw new IOException ( "Unknown argument:" + args[i] ); 198 } 199 } 200 doc = app.createPDFFromText( new FileReader ( args[args.length-1] ) ); 201 doc.save( args[args.length-2] ); 202 } 203 } 204 catch (Exception e) 205 { 206 e.printStackTrace(); 207 } 208 finally 209 { 210 if( doc != null ) 211 { 212 doc.close(); 213 } 214 } 215 } 216 217 220 private void usage() 221 { 222 String [] std14 = PDType1Font.getStandard14Names(); 223 System.err.println( "usage: " + this.getClass().getName() + " [options] <output-file> <text-file>" ); 224 System.err.println( " -standardFont <name> default:" + PDType1Font.HELVETICA.getBaseFont() ); 225 for( int i=0; i<std14.length; i++ ) 226 { 227 System.err.println( " " + std14[i] ); 228 } 229 System.err.println( " -ttf <ttf file> The TTF font to use."); 230 System.err.println( " -fontSize <fontSize> default:10" ); 231 232 233 } 234 237 public PDSimpleFont getFont() 238 { 239 return font; 240 } 241 244 public void setFont(PDSimpleFont aFont) 245 { 246 this.font = aFont; 247 } 248 251 public int getFontSize() 252 { 253 return fontSize; 254 } 255 258 public void setFontSize(int aFontSize) 259 { 260 this.fontSize = aFontSize; 261 } 262 } | Popular Tags |