You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by le...@apache.org on 2010/12/11 18:49:58 UTC
svn commit: r1044686 - in /pdfbox/trunk/pdfbox/src:
main/java/org/apache/pdfbox/TextToPDF.java
test/java/org/apache/pdfbox/TestAll.java
test/java/org/apache/pdfbox/TestTextToPdf.java
Author: lehmi
Date: Sat Dec 11 17:49:57 2010
New Revision: 1044686
URL: http://svn.apache.org/viewvc?rev=1044686&view=rev
Log:
PDFBOX-914: add a new page even if the given string is empty as suggested by Neil McErlean
Added:
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestTextToPdf.java
Modified:
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java
pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java?rev=1044686&r1=1044685&r2=1044686&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/TextToPDF.java Sat Dec 11 17:49:57 2010
@@ -56,8 +56,8 @@ public class TextToPDF
PDDocument doc = null;
try
{
-
- int margin = 40;
+
+ final int margin = 40;
float height = font.getFontDescriptor().getFontBoundingBox().getHeight()/1000;
//calculate font height and increase by 5 percent.
@@ -69,8 +69,17 @@ public class TextToPDF
PDPageContentStream contentStream = null;
float y = -1;
float maxStringLength = page.getMediaBox().getWidth() - 2*margin;
+
+ // There is a special case of creating a PDF document from an empty string.
+ boolean textIsEmpty = true;
+
while( (nextLine = data.readLine()) != null )
{
+
+ // The input text is nonEmpty. New pages will be created and added
+ // to the PDF document as they are needed, depending on the length of
+ // the text.
+ textIsEmpty = false;
String[] lineWords = nextLine.trim().split( " " );
int lineIndex = 0;
@@ -94,6 +103,8 @@ public class TextToPDF
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();
doc.addPage( page );
if( contentStream != null )
@@ -122,6 +133,15 @@ public class TextToPDF
}
+
+ // If the input text was the empty string, then the above while loop will have short-circuited
+ // and we will not have added any PDPages to the document.
+ // So in order to make the resultant PDF document readable by Adobe Reader etc, we'll add an empty page.
+ if (textIsEmpty)
+ {
+ doc.addPage(page);
+ }
+
if( contentStream != null )
{
contentStream.endText();
@@ -140,7 +160,7 @@ public class TextToPDF
}
/**
- * This will create a PDF document with a single image on it.
+ * This will create a PDF document with some text in it.
* <br />
* see usage() for commandline
*
Modified: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java?rev=1044686&r1=1044685&r2=1044686&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java (original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestAll.java Sat Dec 11 17:49:57 2010
@@ -75,6 +75,7 @@ public class TestAll extends TestCase
suite.addTestSuite( TestPDDocumentInformation.class );
suite.addTestSuite( org.apache.pdfbox.pdmodel.graphics.optionalcontent.TestOptionalContentGroups.class );
suite.addTestSuite( org.apache.pdfbox.util.TestLayerUtility.class );
+ suite.addTestSuite( org.apache.pdfbox.TestTextToPdf.class );
return suite;
}
}
Added: pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestTextToPdf.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestTextToPdf.java?rev=1044686&view=auto
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestTextToPdf.java (added)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/TestTextToPdf.java Sat Dec 11 17:49:57 2010
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox;
+
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.List;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
+import org.apache.pdfbox.TextToPDF;
+
+/**
+ * Test suite for TextToPDF.
+ */
+public class TestTextToPdf extends TestCase
+{
+ /**
+ * Test class constructor.
+ *
+ * @param name The name of the test class.
+ *
+ * @throws IOException If there is an error creating the test.
+ */
+ public TestTextToPdf( String name ) throws IOException
+ {
+ super( name );
+ }
+
+ /**
+ * This test ensures that a PDF created from an empty String is still readable by Adobe Reader
+ */
+ public void testCreateEmptyPdf() throws Exception
+ {
+ TextToPDF pdfCreator = new TextToPDF();
+ StringReader reader = new StringReader("");
+ PDDocument pdfDoc = pdfCreator.createPDFFromText(reader);
+ reader.close();
+ pdfDoc.close();
+
+ // In order for the PDF document to be openable by Adobe Reader, it needs
+ // to have some pages in it. So we'll check that.
+ PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
+ List allPages = docCatalog.getAllPages();
+ assertNotNull("All Pages was unexpectedly null.", allPages);
+ assertEquals("Wrong number of pages.", 1, allPages.size());
+ }
+
+ /**
+ * Set the tests in the suite for this test class.
+ *
+ * @return the Suite.
+ */
+ public static Test suite()
+ {
+ return new TestSuite( TestTextToPdf.class );
+ }
+
+ /**
+ * Command line execution.
+ *
+ * @param args Command line arguments.
+ */
+ public static void main( String[] args )
+ {
+ String[] arg = {TestTextToPdf.class.getName() };
+ junit.textui.TestRunner.main( arg );
+ }
+}