简体   繁体   English

Java使用PDFBox图像转换获取空白图像

[英]Java getting blank image using PDFBox image conversion

i'm trying to make a simple pdf reader with Java and PDFBox; 我正在尝试使用Java和PDFBox创建一个简单的pdf阅读器; In my code i convert the pages in ImageIcon and then i set it in a JLabel , the JLabel is added to a ScrollPane so at the end i have a nice scrollable page. 在我的代码中,我转换了ImageIcon中的页面,然后在JLabel中进行了设置,并将JLabel添加到了ScrollPane中,因此最后我有了一个不错的可滚动页面。

For the first page (loaded by the constructor) it works fine, but when i try to load another page from the actionPerformed function the result is a blank page, even if the try-catch give no error. 对于第一页(由构造函数加载),它工作正常,但是当我尝试从actionPerformed函数加载另一页时,即使try-catch没有给出错误,结果也为空白页。

public class PDFreader extends JFrame implements ActionListener {

    List<PDPage> Pages;
    int CurrentPage = 0;

    JButton Back, Next;
    JLabel Info, LabelImage;

    public PDFreader(String Title, PDDocument doc) throws IOException {

        this.setTitle(Title);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        getContentPane().setLayout(new BorderLayout());

        Pages = doc.getDocumentCatalog().getAllPages();
        PDPage pag = (PDPage) Pages.get(CurrentPage);

        ImageIcon PageImage = new ImageIcon(pag.convertToImage());
        LabelImage = new JLabel(PageImage);

        JScrollPane scrollPane = new JScrollPane(LabelImage);

        Back = new JButton("Previous page");
        Next = new JButton("Next page");

        Back.setEnabled(false);
        if(Pages.size()==1)
            Next.setEnabled(false);

        Back.addActionListener(this);
        Next.addActionListener(this);

        JPanel p = new JPanel();
        p.setLayout(new GridLayout(1,2));
        p.add(Back);
        p.add(Next);

        Info = new JLabel("Page 1 of "+ Pages.size(), SwingConstants.CENTER);

        getContentPane().add(scrollPane, BorderLayout.CENTER);
        getContentPane().add(p, BorderLayout.NORTH);
        getContentPane().add(Info, BorderLayout.SOUTH);

        pack();
        setVisible(true);
    }




    @Override
    public void actionPerformed(ActionEvent Ev) {
        // TODO Auto-generated method stub
        if(Ev.getSource() == Next) {
            CurrentPage++;
        }
        if(Ev.getSource() == Back) {
            CurrentPage--;
        }

        if(CurrentPage == 0)
            Back.setEnabled(false);
        else
            Back.setEnabled(true);

        if(CurrentPage == Pages.size()-1)
            Next.setEnabled(false);
        else
            Next.setEnabled(true);          


        try {
            PDPage page = (PDPage) Pages.get(CurrentPage);
            ImageIcon PageImage = new ImageIcon(page.convertToImage());
            LabelImage.setIcon(PageImage);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();        

        }

        Info.setText("Pagina "+(CurrentPage+1)+" di "+ Pages.size());

    }
}

Is there a problem because i'm trying to load it in the actionPerformed ? 是否有问题,因为我正在尝试将其加载到actionPerformed中 Any other tips? 还有其他提示吗?

You are closing the document too early, that is why. 您太早关闭文档了,这就是为什么。 I was able to reproduce the effect you get by closing the document after calling 通过调用后关闭文档,我能够重现您获得的效果

new PDFReader("Title", doc);

So one solution would be eg opening the document within the JPanel constructor instead of passing it as a parameter as you do now (pass the file instead), and close it when the JPanel is closing by adding this: 因此,一种解决方案是例如在JPanel构造函数中打开文档,而不是像现在那样将其作为参数传递(而是传递文件),并在JPanel关闭时通过添加以下内容将其关闭:

addWindowListener(new java.awt.event.WindowAdapter()
{
    @Override
    public void windowClosing(java.awt.event.WindowEvent evt)
    {
        try
        {
            document.close();
        }
        catch (IOException ex)
        {
        }
    }
});

document need to be a local final variable within your PDFReader class. 文档必须是PDFReader类中的局部最终变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM