¿Ayuda a implementar el ejemplo de Java PDF-Renderer en SWT?
-
23-10-2019 - |
Pregunta
Estoy tratando de implementar el siguiente código en SWT y no tengo mucha suerte. ¿Alguien podría darme una pista sobre cómo usar la biblioteca PDF-Render con SWT? Creo que el problema principal es que no puedo resolver cómo adjuntar un PagePanel a un shell SWT.
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();
}
}
});
}
}
Tomado de https://pdf-renderer.dev.java.net/examples.html
¡Gracias!
Solución
... o puede echar un vistazo a JPodrenderer que admite SWT ... (GPL)
Otros consejos
Podrías usar el puente Swing/ SWT. Hay un tutorial en eclipse.org.
Tiene algo de dinero para gastar, pruebe PDFone, que es más rápido y también puede hacer frente a las fuentes ensaladas.
También podrías mirar http://www.jpedal.org/support_sieclipse.php que es un complemento de visor PDF gratuito para Eclipse. Puede descargar el código de código abierto, incluido el proyecto completo del espacio de trabajo.