当前位置: 首页 > Java > 正文

Java实现pdf转化为png图片

关键字:
1 星2 星3 星4 星5 星 (暂无评分)
Loading ... Loading ...
baidu_share

https://java.net/projects/pdf-renderer下载最新的PDFRenderer-0.9.1.jar

Java实现代码为:

package com.hq.png;
 
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
 
import javax.imageio.ImageIO;
 
import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import com.sun.pdfview.PDFRenderer;
 
public class TestViewPdf {
	public static void main(String[] args) {
 
 
		new TestViewPdf().Pdf_Png(1);  
    }  
 
    public void Pdf_Png(int pageNumber) {  
        int pagen = pageNumber;  
        //File file = new File("/home/develop/xq/P9.pdf");  
        File file = new File("d:/GT_24.pdf");  
        PDFFile pdffile = null;  
        // set up the PDF reading  
        try {  
            RandomAccessFile raf = new RandomAccessFile(file, "r");  
            FileChannel channel = raf.getChannel();  
            ByteBuffer buf = channel.map(FileChannel.MapMode.READ_ONLY, 0,  
                    channel.size());  
            pdffile = new PDFFile(buf);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
 
        if (pagen < pdffile.getNumPages()) {  
            return;  
        }  
 
        // print 出该 pdf 文档的页数  
        System.out.println(pdffile.getNumPages());  
 
        // 设置将第 pagen 页生成 png 图片  
        PDFPage page = pdffile.getPage(pagen);  
 
        // create and configure a graphics object  
        int width = (int) page.getBBox().getWidth();  
        int height = (int) page.getBBox().getHeight();  
        BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);  
        Graphics2D g2 = img.createGraphics();  
        //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);  
 
        // do the actual drawing  
        PDFRenderer renderer = new PDFRenderer(page, g2, new Rectangle(0, 0, width, height), null, Color.WHITE);  
        try {  
            page.waitForFinish();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        renderer.run();  
        g2.dispose();  
 
        try {  
            //ImageIO.write(img, "png", new File("/home/develop/xq/123.png"));  
        	ImageIO.write(img, "png", new File("d:/123.png"));  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
}

该程序在windows下运行正常,在linux下报空指针。

java.lang.NullPointerException
        at com.sun.pdfview.font.TTFFont.getOutline(TTFFont.java:198)
        at com.sun.pdfview.font.CIDFontType2.getOutline(CIDFontType2.java:270)
        at com.sun.pdfview.font.OutlineFont.getGlyph(OutlineFont.java:130)
        at com.sun.pdfview.font.PDFFont.getCachedGlyph(PDFFont.java:437)
        at com.sun.pdfview.font.PDFFontEncoding.getGlyphFromCMap(PDFFontEncoding.java:155)
        at com.sun.pdfview.font.PDFFontEncoding.getGlyphs(PDFFontEncoding.java:115)
        at com.sun.pdfview.font.PDFFont.getGlyphs(PDFFont.java:403)
        at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:269)
        at com.sun.pdfview.PDFTextFormat.doText(PDFTextFormat.java:301)
        at com.sun.pdfview.PDFParser.iterate(PDFParser.java:761)
        at com.sun.pdfview.BaseWatchable.run(BaseWatchable.java:101)
        at java.lang.Thread.run(Thread.java:662)

追踪源码,看会调用微软的一些绘图特性,具体不明白,放弃。

选用JPedal实现pdf转化为png图片。在http://sourceforge.net/projects/jpedal/下载jpedal_lgpl.jar

java实现代码:

package com.hq.png;
 
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
import org.jpedal.PdfDecoder;
import org.jpedal.exception.PdfException;
import org.jpedal.fonts.FontMappings;
 
public class JPedal {
 
	public static void main(String[] args) throws IOException, PdfException,
			InterruptedException {
		/**instance of PdfDecoder to convert PDF into image*/
		PdfDecoder decode_pdf = new PdfDecoder(true);
 
		/**set mappings for non-embedded fonts to use*/
		FontMappings.setFontReplacements();
 
		/**open the PDF file - can also be a URL or a byte array*/
		try {
		decode_pdf.openPdfFile("/home/user/develop/xq/P9.pdf"); //file
		//decode_pdf.openPdfFile("C:/myPDF.pdf", "password"); //encrypted file
		//decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF
		//decode_pdf.openPdfFileFromURL("http://www.mysite.com/myPDF.pdf",false);
 
		/**get page 1 as an image*/
		//page range if you want to extract all pages with a loop
		int start = 1, end = decode_pdf.getPageCount();
		for(int i=start;i<end+1;i++){
		BufferedImage img=decode_pdf.getPageAsImage(i);
		ImageIO.write(img, "png", new File("/home/user/develop/xq/123.png"));  
		}
		/**close the pdf file*/
		decode_pdf.closePdfFile();
 
		} catch (PdfException e) {
		e.printStackTrace();
		}
	}
 
 
}

在windows和linux下测试通过。

本文固定链接: http://www.chepoo.com/java-implementation-png-images-into-pdf.html | IT技术精华网

Java实现pdf转化为png图片:目前有2 条留言

  1. 板凳
    :

    找了这么久,终于找到一个稳定的了。thank you!

    [回复]
  2. 沙发
    :

    pdfbox 中文没问题,标点却都乱码,
    renderer 直接buffer出问题了,
    总算是找到一份稳定的了.
    博主,给你64个赞

    [回复]

发表评论