失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > java pdf png_Java实现pdf转化为png图片

java pdf png_Java实现pdf转化为png图片

时间:2020-05-11 05:00:48

相关推荐

java pdf png_Java实现pdf转化为png图片

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图片。在/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("/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

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();

}

}

}

如果觉得《java pdf png_Java实现pdf转化为png图片》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。