You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2016/07/04 16:46:39 UTC
svn commit: r1751340 -
/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Author: tilman
Date: Mon Jul 4 16:46:38 2016
New Revision: 1751340
URL: http://svn.apache.org/viewvc?rev=1751340&view=rev
Log:
PDFBOX-3410: added landscape option, as wished by Marc Stuart
PDFBOX-3409: don't drop leading spaces, and handle formfeed as wished by Marc Stuart
Modified:
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java?rev=1751340&r1=1751339&r2=1751340&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/TextToPDF.java Mon Jul 4 16:46:38 2016
@@ -28,6 +28,7 @@ import org.apache.pdfbox.pdmodel.PDDocum
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
@@ -60,6 +61,7 @@ public class TextToPDF
private static final float LINE_HEIGHT_FACTOR = 1.05f;
private int fontSize = DEFAULT_FONT_SIZE;
+ private boolean landscape = false;
private PDFont font = DEFAULT_FONT;
private static final Map<String, PDType1Font> STANDARD_14 = new HashMap<String, PDType1Font>();
@@ -111,12 +113,17 @@ public class TextToPDF
final int margin = 40;
float height = font.getBoundingBox().getHeight() / FONTSCALE;
+ PDRectangle mediaBox = PDRectangle.LETTER;
+ if (landscape)
+ {
+ mediaBox = 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();
+ PDPage page = new PDPage(mediaBox);
PDPageContentStream contentStream = null;
float y = -1;
float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
@@ -132,20 +139,61 @@ public class TextToPDF
// the text.
textIsEmpty = false;
- String[] lineWords = nextLine.trim().split( " " );
+ String[] lineWords = nextLine.replaceAll("[\\n\\r]+$", "").split(" ");
int lineIndex = 0;
while( lineIndex < lineWords.length )
{
StringBuilder nextLineToDraw = new StringBuilder();
float lengthIfUsingNextWord = 0;
+ boolean ff = false;
do
{
- nextLineToDraw.append( lineWords[lineIndex] );
- nextLineToDraw.append( " " );
- lineIndex++;
+ String word1, word2 = "";
+ int indexFF = lineWords[lineIndex].indexOf('\f');
+ if (indexFF == -1)
+ {
+ word1 = lineWords[lineIndex];
+ }
+ else
+ {
+ ff = true;
+ word1 = lineWords[lineIndex].substring(0, indexFF);
+ if (indexFF < lineWords[lineIndex].length())
+ {
+ word2 = lineWords[lineIndex].substring(indexFF + 1);
+ }
+ }
+ // word1 is the part before ff, word2 after
+ // both can be empty
+ // word1 can also be empty without ff, if a line has many spaces
+ if (word1.length() > 0 || !ff)
+ {
+ nextLineToDraw.append(word1);
+ nextLineToDraw.append(" ");
+ }
+ if (!ff || word2.length() == 0)
+ {
+ lineIndex++;
+ }
+ else
+ {
+ lineWords[lineIndex] = word2;
+ }
+ if (ff)
+ {
+ break;
+ }
if( lineIndex < lineWords.length )
{
- String lineWithNextWord = nextLineToDraw.toString() + lineWords[lineIndex];
+ // need cut off at \f in next word to avoid IllegalArgumentException
+ String nextWord = lineWords[lineIndex];
+ indexFF = nextWord.indexOf('\f');
+ if (indexFF != -1)
+ {
+ nextWord = nextWord.substring(0, indexFF);
+ }
+
+ String lineWithNextWord = nextLineToDraw.toString() + " " + nextWord;
lengthIfUsingNextWord =
(font.getStringWidth( lineWithNextWord )/FONTSCALE) * fontSize;
}
@@ -156,7 +204,7 @@ public class TextToPDF
{
// We have crossed the end-of-page boundary and need to extend the
// document by another page.
- page = new PDPage();
+ page = new PDPage(mediaBox);
doc.addPage( page );
if( contentStream != null )
{
@@ -179,6 +227,18 @@ public class TextToPDF
contentStream.newLineAtOffset(0, -height);
y -= height;
contentStream.showText(nextLineToDraw.toString());
+ if (ff)
+ {
+ page = new PDPage(mediaBox);
+ doc.addPage(page);
+ contentStream.endText();
+ contentStream.close();
+ contentStream = new PDPageContentStream(doc, page);
+ contentStream.setFont(font, fontSize);
+ contentStream.beginText();
+ y = page.getMediaBox().getHeight() - margin + height;
+ contentStream.newLineAtOffset(margin, y);
+ }
}
@@ -251,6 +311,10 @@ public class TextToPDF
i++;
app.setFontSize( Integer.parseInt( args[i] ) );
}
+ else if( args[i].equals( "-landscape" ))
+ {
+ app.setLandscape(true);
+ }
else
{
throw new IOException( "Unknown argument:" + args[i] );
@@ -278,13 +342,14 @@ public class TextToPDF
message.append("Usage: jar -jar pdfbox-app-x.y.z.jar TextToPDF [options] <outputfile> <textfile>\n");
message.append("\nOptions:\n");
message.append(" -standardFont <name> : " + DEFAULT_FONT.getBaseFont() + " (default)\n");
-
+
for (String std14String : std14)
{
message.append(" " + std14String + "\n");
}
message.append(" -ttf <ttf file> : The TTF font to use.\n");
message.append(" -fontSize <fontSize> : default: " + DEFAULT_FONT_SIZE );
+ message.append(" -landscape : sets orientation to landscape" );
System.err.println(message.toString());
System.exit(1);
@@ -342,4 +407,24 @@ public class TextToPDF
{
this.fontSize = aFontSize;
}
+
+ /**
+ * Tells the paper orientation.
+ *
+ * @return
+ */
+ public boolean isLandscape()
+ {
+ return landscape;
+ }
+
+ /**
+ * Sets paper orientation.
+ *
+ * @param landscape
+ */
+ public void setLandscape(boolean landscape)
+ {
+ this.landscape = landscape;
+ }
}