pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1712095 - /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
Date Mon, 02 Nov 2015 17:28:03 GMT
Author: tilman
Date: Mon Nov  2 17:28:03 2015
New Revision: 1712095

URL: http://svn.apache.org/viewvc?rev=1712095&view=rev
Log:
PDFBOX-3081: example to draw glyph sizes in rendered images

Added:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
  (with props)

Added: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java?rev=1712095&view=auto
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
(added)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
Mon Nov  2 17:28:03 2015
@@ -0,0 +1,179 @@
+/*
+ * 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.examples.util;
+
+import java.awt.BasicStroke;
+import java.awt.Color;
+import java.awt.Graphics2D;
+import java.awt.geom.Point2D;
+import java.awt.geom.Rectangle2D;
+import java.awt.image.BufferedImage;
+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 javax.imageio.ImageIO;
+import org.apache.fontbox.util.BoundingBox;
+import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.font.PDFont;
+import org.apache.pdfbox.rendering.PDFRenderer;
+import org.apache.pdfbox.text.PDFTextStripper;
+import org.apache.pdfbox.text.TextPosition;
+
+/**
+ * This is an example on how to get some x/y coordinates of text and to show them in a rendered
+ * image.
+ *
+ * @author Ben Litchfield
+ * @author Tilman Hausherr
+ */
+public class DrawPrintTextLocations extends PDFTextStripper
+{
+    private BufferedImage image;
+    private final String filename;
+    static final int SCALE = 4;
+    private Graphics2D g2d;
+    private final PDDocument document;
+
+    /**
+     * Instantiate a new PDFTextStripper object.
+     *
+     * @param document
+     * @param filename
+     * @throws IOException If there is an error loading the properties.
+     */
+    public DrawPrintTextLocations(PDDocument document, String filename) throws IOException
+    {
+        this.document = document;
+        this.filename = filename;
+    }
+
+    /**
+     * This will print the documents data.
+     *
+     * @param args The command line arguments.
+     *
+     * @throws IOException If there is an error parsing the document.
+     */
+    public static void main(String[] args) throws IOException
+    {
+        if (args.length != 1)
+        {
+            usage();
+        }
+        else
+        {
+            PDDocument document = null;
+            try
+            {
+                document = PDDocument.load(new File(args[0]));
+
+                DrawPrintTextLocations stripper = new DrawPrintTextLocations(document, args[0]);
+                stripper.setSortByPosition(true);
+
+                for (int page = 0; page < document.getNumberOfPages(); ++page)
+                {
+                    stripper.stripPage(page);
+                }
+            }
+            finally
+            {
+                if (document != null)
+                {
+                    document.close();
+                }
+            }
+        }
+    }
+
+    private void stripPage(int page) throws IOException
+    {
+        PDFRenderer pdfRenderer = new PDFRenderer(document);
+        image = pdfRenderer.renderImage(page, SCALE);
+
+        g2d = image.createGraphics();
+        g2d.setStroke(new BasicStroke(0.1f));
+        g2d.scale(SCALE, SCALE);
+
+        setStartPage(page + 1);
+        setEndPage(page + 1);
+
+        Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
+        writeText(document, dummy);
+
+        g2d.dispose();
+
+        String imageFilename = filename;
+        int pt = imageFilename.lastIndexOf('.');
+        imageFilename = imageFilename.substring(0, pt) + "-marked-" + (page + 1) + ".png";
+        ImageIO.write(image, "png", new File(imageFilename));
+    }
+
+    /**
+     * Override the default functionality of PDFTextStripper.
+     */
+    @Override
+    protected void writeString(String string, List<TextPosition> textPositions) throws
IOException
+    {
+        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());
+
+            // in red:
+            // show rectangles with the "height" (not a real height, but used for text extraction

+            // heuristics, it is 1/2 of the bounding box height and starts at y=0)
+            Rectangle2D.Float rect = new Rectangle2D.Float(
+                    text.getXDirAdj(),
+                    (text.getYDirAdj() - text.getHeightDir()),
+                    text.getWidthDirAdj(),
+                    text.getHeightDir());
+            g2d.setColor(Color.red);
+            g2d.draw(rect);
+
+            // in blue:
+            // show rectangle with the real vertical bounds, based on the font bounding box
y values
+            // usually, the height is identical to what you see when marking text in Adobe
Reader
+            PDFont font = text.getFont();
+            BoundingBox bbox = font.getBoundingBox();
+            Point2D.Float p1 = font.getFontMatrix().transformPoint(bbox.getLowerLeftX(),
bbox.getLowerLeftY());
+            Point2D.Float p2 = font.getFontMatrix().transformPoint(bbox.getUpperRightX(),
bbox.getUpperRightY());
+
+            rect = new Rectangle2D.Float(
+                    text.getXDirAdj(),
+                    (text.getYDirAdj() - p2.y * text.getYScale()),
+                    text.getWidthDirAdj(),
+                    (p2.y - p1.y) * text.getYScale());
+            g2d.setColor(Color.blue);
+            g2d.draw(rect);
+
+        }
+    }
+
+    /**
+     * This will print the usage for this document.
+     */
+    private static void usage()
+    {
+        System.err.println("Usage: java " + DrawPrintTextLocations.class.getName() + " <input-pdf>");
+    }
+}

Propchange: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/util/DrawPrintTextLocations.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message