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 2020/07/15 17:25:20 UTC
svn commit: r1879898 - in /pdfbox/branches/issue45/pdfbox/src/test:
java/org/apache/pdfbox/text/TestTextStripper.java
resources/input/eu-001.pdf resources/input/eu-001.pdf-sorted.txt
resources/input/eu-001.pdf-tabula.txt resources/input/eu-001.pdf.txt
Author: tilman
Date: Wed Jul 15 17:25:19 2020
New Revision: 1879898
URL: http://svn.apache.org/viewvc?rev=1879898&view=rev
Log:
PDFBOX-4909: add files for tabula test, which uses custom height calculation
Added:
pdfbox/branches/issue45/pdfbox/src/test/resources/input/eu-001.pdf
- copied unchanged from r1879872, pdfbox/trunk/pdfbox/src/test/resources/input/eu-001.pdf
pdfbox/branches/issue45/pdfbox/src/test/resources/input/eu-001.pdf-sorted.txt
- copied unchanged from r1879872, pdfbox/trunk/pdfbox/src/test/resources/input/eu-001.pdf-sorted.txt
pdfbox/branches/issue45/pdfbox/src/test/resources/input/eu-001.pdf-tabula.txt
- copied unchanged from r1879872, pdfbox/trunk/pdfbox/src/test/resources/input/eu-001.pdf-tabula.txt
pdfbox/branches/issue45/pdfbox/src/test/resources/input/eu-001.pdf.txt
- copied unchanged from r1879872, pdfbox/trunk/pdfbox/src/test/resources/input/eu-001.pdf.txt
Modified:
pdfbox/branches/issue45/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
Modified: pdfbox/branches/issue45/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java?rev=1879898&r1=1879897&r2=1879898&view=diff
==============================================================================
--- pdfbox/branches/issue45/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java (original)
+++ pdfbox/branches/issue45/pdfbox/src/test/java/org/apache/pdfbox/text/TestTextStripper.java Wed Jul 15 17:25:19 2020
@@ -41,12 +41,17 @@ import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
+import static junit.framework.TestCase.assertFalse;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.fontbox.util.BoundingBox;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.TestPDPageTree;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.pdmodel.font.PDFontDescriptor;
+import org.apache.pdfbox.pdmodel.font.PDType3Font;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
@@ -413,7 +418,7 @@ public class TestTextStripper extends Te
private static List<String> fileToLines(File file) throws IOException
{
List<String> lines = new LinkedList<String>();
- String line = "";
+ String line;
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), ENCODING));
while ((line = in.readLine()) != null)
@@ -447,6 +452,7 @@ public class TestTextStripper extends Te
* must be empty.
*
* @throws IOException
+ * @throws URISyntaxException
*/
public void testStripByOutlineItems() throws IOException, URISyntaxException
{
@@ -610,6 +616,94 @@ public class TestTextStripper extends Te
}
}
+ public void testTabula() throws IOException
+ {
+ File pdfFile = new File("src/test/resources/input", "eu-001.pdf");
+ File outFile = new File("target/test-output", "eu-001.pdf-tabula.txt");
+ File expectedOutFile = new File("src/test/resources/input", "eu-001.pdf-tabula.txt");
+ File diffFile = new File("target/test-output", "eu-001.pdf-tabula-diff.txt");
+ PDDocument tabulaDocument = PDDocument.load(pdfFile);
+ PDFTextStripper tabulaStripper = new PDFTabulaTextStripper();
+
+ OutputStream os = new FileOutputStream(outFile);
+
+ os.write(0xEF);
+ os.write(0xBB);
+ os.write(0xBF);
+
+ Writer writer = new BufferedWriter(new OutputStreamWriter(os, ENCODING));
+ try
+ {
+ tabulaStripper.writeText(tabulaDocument, writer);
+ }
+ finally
+ {
+ writer.close();
+ }
+
+ os.close();
+
+ compareResult(expectedOutFile, outFile, pdfFile, false, diffFile);
+
+ assertFalse(bFail);
+ }
+
+ private class PDFTabulaTextStripper extends PDFTextStripper
+ {
+ PDFTabulaTextStripper() throws IOException
+ {
+ // empty
+ }
+
+ @Override
+ protected float computeFontHeight(PDFont font) throws IOException
+ {
+ BoundingBox bbox = font.getBoundingBox();
+ if (bbox.getLowerLeftY() < Short.MIN_VALUE)
+ {
+ // PDFBOX-2158 and PDFBOX-3130
+ // files by Salmat eSolutions / ClibPDF Library
+ bbox.setLowerLeftY(-(bbox.getLowerLeftY() + 65536));
+ }
+ // 1/2 the bbox is used as the height todo: why?
+ float glyphHeight = bbox.getHeight() / 2;
+
+ // sometimes the bbox has very high values, but CapHeight is OK
+ PDFontDescriptor fontDescriptor = font.getFontDescriptor();
+ if (fontDescriptor != null)
+ {
+ float capHeight = fontDescriptor.getCapHeight();
+ if (Float.compare(capHeight, 0) != 0
+ && (capHeight < glyphHeight || Float.compare(glyphHeight, 0) == 0))
+ {
+ glyphHeight = capHeight;
+ }
+ // PDFBOX-3464, PDFBOX-448:
+ // sometimes even CapHeight has very high value, but Ascent and Descent are ok
+ float ascent = fontDescriptor.getAscent();
+ float descent = fontDescriptor.getDescent();
+ if (ascent > 0 && descent < 0
+ && ((ascent - descent) / 2 < glyphHeight || Float.compare(glyphHeight, 0) == 0))
+ {
+ glyphHeight = (ascent - descent) / 2;
+ }
+ }
+
+ // transformPoint from glyph space -> text space
+ float height;
+ if (font instanceof PDType3Font)
+ {
+ height = font.getFontMatrix().transformPoint(0, glyphHeight).y;
+ }
+ else
+ {
+ height = glyphHeight / 1000;
+ }
+
+ return height;
+ }
+ }
+
/**
* Set the tests in the suite for this test class.
*