2016-01-19 22 views
2

我需要使用Mediabox從pdf獲取頁面中的座標,但對於某些pdf,我獲得null,對於其他人,我獲得常規Mediabox。 爲什麼發生?該方法如何工作?從PDPage獲取null#getMediaBox()

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()。

回答

2

您似乎使用PDFBox的1.x.x版本。對於這些版本,觀察到的行爲是可以預期的。該方法的JavaDoc:

/** 
* 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了。

+0

好吧,它的工作。 –