--- pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java.orig	2012-07-17 22:31:46.000000000 +0300
+++ pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java	2013-03-04 17:30:35.835055368 +0200
@@ -30,6 +30,7 @@
 import org.apache.pdfbox.pdmodel.font.PDTrueTypeFont;
 import org.apache.pdfbox.pdmodel.font.PDType1Font;
 
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
 
 /**
  * This will take a text file and ouput a pdf with that text.
@@ -39,9 +40,19 @@
  */
 public class TextToPDF
 {
-    private int fontSize = 10;
+    // Set defaults 
+    private float fontSize = 10.0f;
     private PDSimpleFont font = PDType1Font.HELVETICA;
 
+    private float leftMargin = 40.0f;
+    private float rightMargin = 40.0f;
+    private float topMargin = 40.0f;
+    private float bottomMargin = 40.0f;
+
+    private float lineSpacing = 1.05f;
+
+    private PDRectangle pageSize = PDPage.PAGE_SIZE_LETTER;
+
     /**
      * Create a PDF document with some text.
      *
@@ -56,19 +67,20 @@
         PDDocument doc = null;
         try
         {
+            float fontHeight = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000;
 
-            final int margin = 40;
-            float height = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000;
+            // Calculate the line height
+            // 1) Find the height of the font in 'points'; 2) multiply by the requested font size;
+            // 3) multiply by the line spacing
+            float lineHeight = fontHeight * fontSize * lineSpacing;
 
-            //calculate font height and increase by 5 percent.
-            height = height*fontSize*1.05f;
             doc = new PDDocument();
             BufferedReader data = new BufferedReader( text );
             String nextLine = null;
-            PDPage page = new PDPage();
+            PDPage page = new PDPage( pageSize );
             PDPageContentStream contentStream = null;
-            float y = -1;
-            float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
+            float y = -1; // Any number less than zero
+            float maxStringLength = page.getMediaBox().getWidth() - leftMargin - rightMargin;
 
             // There is a special case of creating a PDF document from an empty string.
             boolean textIsEmpty = true;
@@ -95,17 +107,16 @@
                         if( lineIndex < lineWords.length )
                         {
                             String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
-                            lengthIfUsingNextWord =
-                                (font.getStringWidth( lineWithNextWord )/1000) * fontSize;
+                            lengthIfUsingNextWord = (font.getStringWidth( lineWithNextWord )/1000) * fontSize;
                         }
                     }
                     while( lineIndex < lineWords.length &&
                            lengthIfUsingNextWord < maxStringLength );
-                    if( y < margin )
+                    if( y < (bottomMargin + lineHeight) )
                     {
                         // We have crossed the end-of-page boundary and need to extend the
                         // document by another page.
-                        page = new PDPage();
+                        page = new PDPage( pageSize );
                         doc.addPage( page );
                         if( contentStream != null )
                         {
@@ -115,9 +126,8 @@
                         contentStream = new PDPageContentStream(doc, page);
                         contentStream.setFont( font, fontSize );
                         contentStream.beginText();
-                        y = page.getMediaBox().getHeight() - margin + height;
-                        contentStream.moveTextPositionByAmount(
-                            margin, y );
+                        y = page.getMediaBox().getHeight() - topMargin - font.getFontDescriptor().getXHeight()/1000;
+                        contentStream.moveTextPositionByAmount( leftMargin, y );
 
                     }
                     //System.out.println( "Drawing string at " + x + "," + y );
@@ -126,8 +136,8 @@
                     {
                         throw new IOException( "Error:Expected non-null content stream." );
                     }
-                    contentStream.moveTextPositionByAmount( 0, -height);
-                    y -= height;
+                    contentStream.moveTextPositionByAmount( 0, -lineHeight);
+                    y -= lineHeight;
                     contentStream.drawString( nextLineToDraw.toString() );
                 }
 
@@ -196,7 +206,28 @@
                     else if( args[i].equals( "-fontSize" ))
                     {
                         i++;
-                        app.setFontSize( Integer.parseInt( args[i] ) );
+                        app.setFontSize( Float.parseFloat( args[i] ) );
+                    }
+                    else if( args[i].equals( "-margins" ))
+                    {
+                        i++;
+                        app.setLeftMargin( Float.parseFloat( args[i] ) );
+                        i++;
+                        app.setRightMargin( Float.parseFloat( args[i] ) );
+                        i++;
+                        app.setTopMargin( Float.parseFloat( args[i] ) );
+                        i++;
+                        app.setBottomMargin( Float.parseFloat( args[i] ) );
+                    }
+                    else if( args[i].equals( "-lineSpacing" ))
+                    {
+                        i++;
+                        app.setLineSpacing( Float.parseFloat( args[i] ) );
+                    }
+                    else if( args[i].equals( "-pageSize" ))
+                    {
+                        i++;
+                        app.setPageSize( args[i] );
                     }
                     else
                     {
@@ -226,16 +257,18 @@
     private void usage()
     {
         String[] std14 = PDType1Font.getStandard14Names();
-        System.err.println( "usage: jar -jar pdfbox-app-x.y.z.jar TextToPDF [options] <output-file> <text-file>" );
-        System.err.println( "    -standardFont <name>    default:" + PDType1Font.HELVETICA.getBaseFont() );
+        System.err.println( "usage: java -jar pdfbox-app-x.y.z.jar TextToPDF [options] <output-file> <text-file>" );
+        System.err.println( "    -standardFont <name>                        default:" + PDType1Font.HELVETICA.getBaseFont() );
         for( int i=0; i<std14.length; i++ )
         {
-            System.err.println( "                                    " + std14[i] );
+        System.err.println( "                                                " + std14[i] );
         }
-        System.err.println( "    -ttf <ttf file>         The TTF font to use.");
-        System.err.println( "    -fontSize <fontSize>    default:10" );
-
-
+        System.err.println( "    -ttf         <ttf file>                     The TTF font to use.");
+        System.err.println( "    -fontSize    <fontSize>                     default: 10.0" );
+        System.err.println( "    -margins     <left> <right> <top> <bottom>  default: 40.0 40.0 40.0 40.0" );
+        System.err.println( "    -lineSpacing <spacing>                      default: 1.05" );
+        System.err.println( "    -pageSize    <size>                         default: US-Letter" );
+        System.err.println( "                                                A6, A5, A4, A3, A2, A1, US-Letter" );
     }
     /**
      * @return Returns the font.
@@ -254,15 +287,119 @@
     /**
      * @return Returns the fontSize.
      */
-    public int getFontSize()
+    public float getFontSize()
     {
         return fontSize;
     }
     /**
      * @param aFontSize The fontSize to set.
      */
-    public void setFontSize(int aFontSize)
+    public void setFontSize(float aFontSize)
     {
+        if ( aFontSize < 0.0f ) {
+            throw new IllegalArgumentException( "Requested font size less than zero: " + aFontSize );
+        }
         this.fontSize = aFontSize;
     }
+
+    public float getLeftMargin()
+    {
+        return leftMargin;
+    }
+
+    public void setLeftMargin(float aLeftMargin)
+    {
+        if ( aLeftMargin < 0.0f ) {
+            throw new IllegalArgumentException( "Requested left margin size less than zero: " + aLeftMargin );
+        }
+        this.leftMargin = aLeftMargin;
+    }
+
+    public float getRightMargin()
+    {
+        return rightMargin;
+    }
+
+    public void setRightMargin(float aRightMargin)
+    {
+        if ( aRightMargin < 0.0f ) {
+            throw new IllegalArgumentException( "Requested right margin size less than zero: " + aRightMargin );
+        }
+        this.rightMargin = aRightMargin;
+    }
+
+    public float getTopMargin()
+    {
+        return topMargin;
+    }
+
+    public void setTopMargin(float aTopMargin)
+    {
+        if ( aTopMargin < 0.0f ) {
+            throw new IllegalArgumentException( "Requested top margin size less than zero: " + aTopMargin );
+        }
+        this.topMargin = aTopMargin;
+    }
+
+    public float getBottomMargin()
+    {
+        return bottomMargin;
+    }
+
+    public void setBottomMargin(float aBottomMargin)
+    {
+        if ( aBottomMargin < 0.0f ) {
+            throw new IllegalArgumentException( "Requested bottom margin size less than zero: " + aBottomMargin );
+        }
+        this.bottomMargin = aBottomMargin;
+    }
+
+    public float getLineSpacing()
+    {
+        return lineSpacing;
+    }
+
+    public void setLineSpacing(float aLineSpacing)
+    {
+        if ( aLineSpacing < 0.0f ) {
+            throw new IllegalArgumentException( "Requested line spacing of less than zero: " + aLineSpacing );
+        }
+        this.lineSpacing = aLineSpacing;
+    }
+
+    public PDRectangle getPageSize()
+    {
+        return pageSize;
+    }
+
+    public void setPageSize(String aPageSize)
+    {
+        if ( aPageSize.equals("A6") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A6;
+        }
+        else if ( aPageSize.equals("A5") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A5;
+        }
+        else if ( aPageSize.equals("A4") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A4;
+        }
+        else if ( aPageSize.equals("A3") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A3;
+        }
+        else if ( aPageSize.equals("A2") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A2;
+        }
+        else if ( aPageSize.equals("A1") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A1;
+        }
+        else if ( aPageSize.equals("A0") ) {
+            this.pageSize = PDPage.PAGE_SIZE_A0;
+        }
+        else if ( aPageSize.equals("US-Letter") ) {
+            this.pageSize = PDPage.PAGE_SIZE_LETTER;
+        }
+        else {
+            throw new IllegalArgumentException( "Unknown page size: " + aPageSize );
+        }
+    }
 }
