com.sun.pdfview.PDFFile Java Examples

The following examples show how to use com.sun.pdfview.PDFFile. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PdfImageReader.java    From ocular with GNU General Public License v3.0 6 votes vote down vote up
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);
	}
}
 
Example #2
Source File: PdfImageReader.java    From ocular with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @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);
	}
}
 
Example #3
Source File: PdfImageReader.java    From ocular with GNU General Public License v3.0 5 votes vote down vote up
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);
	}
}
 
Example #4
Source File: PdfImageReader.java    From ocular with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example #5
Source File: ApplicationPDF.java    From wandora with GNU General Public License v3.0 5 votes vote down vote up
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.");
    }
}