帮助将Java Pdf-renderer示例实现到SWT中?

时间:2011-01-24 11:24:15

标签: java swing pdf swt pdfrenderer

我正在尝试在SWT中实现以下代码,但我没有太多运气。有人能给我一个关于如何在SWT中使用Pdf-Render库的提示吗?我认为主要问题是我无法弄清楚如何将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 个答案:

答案 0 :(得分:1)

您可以使用Swing / SWT桥。 eclipse.org上有一个tutorial

答案 1 :(得分:0)

你有一些钱可以花,试试PdfONE,它更快,也可以应付嵌入字体。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

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