从 PDPage#getMediaBox() 获取空值
Getting null from PDPage#getMediaBox()
我需要使用 Mediabox 从 pdf 获取页面中的坐标,但对于某些 pdf,我得到的是 null,而对于其他 pdf,我得到的是常规 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.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
。
我需要使用 Mediabox 从 pdf 获取页面中的坐标,但对于某些 pdf,我得到的是 null,而对于其他 pdf,我得到的是常规 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.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
。