Estou tentando usar a biblioteca pdfbox (de grátis) da apache para exibir um pdf em uma aplicação em JavaFX, uma vez que o Javafx nao faz isso nativamente… Só que eu armazeno meus pdf’s diretamente no banco de dados (derby) em uma coluna do tipo “blob”.
Eu consigo criar o PDDocument a partir de um File assim:
PDF.Java
package br.com.ddavel.swynolibrary;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Path;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
public class PDF {
private PDDocument document;
private PDFRenderer renderer;
public PDF(Path pdf) {
try {
document = PDDocument.load(pdf.toFile());
renderer = new PDFRenderer(document);
} catch (IOException ex) {
new Alerta().addErro(this.getClass(), "PDF(Path path)", ex);
}
}
public int numPages() {
return document.getPages().getCount();
}
public Image getImage(int pageNumber) {
BufferedImage pageImage;
try {
pageImage = renderer.renderImage(pageNumber);
return SwingFXUtils.toFXImage(pageImage, null);
} catch (IOException ex) {
new Alerta().addErro(this.getClass(), "getImage(int pageNumber)", ex);
return null;
}
}
}
Só que como eu disse eu gostaria de trabalhar com os bytes para não ter que salvar o arquivo e trabalhar direto no banco… Como eu faria algo parecido com isso?
byte[] pdfBytes = (byte[]) new ArquivoDao().getArquivoPorId(idDoArquivo);
document = PDDocument.load(pdfBytes);