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/06/28 08:40:59 UTC
svn commit: r1687991 -
/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java
Author: jahewson
Date: Sun Jun 28 06:40:58 2015
New Revision: 1687991
URL: http://svn.apache.org/r1687991
Log:
PDFBOX-2843: Make PrintTextLocations work again
Modified:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java
Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java?rev=1687991&r1=1687990&r2=1687991&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/PrintTextLocations.java Sun Jun 28 06:40:58 2015
@@ -16,15 +16,16 @@
*/
package org.apache.pdfbox.examples.util;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Writer;
+import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
-import org.apache.pdfbox.pdmodel.PDPage;
-import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;
-import java.io.File;
-import java.io.IOException;
-
/**
* This is an example on how to get some x/y coordinates of text.
*
@@ -35,13 +36,12 @@ import java.io.IOException;
public class PrintTextLocations extends PDFTextStripper
{
/**
- * Default constructor.
+ * Instantiate a new PDFTextStripper object.
*
- * @throws IOException If there is an error loading text stripper properties.
+ * @throws IOException If there is an error loading the properties.
*/
public PrintTextLocations() throws IOException
{
- super.setSortByPosition( true );
}
/**
@@ -63,18 +63,14 @@ public class PrintTextLocations extends
try
{
document = PDDocument.load( new File(args[0]) );
- PrintTextLocations printer = new PrintTextLocations();
- int pageNum = 0;
- for( PDPage page : document.getPages() )
- {
- pageNum++;
- System.out.println( "Processing page: " + pageNum );
- PDStream contents = page.getStream();
- if( contents != null )
- {
- printer.processPage(page);
- }
- }
+
+ PDFTextStripper stripper = new PrintTextLocations();
+ stripper.setSortByPosition( true );
+ stripper.setStartPage( 0 );
+ stripper.setEndPage( document.getNumberOfPages() );
+
+ Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
+ stripper.writeText(document, dummy);
}
finally
{
@@ -87,20 +83,21 @@ public class PrintTextLocations extends
}
/**
- * A method provided as an event interface to allow a subclass to perform
- * some specific functionality when text needs to be processed.
- *
- * @param text The text to be processed
+ * Override the default functionality of PDFTextStripper.
*/
- protected void processTextPosition( TextPosition text )
+ @Override
+ protected void writeString(String string, List<TextPosition> textPositions) throws IOException
{
- System.out.println( "String[" + text.getXDirAdj() + "," +
- text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale=" +
- text.getXScale() + " height=" + text.getHeightDir() + " space=" +
- text.getWidthOfSpace() + " width=" +
- text.getWidthDirAdj() + "]" + text.getUnicode() );
+ for (TextPosition text : textPositions)
+ {
+ System.out.println( "String[" + text.getXDirAdj() + "," +
+ text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale=" +
+ text.getXScale() + " height=" + text.getHeightDir() + " space=" +
+ text.getWidthOfSpace() + " width=" +
+ text.getWidthDirAdj() + "]" + text.getUnicode() );
+ }
}
-
+
/**
* This will print the usage for this document.
*/
@@ -108,5 +105,4 @@ public class PrintTextLocations extends
{
System.err.println( "Usage: java org.apache.pdfbox.examples.pdmodel.PrintTextLocations <input-pdf>" );
}
-
}