pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1861373 - in /pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger: PDFDebugger.java pagepane/PagePane.java ui/TextDialog.java ui/ViewMenu.java
Date Sat, 15 Jun 2019 11:42:50 GMT
Author: tilman
Date: Sat Jun 15 11:42:50 2019
New Revision: 1861373

URL: http://svn.apache.org/viewvc?rev=1861373&view=rev
Log:
PDFBOX-2941: added simple text extraction in separate window

Added:
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/TextDialog.java
      - copied, changed from r1860828, pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/LogDialog.java
Modified:
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/PagePane.java
    pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/ViewMenu.java

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java?rev=1861373&r1=1861372&r2=1861373&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/PDFDebugger.java Sat Jun
15 11:42:50 2019
@@ -106,6 +106,7 @@ import org.apache.pdfbox.debugger.ui.Pag
 import org.apache.pdfbox.debugger.ui.ReaderBottomPanel;
 import org.apache.pdfbox.debugger.ui.RecentFiles;
 import org.apache.pdfbox.debugger.ui.RotationMenu;
+import org.apache.pdfbox.debugger.ui.TextDialog;
 import org.apache.pdfbox.debugger.ui.Tree;
 import org.apache.pdfbox.debugger.ui.ViewMenu;
 import org.apache.pdfbox.debugger.ui.ZoomMenu;
@@ -252,6 +253,8 @@ public class PDFDebugger extends JFrame
         LogDialog.init(viewer, viewer.statusBar.getLogLabel());
         System.setProperty("org.apache.commons.logging.Log", "org.apache.pdfbox.debugger.ui.DebugLog");
 
