pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1817398 - /pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Date Thu, 07 Dec 2017 16:53:10 GMT
Author: tilman
Date: Thu Dec  7 16:53:10 2017
New Revision: 1817398

URL: http://svn.apache.org/viewvc?rev=1817398&view=rev
Log:
PDFBOX-4025: allow other page sizes than letter, as suggested by Andreas Pehnack

Modified:
    pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java

Modified: pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java?rev=1817398&r1=1817397&r2=1817398&view=diff
==============================================================================
--- pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java (original)
+++ pdfbox/branches/2.0/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java Thu Dec
 7 16:53:10 2017
@@ -61,6 +61,7 @@ public class TextToPDF
     private static final float LINE_HEIGHT_FACTOR = 1.05f;
 
     private int fontSize = DEFAULT_FONT_SIZE;
+    private PDRectangle mediaBox = PDRectangle.LETTER;
     private boolean landscape = false;
     private PDFont font = DEFAULT_FONT;
 
@@ -102,6 +103,7 @@ public class TextToPDF
     /**
      * Create a PDF document with some text.
      *
+     * @param doc The document.
      * @param text The stream of text data.
      *
      * @throws IOException If there is an error writing the data.
@@ -113,17 +115,17 @@ public class TextToPDF
 
             final int margin = 40;
             float height = font.getBoundingBox().getHeight() / FONTSCALE;
-            PDRectangle mediaBox = PDRectangle.LETTER;
+            PDRectangle actualMediaBox = mediaBox;
             if (landscape)
             {
-                mediaBox = new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth());
+                actualMediaBox = new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth());
             }
 
             //calculate font height and increase by a factor.
             height = height*fontSize*LINE_HEIGHT_FACTOR;
             BufferedReader data = new BufferedReader( text );
-            String nextLine = null;
-            PDPage page = new PDPage(mediaBox);
+            String nextLine;
+            PDPage page = new PDPage(actualMediaBox);
             PDPageContentStream contentStream = null;
             float y = -1;
             float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
@@ -198,13 +200,13 @@ public class TextToPDF
                                 (font.getStringWidth( lineWithNextWord )/FONTSCALE) * fontSize;
                         }
                     }
-                    while( lineIndex < lineWords.length &&
-                           lengthIfUsingNextWord < maxStringLength );
+                    while (lineIndex < lineWords.length && lengthIfUsingNextWord
< maxStringLength);
+
                     if( y < margin )
                     {
                         // We have crossed the end-of-page boundary and need to extend the
                         // document by another page.
-                        page = new PDPage(mediaBox);
+                        page = new PDPage(actualMediaBox);
                         doc.addPage( page );
                         if( contentStream != null )
                         {
@@ -215,9 +217,7 @@ public class TextToPDF
                         contentStream.setFont( font, fontSize );
                         contentStream.beginText();
                         y = page.getMediaBox().getHeight() - margin + height;
-                        contentStream.newLineAtOffset(
-                                margin, y);
-
+                        contentStream.newLineAtOffset(margin, y);
                     }
 
                     if( contentStream == null )
@@ -229,7 +229,7 @@ public class TextToPDF
                     contentStream.showText(nextLineToDraw.toString());
                     if (ff)
                     {
-                        page = new PDPage(mediaBox);
+                        page = new PDPage(actualMediaBox);
                         doc.addPage(page);
                         contentStream.endText();
                         contentStream.close();
@@ -240,8 +240,6 @@ public class TextToPDF
                         contentStream.newLineAtOffset(margin, y);
                     }
                 }
-
-
             }
 
             // If the input text was the empty string, then the above while loop will have
short-circuited
@@ -311,13 +309,23 @@ public class TextToPDF
                         i++;
                         app.setFontSize( Integer.parseInt( args[i] ) );
                     }
+                    else if( args[i].equals( "-pageSize" ))
+                    {
+                        i++;
+                        PDRectangle rectangle = createRectangle(args[i]);
+                        if (rectangle == null)
+                        {
+                            throw new IOException("Unknown argument: " + args[i]);
+                        }
+                        app.setMediaBox(rectangle);
+                    }
                     else if( args[i].equals( "-landscape" ))
                     {
                         app.setLandscape(true);
                     }                    
                     else
                     {
-                        throw new IOException( "Unknown argument:" + args[i] );
+                        throw new IOException( "Unknown argument: " + args[i] );
                     }
                 }
                 
@@ -331,6 +339,50 @@ public class TextToPDF
         }
     }
 
+    private static PDRectangle createRectangle( String paperSize )
+    {
+        if ("letter".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.LETTER;
+        }
+        else if ("legal".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.LEGAL;
+        }
+        else if ("A0".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A0;
+        }
+        else if ("A1".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A1;
+        }
+        else if ("A2".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A2;
+        }
+        else if ("A3".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A3;
+        }
+    	else if ("A4".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A4;
+        }
+        else if ("A5".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A5;
+        }
+        else if ("A6".equalsIgnoreCase(paperSize))
+        {
+            return PDRectangle.A6;
+        }
+        else
+        {
+            return null;
+        }
+    }
+
     /**
      * This will print out a message telling how to use this example.
      */
@@ -348,9 +400,18 @@ public class TextToPDF
             message.append("                         ").append(std14String).append("\n");
         }
         message.append("  -ttf <ttf file>      : The TTF font to use.\n");
-        message.append("  -fontSize <fontSize> : default: " + DEFAULT_FONT_SIZE );
+        message.append("  -fontSize <fontSize> : default: ").append(DEFAULT_FONT_SIZE).append("\n");
+        message.append("  -pageSize <pageSize> : Letter (default)\n");
+        message.append("                         Legal\n");
+        message.append("                         A0\n");
+        message.append("                         A1\n");
+        message.append("                         A2\n");
+        message.append("                         A3\n");
+        message.append("                         A4\n");
+        message.append("                         A5\n");
+        message.append("                         A6\n");
         message.append("  -landscape           : sets orientation to landscape" );
-        
+
         System.err.println(message.toString());
         System.exit(1);
     }
@@ -409,6 +470,26 @@ public class TextToPDF
     }
 
     /**
+     * Sets page size of produced PDF.
+     *
+     * @return returns the page size (media box)
+     */
+    public PDRectangle getMediaBox()
+    {
+        return mediaBox;
+    }
+
+    /**
+     * Sets page size of produced PDF.
+     *
+     * @param mediaBox
+     */
+    public void setMediaBox(PDRectangle mediaBox)
+    {
+        this.mediaBox = mediaBox;
+    }
+
+    /**
      * Tells the paper orientation.
      *
      * @return true for landscape orientation



Mime
View raw message