从PDPage获取null#getMediaBox()

时间:2016-01-19 08:07:34

标签: java pdf pdfbox

我需要使用Mediabox从pdf获取页面中的坐标,但是对于某些pdf我得到null而对于其他我得到常规Mediabox。 为什么会这样?该方法如何工作?

private void addPDF(File pdf) throws IOException, InterruptedException {
    waiting_label.setText("");
    pdf_name.setText(pdf.getName());
    all_my_p = new ArrayList<>();
    System.out.println("prova.JPanelImageAndButton.addPDF()");
    /*pddoc = null;
    cosdoc = null;*/
    PDFParser parser = new PDFParser(new FileInputStream(pdf));
    parser.parse();
    cosdoc = parser.getDocument();
    pddoc = new PDDocument(cosdoc);
    List<PDPage> list = pddoc.getDocumentCatalog().getAllPages();

    pdf_name.setText(pdf.getName());

    if (my_p != null) {
        remove(my_p);
    }
    JFrame top = (JFrame) SwingUtilities.getWindowAncestor(this);
    Dimension d = new Dimension(top.getWidth(), top.getHeight() - p.getHeight());
    for (int i = 0; i < n_page; i++) {
        PDPage pdp=list.get(i);
        System.out.println("prova.JPanelImageAndButton.addPDF()"+pdp.getMediaBox());

        final MyPanelFrame t = new MyPanelFrame(pdf.getName() + "_temp" + (i + 1) + ".png", pdp);
        t.setPreferredSize(d);
        t.setBounds(new Rectangle(10, 30, top.getWidth(), top.getHeight()));
        t.addHierarchyBoundsListener(new HierarchyBoundsListener() {
            @Override
            public void ancestorMoved(HierarchyEvent e) {
            }

            @Override
            public void ancestorResized(HierarchyEvent e) {
                t.setPreferredSize(new Dimension(top.getWidth(), top.getHeight() - p.getHeight()));
                t.setBounds(new Rectangle(10, 30, top.getWidth(), top.getWidth()));
                top.revalidate();
            }
        });
        all_my_p.add(t);
    }
    my_p = all_my_p.get(0);

    add(my_p);

    top.setSize(top.getWidth() + 1, top.getHeight() + 1);
    top.revalidate();
    top.setSize(top.getWidth() - 1, top.getHeight() - 1);
    top.revalidate();
    top.setExtendedState(JFrame.MAXIMIZED_BOTH);
    label_load.setText("");
    label_save.setText("");
    activityDone = true;

    //pddoc.close();
    //cosdoc.close();
}

这是一个例子,但对于相同的pdf,我在任何地方都使用getMediaBox()得到null。

1 个答案:

答案 0 :(得分:3)

您似乎使用了1.x.x版本的PDFBox。对于这些版本,可以预期观察到的行为,参见该方法的JavaDocs:

/**
 * A rectangle, expressed
 * in default user space units, defining the boundaries of the physical
 * medium on which the page is intended to be displayed or printed
 *
 * This will get the MediaBox at this page and not look up the hierarchy.
 * This attribute is inheritable, and findMediaBox() should probably used.
 * This will return null if no MediaBox are available at this level.
 *
 * @return The MediaBox at this level in the hierarchy.
 */
public PDRectangle getMediaBox()

此评论还提供了解决方案,改为使用findMediaBox()

/**
 * This will find the MediaBox for this page by looking up the hierarchy until
 * it finds them.
 *
 * @return The MediaBox at this level in the hierarchy.
 */
public PDRectangle findMediaBox()

如果您打算切换到PDFBox 2.0.0,您会发现getMediaBox的行为已更改,如果有必要,它已经遍历层次结构,并且不再有findMediaBox