1

我正在尝试在 SWT 中实现以下代码,但运气不佳。有人可以给我一个关于如何将 Pdf-Render 库与 SWT 一起使用的提示吗?我认为主要问题是我无法弄清楚如何将 PagePanel 附加到 SWT shell。

package pdfpaneltest;

import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PagePanel;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.*;

/**
 * An example of using the PagePanel class to show PDFs. For more advanced
 * usage including navigation and zooming, look ad the 
 * com.sun.pdfview.PDFViewer class.
 *
 * @author joshua.marinacci@sun.com
 */
public class Main {

    public static void setup() throws IOException {

        //set up the frame and panel
        JFrame frame = new JFrame("PDF Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PagePanel panel = new PagePanel();
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);

        //load a pdf from a byte buffer
        File file = new File("test.pdf");
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        FileChannel channel = raf.getChannel();
        ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY,
            0, channel.size());
        PDFFile pdffile = new PDFFile(buf);

        // show the first page
        PDFPage page = pdffile.getPage(0);
        panel.showPage(page);

    }

    public static void main(final String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    Main.setup();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        });
    }
}

取自https://pdf-renderer.dev.java.net/examples.html

谢谢!

4

4 回答 4

1

您可以使用 Swing/SWT 桥接器。eclipse.org 上有一个教程

于 2011-01-24T14:00:27.213 回答
0

如果你有一些钱可以花,试试 PdfONE,它速度更快,也可以处理嵌入式字体。

于 2011-01-24T14:20:25.403 回答
0

...或者您可以查看支持 SWT ... (GPL) 的 jPodRenderer

于 2011-01-24T14:22:05.090 回答
0

您还可以查看http://www.jpedal.org/support_siEclipse.php,它是 Eclipse 的免费 PDF 查看器插件。您可以下载包含完整工作区项目的开源代码。

于 2011-01-24T17:13:55.767 回答