开发者

Help implementing Java Pdf-renderer example into SWT?

开发者 https://www.devze.com 2023-02-06 15:26 出处:网络
I\'m trying to implement the following code in SWT and I\'m not having much luck. Could someone give me a hint on how to use the Pdf-Render library with SWT? I think the main issue is I can\'t work ou

I'm trying to implement the following code in SWT and I'm not having much luck. Could someone give me a hint on how to use the Pdf-Render library with SWT? I think the main issue is I can't work out how to attach a PagePanel to an 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();
                }
            }
        });
    }
}

Taken from https://pdf-renderer.dev.java.net/examples.html

Thanks!


You could use the Swing/ SWT bridge. There is a tutorial on eclipse.org.


...or you may have a look at jPodRenderer which supports SWT... (GPL)


It you have some money to spend, try PdfONE, which is faster and also can cope with enbedded fonts.


You could also look at http://www.jpedal.org/support_siEclipse.php which is a free PDF viewer plugin for Eclipse. You can download the Open Source code including the full workspace project.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号