+        TextDialog.init(viewer);
+
         // trigger premature initializations for more accurate rendering benchmarks
         // See discussion in PDFBOX-3988
         if (PDType1Font.COURIER.isStandard14())

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/PagePane.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/PagePane.java?rev=1861373&r1=1861372&r2=1861373&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/PagePane.java
(original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/pagepane/PagePane.java
Sat Jun 15 11:42:50 2019
@@ -53,6 +53,7 @@ import java.util.Map.Entry;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.TimeUnit;
 import org.apache.pdfbox.debugger.ui.HighResolutionImageIcon;
+import org.apache.pdfbox.debugger.ui.TextDialog;
 import org.apache.pdfbox.pdmodel.common.PDRectangle;
 import org.apache.pdfbox.pdmodel.interactive.action.PDAction;
 import org.apache.pdfbox.pdmodel.interactive.action.PDActionGoTo;
@@ -65,6 +66,7 @@ import org.apache.pdfbox.pdmodel.interac
 import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.PDPageDestination;
 import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
 import org.apache.pdfbox.pdmodel.interactive.form.PDField;
+import org.apache.pdfbox.text.PDFTextStripper;
 
 /**
  * Display the page number and a page rendering.
@@ -226,6 +228,30 @@ public class PagePane implements ActionL
         {
             startRendering();
         }
+        else if (ViewMenu.isExtractText(actionEvent))
+        {
+            startExtracting();
+        }
+    }
+
+    private void startExtracting()
+    {
+        TextDialog textDialog = TextDialog.instance();
+        textDialog.setSize(800, 400);
+        textDialog.setVisible(true);
+        textDialog.setLocation(getPanel().getLocationOnScreen().x + getPanel().getWidth()
/ 2,
+                               getPanel().getLocationOnScreen().y + getPanel().getHeight()
/ 2);
+        try
+        {
+            PDFTextStripper stripper = new PDFTextStripper();
+            stripper.setStartPage(pageIndex + 1);
+            stripper.setEndPage(pageIndex + 1);
+            textDialog.setText(stripper.getText(document));
+        }
+        catch (IOException ex)
+        {
+            throw new RuntimeException(ex);
+        }
     }
 
     private void startRendering()

Copied: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/TextDialog.java
(from r1860828, pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/LogDialog.java)
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/TextDialog.java?p2=pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/TextDialog.java&p1=pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/LogDialog.java&r1=1860828&r2=1861373&rev=1861373&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/LogDialog.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/TextDialog.java Sat
Jun 15 11:42:50 2019
@@ -1,9 +1,10 @@
 /*
- * Copyright 2016 The Apache Software Foundation.
- *
- * Licensed 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
+ * 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
  *
@@ -15,189 +16,66 @@
  */
 package org.apache.pdfbox.debugger.ui;
 
-import java.awt.Color;
+import java.awt.Container;
 import java.awt.Frame;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.util.ArrayList;
-import java.util.List;
 import javax.swing.JDialog;
-import javax.swing.JLabel;
 import javax.swing.JScrollPane;
 import javax.swing.JTextPane;
-import javax.swing.text.BadLocationException;
-import javax.swing.text.SimpleAttributeSet;
-import javax.swing.text.StyleConstants;
-import javax.swing.text.StyledDocument;
 
 /**
- * Custom log dialog.
+ * Window for text extraction result.
  *
+ * @author Tilman Hausherr
  * @author John Hewson
  */
-public class LogDialog extends JDialog
+public class TextDialog extends JDialog
 {
-    private static LogDialog instance;
-    
-    public static void init(Frame owner, JLabel logLabel)
+    private static TextDialog instance;
+
+    public static void init(Frame owner)
     {
-        instance = new LogDialog(owner, logLabel);
+        instance = new TextDialog(owner);
     }
 
-    public static LogDialog instance()
+    public static TextDialog instance()
     {
         return instance;
     }
 
-    private final JLabel logLabel;
     private final JTextPane textPane;
     private final JScrollPane scrollPane;
-    private int fatalCount = 0;
-    private int errorCount = 0;
-    private int warnCount = 0;
-    private int otherCount = 0;
-    private int exceptionCount = 0;
-    
-    private LogDialog(Frame owner, JLabel logLabel)
+
+    private TextDialog(Frame owner)
     {
         super(owner);
-        this.logLabel = logLabel;
-        
+
         textPane = new JTextPane();
         scrollPane = new JScrollPane(textPane);
         getContentPane().add(scrollPane);
-        
-        this.pack();
+        pack();
     }
-    
-    public void log(String name, String level, Object o, Throwable throwable)
-    {
-        StyledDocument doc = textPane.getStyledDocument();
-        
-        String levelText;
-        SimpleAttributeSet levelStyle = new SimpleAttributeSet();
-        switch (level)
-        {
-            case "fatal":
-                levelText = "Fatal";
-                StyleConstants.setForeground(levelStyle, Color.WHITE);
-                StyleConstants.setBackground(levelStyle, Color.BLACK);
-                fatalCount++;
-                break;
-            case "error":
-                levelText = "Error";
-                StyleConstants.setForeground(levelStyle, new Color(0xFF291F));
-                StyleConstants.setBackground(levelStyle, new Color(0xFFF0F0));
-                errorCount++;
-                break;
-            case "warn":
-                levelText = "Warning";
-                StyleConstants.setForeground(levelStyle, new Color(0x614201));
-                StyleConstants.setBackground(levelStyle, new Color(0xFFFCE5));
-                warnCount++;
-                break;
-            case "info":
-                levelText = "Info";
-                StyleConstants.setForeground(levelStyle, new Color(0x203261));
-                StyleConstants.setBackground(levelStyle, new Color(0xE2E8FF));
-                otherCount++;
-                break;
-            case "debug":
-                levelText = "Debug";
-                StyleConstants.setForeground(levelStyle, new Color(0x32612E));
-                StyleConstants.setBackground(levelStyle, new Color(0xF4FFEC));
-                otherCount++;
-                break;
-            case "trace":
-                levelText = "Trace";
-                StyleConstants.setForeground(levelStyle, new Color(0x64438D));
-                StyleConstants.setBackground(levelStyle, new Color(0xFEF3FF));
-                otherCount++;
-                break;
-            default:
-                throw new Error(level);
-        }
-
-        SimpleAttributeSet nameStyle = new SimpleAttributeSet();
-        StyleConstants.setForeground(nameStyle, new Color(0x6A6A6A));
-
-        String shortName = name.substring(name.lastIndexOf('.') + 1);
-        String message = o == null ? "(null)" : o.toString();
-        
-        if (throwable != null)
-        {
-            StringWriter sw = new StringWriter();
-            throwable.printStackTrace(new PrintWriter(sw));
-            message += "\n    " + sw.toString();
-            exceptionCount++;
-        }
-        
-        try
-        {
-            doc.insertString(doc.getLength(), " " + levelText + " ", levelStyle);
-            doc.insertString(doc.getLength(), " [" + shortName + "]", nameStyle);
-            doc.insertString(doc.getLength(), " " + message + "\n", null);
-        }
-        catch (BadLocationException e)
-        {
-            throw new Error(e);
-        }
-        textPane.setCaretPosition(doc.getLength());
-
-        // update status bar with new counts
-        updateStatusBar();
-    }
-    
-    private void updateStatusBar()
-    {
-        List<String> infos = new ArrayList<>();
-
-        if (exceptionCount > 0)
-        {
-            infos.add(exceptionCount + " exception" + (errorCount > 1 ? "s" : ""));
-        }
-
-        if (fatalCount > 0)
-        {
-            infos.add(errorCount + " error" + (errorCount > 1 ? "s" : ""));
-        }
-
-        if (errorCount > 0)
-        {
-            infos.add(errorCount + " error" + (errorCount > 1 ? "s" : ""));
-        }
-
-        if (warnCount > 0)
-        {
-            infos.add(warnCount + " warning" + (warnCount > 1 ? "s" : ""));
-        }
-
-        if (otherCount > 0)
-        {
-            infos.add(otherCount + " message" + (otherCount > 1 ? "s" : ""));
-        }
-        
-        String info = "";
-        for (String str : infos)
-        {
-            if (info.length() > 0)
-            {
-                info += ", ";
-            }
-            info += str;
-        }
-        
-        logLabel.setText(info);
-    }
-    
+
     public void clear()
     {
-        fatalCount = 0;
-        errorCount = 0;
-        warnCount = 0;
-        otherCount = 0;
-        exceptionCount = 0;
         textPane.setText("");
-        logLabel.setText("");
+    }
+
+    public void setText(String text)
+    {
+        textPane.setText(text);
+    }
+
+    // these two just to avoid the "overridable method call in constructor" warning
+
+    @Override
+    public final Container getContentPane()
+    {
+        return super.getContentPane();
+    }
+
+    @Override
+    public final void pack()
+    {
+        super.pack();
     }
 }

Modified: pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/ViewMenu.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/ViewMenu.java?rev=1861373&r1=1861372&r2=1861373&view=diff
==============================================================================
--- pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/ViewMenu.java (original)
+++ pdfbox/trunk/debugger/src/main/java/org/apache/pdfbox/debugger/ui/ViewMenu.java Sat Jun
15 11:42:50 2019
@@ -34,6 +34,7 @@ public class ViewMenu extends MenuBase
     private static final String SHOW_FONT_BBOX = "Show Approximate Text Bounds";
     private static final String SHOW_GLYPH_BOUNDS = "Show Glyph Bounds";
     private static final String ALLOW_SUBSAMPLING = "Allow subsampling";            
+    private static final String EXTRACT_TEXT = "Extract Text";            
 
     private JMenuItem viewModeItem;
     private JCheckBoxMenuItem showTextStripper;
@@ -41,6 +42,7 @@ public class ViewMenu extends MenuBase
     private JCheckBoxMenuItem showFontBBox;
     private JCheckBoxMenuItem showGlyphBounds;
     private JCheckBoxMenuItem allowSubsampling;
+    private static JMenuItem extractTextMenuItem;
     
     private final PDFDebugger pdfDebugger;
 
@@ -123,7 +125,18 @@ public class ViewMenu extends MenuBase
     {
         return instance.showGlyphBounds.isSelected();
     }
-    
+
+    /**
+     * Tell whether the "Extract Text" menu entry was hit.
+     *
+     * @param actionEvent
+     * @return true if the "Extract Text" menu entry was hit.
+     */
+    public static boolean isExtractText(ActionEvent actionEvent)
+    {
+        return extractTextMenuItem.equals(actionEvent.getSource());
+    }
+
     /**
      * State if subsampling for image rendering shall be used.
      * 
@@ -202,6 +215,12 @@ public class ViewMenu extends MenuBase
         allowSubsampling.setEnabled(false);
         viewMenu.add(allowSubsampling);
 
+        viewMenu.addSeparator();
+
+        extractTextMenuItem = new JMenuItem(EXTRACT_TEXT);
+        extractTextMenuItem.setEnabled(false);
+        viewMenu.add(extractTextMenuItem);
+
         return viewMenu;
     }
 }



Mime
View raw message