You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/01/23 19:35:55 UTC

svn commit: r1654299 - /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java

Author: jahewson
Date: Fri Jan 23 18:35:55 2015
New Revision: 1654299

URL: http://svn.apache.org/r1654299
Log:
PDFBOX-2589: Simplify HelloWorldTTF example

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java?rev=1654299&r1=1654298&r2=1654299&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldTTF.java Fri Jan 23 18:35:55 2015
@@ -19,7 +19,6 @@ package org.apache.pdfbox.examples.pdmod
 
 import java.io.File;
 import java.io.IOException;
-
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDPage;
 import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
@@ -27,85 +26,45 @@ import org.apache.pdfbox.pdmodel.font.PD
 import org.apache.pdfbox.pdmodel.font.PDType0Font;
 
 /**
- * This is an example that creates a simple document
- * with a ttf-font.
- *
- * @author <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
- * @version $Revision: 1.2 $
+ * Creates a simple document with a TrueType font.
  */
 public class HelloWorldTTF
 {
-    /**
-     * create the second sample document from the PDF file format specification.
-     *
-     * @param file      The file to write the PDF to.
-     * @param message   The message to write in the file.
-     * @param fontfile  The ttf-font file.
-     *
-     * @throws IOException If there is an error writing the data.
-     */
-    public void doIt(final String file, final String message, final String fontfile)
-        throws IOException
+    public static void main(String[] args) throws IOException
     {
+        if (args.length != 3)
+        {
+            System.err.println("usage: " + HelloWorldTTF.class.getName() +
+                    " <output-file> <Message> <ttf-file>");
+            System.exit(1);
+        }
 
-        // the document
-        PDDocument doc = null;
+        String pdfPath = args[0];
+        String message = args[1];
+        String ttfPath = args[2];
+        
+        PDDocument doc = new PDDocument();
         try
         {
-            doc = new PDDocument();
-
             PDPage page = new PDPage();
             doc.addPage(page);
-            PDFont font = PDType0Font.load(doc, new File(fontfile));
+            
+            PDFont font = PDType0Font.load(doc, new File(ttfPath));
 
-            PDPageContentStream contentStream = new PDPageContentStream(doc,
-                    page);
-            contentStream.beginText();
-            contentStream.setFont(font, 12);
-            contentStream.newLineAtOffset(100, 700);
-            contentStream.showText(message);
-            contentStream.endText();
-            contentStream.close();
-            doc.save(file);
-            System.out.println(file + " created!");
+            PDPageContentStream contents = new PDPageContentStream(doc, page);
+            contents.beginText();
+            contents.setFont(font, 12);
+            contents.newLineAtOffset(100, 700);
+            contents.showText(message);
+            contents.endText();
+            contents.close();
+            
+            doc.save(pdfPath);
+            System.out.println(pdfPath + " created!");
         }
         finally
         {
-            if (doc != null)
-            {
-                doc.close();
-            }
+            doc.close();
         }
     }
-
-    /**
-     * This will create a hello world PDF document
-     * with a ttf-font.
-     * <br />
-     * see usage() for commandline
-     *
-     * @param args Command line arguments.
-     */
-    public static void main(String[] args) throws IOException
-    {
-
-        HelloWorldTTF app = new HelloWorldTTF();
-        if (args.length != 3)
-        {
-            app.usage();
-        }
-        else
-        {
-            app.doIt(args[0], args[1], args[2]);
-        }
-    }
-
-    /**
-     * This will print out a message telling how to use this example.
-     */
-    private void usage()
-    {
-        System.err.println("usage: " + this.getClass().getName()
-                + " <output-file> <Message> <ttf-file>");
-    }
 }