Java源码示例:com.sun.pdfview.PDFFile
示例1
public static List<BufferedImage> readPdfAsImages(File pdfFile) {
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
List<BufferedImage> images = new ArrayList<BufferedImage>();
for (int pageNumber = 1; pageNumber <= pdf.getNumPages(); ++pageNumber) {
images.add(readPage(pdf, pageNumber));
}
raf.close();
return images;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例2
/**
*
* @param pdfFile
* Path to the pdf file.
* @param pageNumber
* One-based page number to read
* @return
*/
public static BufferedImage readPdfPageAsImage(File pdfFile, int pageNumber) {
if (pageNumber < 1)
throw new RuntimeException("page numbering starts with 1; '" + pageNumber + "' given");
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
BufferedImage image = readPage(pdf, pageNumber);
raf.close();
return image;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例3
public static int numPagesInPdf(File pdfFile) {
try {
RandomAccessFile raf = new RandomAccessFile(pdfFile, "r");
FileChannel channel = raf.getChannel();
ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0, channel.size());
PDFFile pdf = new PDFFile(buf);
int numPages = pdf.getNumPages();
raf.close();
return numPages;
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
示例4
private static BufferedImage readPage(PDFFile pdf, int pageNumber) {
double scale = 2.5; // because otherwise the image comes out really tiny
PDFPage page = pdf.getPage(pageNumber);
Rectangle rect = new Rectangle(0, 0, (int) page.getBBox().getWidth(), (int) page.getBBox().getHeight());
BufferedImage bufferedImage = new BufferedImage((int)(rect.width * scale), (int)(rect.height * scale), BufferedImage.TYPE_INT_RGB);
Image image = page.getImage((int)(rect.width * scale), (int)(rect.height * scale), rect, null, true, true);
Graphics2D bufImageGraphics = bufferedImage.createGraphics();
bufImageGraphics.drawImage(image, 0, 0, null);
return bufferedImage;
}
示例5
private void initialize() throws FileNotFoundException, IOException, MalformedURLException, URISyntaxException, Exception {
pdfPanel.addMouseListener(actionListener);
pdfPanel.addMouseMotionListener(actionListener);
pdfPanel.addKeyListener(actionListener);
pdfPanel.setComponentPopupMenu(menu);
byte[] pdfBytes = null;
if(DataURL.isDataURL(source)) {
pdfBytes = new DataURL(source).getData();
}
else {
URL sourceURL = new URL(source);
if("file".equalsIgnoreCase(sourceURL.getProtocol())) {
pdfBytes = Files.getByteArray(new File(sourceURL.toURI()));
}
else {
pdfBytes = IObox.fetchUrl(sourceURL);
}
}
if(pdfBytes != null) {
pdfFile = new PDFFile(ByteBuffer.wrap(pdfBytes));
if(pdfFile != null) {
pageCount = pdfFile.getNumPages();
if(pageCount > 0) {
setPageText.invoke(1, pageCount);
pdfPanel.changePage(pdfFile.getPage(0));
}
else {
throw new Exception("PDF has no pages at all. Can't view.");
}
}
}
else {
throw new Exception("Can't read data out of locator resource.");
}
}