【问题标题】:JAVA pdfbox PDF to very simple HTMLJAVA pdfbox PDF到非常简单的HTML
【发布时间】:2012-06-01 14:02:35
【问题描述】:

我想构建一个将 PDF 剧本转换为 HTML 的应用程序。剧本是非常简单的文本,没有图像或其他类型的对象,但格式非常重要。 幸运的是,也没有太多的格式约定。

也就是说,我在互联网上找到了 PDFbox java 库,我想使用它,但我找不到有关如何检索有关格式(或文本坐标)信息的示例。

我需要知道边距框坐标和文本的坐标,以便我可以比较它们以检查文本是否缩进。

我希望我已经足够清楚了。

提前谢谢你!

【问题讨论】:

    标签: java html pdf pdfbox


    【解决方案1】:

    https://pdfbox.apache.org/2.0/commandline.html#extracttext

    "-html boolean false 以 HTML 格式而不是原始文本输出。"

    这看起来像你需要的。

    【讨论】:

    • 感谢您的回答,但不,它不符合我的要求。我需要知道文本坐标
    • 您的实际要求是将PDF原样转换为HTML还是从PDF中提取文本,缩进并创建HTML。您是否对 PDFBox 以外的程序持开放态度?
    • 很好,@Emily,我想知道为什么pdfbox官方网站上没有从pdf中提取html这样的示例,它可能会有所帮助。
    【解决方案2】:

    请看一下这个link。我想它会对你有所帮助。无论如何,如果链接断开,我会从那里复制代码...

    package printtextlocations;
    
    import java.io.File;
    import java.io.IOException;
    import java.text.DecimalFormat;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import org.apache.pdfbox.exceptions.InvalidPasswordException;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.pdmodel.PDPage;
     import org.apache.pdfbox.pdmodel.common.PDStream;
    import org.apache.pdfbox.util.PDFTextStripper;
    import org.apache.pdfbox.util.TextPosition;
    
    public class PrintTextLocations extends PDFTextStripper {
    
    public static StringBuilder tWord = new StringBuilder();
    public static String seek;
    public static String[] seekA;
    public static List wordList = new ArrayList();
    public static boolean is1stChar = true;
    public static boolean lineMatch;
    public static int pageNo = 1;
    public static double lastYVal;
    
    public PrintTextLocations()
            throws IOException {
        super.setSortByPosition(true);
    }
    
    public static void main(String[] args)
            throws Exception {
        PDDocument document = null;
        seekA = args[1].split(",");
        seek = args[1];
        try {
            File input = new File(args[0]);
            document = PDDocument.load(input);
            if (document.isEncrypted()) {
                try {
                    document.decrypt("");
                } catch (InvalidPasswordException e) {
                    System.err.println("Error: Document is encrypted with a password.");
                    System.exit(1);
                }
            }
            PrintTextLocations printer = new PrintTextLocations();
            List allPages = document.getDocumentCatalog().getAllPages();
    
            for (int i = 0; i < allPages.size(); i++) {
                PDPage page = (PDPage) allPages.get(i);
                PDStream contents = page.getContents();
    
                if (contents != null) {
                    printer.processStream(page, page.findResources(), page.getContents().getStream());
                }
                pageNo += 1;
            }
        } finally {
            if (document != null) {
                System.out.println(wordList);
                document.close();
            }
        }
    }
    
    @Override
    protected void processTextPosition(TextPosition text) {
        String tChar = text.getCharacter();
        System.out.println("String[" + text.getXDirAdj() + ","
                + text.getYDirAdj() + " fs=" + text.getFontSize() + " xscale="
                + text.getXScale() + " height=" + text.getHeightDir() + " space="
                + text.getWidthOfSpace() + " width="
                + text.getWidthDirAdj() + "]" + text.getCharacter());
        String REGEX = "[,.\\[\\](:;!?)/]";
        char c = tChar.charAt(0);
        lineMatch = matchCharLine(text);
        if ((!tChar.matches(REGEX)) && (!Character.isWhitespace(c))) {
            if ((!is1stChar) && (lineMatch == true)) {
                appendChar(tChar);
            } else if (is1stChar == true) {
                setWordCoord(text, tChar);
            }
        } else {
            endWord();
        }
    }
    
    protected void appendChar(String tChar) {
        tWord.append(tChar);
        is1stChar = false;
    }
    
    protected void setWordCoord(TextPosition text, String tChar) {
        tWord.append("(").append(pageNo).append(")[").append(roundVal(Float.valueOf(text.getXDirAdj()))).append(" : ").append(roundVal(Float.valueOf(text.getYDirAdj()))).append("] ").append(tChar);
        is1stChar = false;
    }
    
    protected void endWord() {
        String newWord = tWord.toString().replaceAll("[^\\x00-\\x7F]", "");
        String sWord = newWord.substring(newWord.lastIndexOf(' ') + 1);
        if (!"".equals(sWord)) {
            if (Arrays.asList(seekA).contains(sWord)) {
                wordList.add(newWord);
            } else if ("SHOWMETHEMONEY".equals(seek)) {
                wordList.add(newWord);
            }
        }
        tWord.delete(0, tWord.length());
        is1stChar = true;
    }
    
    protected boolean matchCharLine(TextPosition text) {
        Double yVal = roundVal(Float.valueOf(text.getYDirAdj()));
        if (yVal.doubleValue() == lastYVal) {
            return true;
        }
        lastYVal = yVal.doubleValue();
        endWord();
        return false;
    }
    
    protected Double roundVal(Float yVal) {
        DecimalFormat rounded = new DecimalFormat("0.0'0'");
        Double yValDub = new Double(rounded.format(yVal));
        return yValDub;
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      • 2011-04-27
      • 2016-03-26
      • 2010-09-23
      • 2012-01-18
      • 2014-02-26
      相关资源
      最近更新 更多