Java+PDFBox将PDF转成图片

废话少说直接上代码:
 

package com.pdf.test;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import com.lowagie.text.pdf.PdfReader;

public class pdf2ImageDemo {
	public static void main(String[] args) {
		String outPutPath = "D:/PDF/Data.pdf";
		String dstImgFolder = "D:\\Developer\\eclipse2017\\"
				+ "pdf-logoImage-testing\\src\\test\\resources\\images";
		long start = System.currentTimeMillis();
		pdf2ImageDemo(outPutPath, dstImgFolder, 96);
		long end = System.currentTimeMillis();
		System.out.println("总共耗时:" + (end - start));
	}

	/***
	 * PDF文件转PNG/JPEG图片
	 * @param PdfFilePathpdf完整路径
	 * @param imgFilePath图片存放的文件夹
	 * @param dpi越大转换后越清晰,相对转换速度越慢,一般电脑默认96dpi
	 */
	public static void pdf2ImageDemo(String PdfFilePath, 
			String dstImgFolder, int dpi) {
		File file = new File(PdfFilePath);
		PDDocument pdDocument;
		try {
			String imgPDFPath = file.getParent();
			int dot = file.getName().lastIndexOf('.');
			// 获取图片文件名
			String imagePDFName = file.getName().substring(0, dot); 
			String imgFolderPath = null;
			if (dstImgFolder.equals("")) {
				// 获取图片存放的文件夹路径
				imgFolderPath = imgPDFPath + File.separator + imagePDFName;
			} else {
				imgFolderPath = dstImgFolder + File.separator + imagePDFName;
			}

			if (createDirectory(imgFolderPath)) {
				pdDocument = PDDocument.load(file);
				PDFRenderer renderer = new PDFRenderer(pdDocument);
				PdfReader reader = new PdfReader(PdfFilePath);
				int pages = reader.getNumberOfPages();// 获取PDF页数
				System.out.println("PDF page number is:" + pages);
				StringBuffer imgFilePath = null;
				for (int i = 0; i < pages; i++) {
					String imgFilePathPrefix = imgFolderPath
							+ File.separator + imagePDFName;
					imgFilePath = new StringBuffer();
					imgFilePath.append(imgFilePathPrefix);
					imgFilePath.append("_");
					imgFilePath.append(String.valueOf(i + 1));
					imgFilePath.append(".png");// PNG
					File dstFile = new File(imgFilePath.toString());
					BufferedImage image = renderer.renderImageWithDPI(i, dpi);
					ImageIO.write(image, "png", dstFile);// PNG
				}
				System.out.println("PDF文档转PNG图片成功!");
			} else {
				System.out.println("PDF文档转PNG图片失败:"
			+ "创建" + imgFolderPath + "失败");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static boolean createDirectory(String folder) {
		File dir = new File(folder);
		if (dir.exists()) {
			return true;
		} else {
			return dir.mkdirs();
		}
	}
}

 

发布了69 篇原创文章 · 获赞 117 · 访问量 19万+
展开阅读全文

pdfBoxpdf文档插入图片时抛出异常

12-29

用pdfBox给pdf文档插入图片时,eclipse本地run as application开发,代码抛出异常java.lang.IllegalStateException 代码如下: ********************************************** public class pdfdemo { public static void main(String args[]) { pdfdemo pdf = new pdfdemo(); //插入图片 try { pdf.AddImagetoPDF("F:\\yunsign\\pdfwithText.pdf", "F:\\yunsign\\userseal.jpg", "F:\\yunsign\\pdfwithImage.pdf"); } catch (COSVisitorException | IOException e) { e.printStackTrace(); } } //插入函数 public void AddImagetoPDF(String inputFile, String image, String outputFile) throws IOException, COSVisitorException { PDDocument doc = null; try { doc = PDDocument.load(inputFile); PDPage page = (PDPage) doc.getDocumentCatalog().getAllPages().get(0); PDXObjectImage ximage = null; ximage = new PDJpeg(doc, new FileInputStream(image)); //报错行*******************//报错行 PDPageContentStream contentStream = new PDPageContentStream(doc, page, true, true); float scale = 0.5f; contentStream.drawXObject(ximage, 20, 20, ximage.getWidth() * scale, ximage.getHeight() * scale); //contentStream.drawImage(ximage, 20, 20); contentStream.close(); doc.save(outputFile); } finally { if (doc != null) { doc.close(); } } } } *********************************************************************************** 报错截图: ![图片说明](https://img-ask.csdn.net/upload/201612/29/1483002135_876334.png) 给pdf文档插入图片的代码,我是直接在pdfbox组件中招的源代码的例子,怎么还会报错呢? 网上搜了下,java.lang.IllegalStateException异常,一般是在web容器生成的servlet代码中有out.write(””),这个和JSP中调用的response.getOutputStream()冲突,可是我的代码压根儿没部署到web端,只是在本地run as javaApplication 希望知道的人帮我解决一下,感激不尽。 问答

pdf转png图片遇到的问题

07-13

因为有好几种方法,但大都有问题,试用pdfbox转换的时候PDF竖线都很粗 ![图片说明](https://img-ask.csdn.net/upload/201807/10/1531211655_380731.png) 因不知什么原因导致,试用了第二种方法icepdf转换,但都是根据网上的代码转换的,PDF只有框架,没有文字 ![图片说明](https://img-ask.csdn.net/upload/201807/10/1531211665_963513.png) pdf-renderer也试了,但因问题是需要合作商修改代码所以没有使用 pdfbox的代码片段: File file = new File(pdfPath); try { PDDocument doc = PDDocument.load(file); org.apache.pdfbox.rendering.PDFRenderer renderer = new org.apache.pdfbox.rendering.PDFRenderer(doc); int pageCount = doc.getNumberOfPages(); for (int i = 0; i < pageCount; i++) { BufferedImage image = renderer.renderImageWithDPI(i, 300); // Windows native DPI File file1 = new File(path); ImageIO.write(image, "JPG", file1); } } catch (IOException e) { e.printStackTrace(); } ``` icepdf的代码片段: Document document = new Document(); document.setFile(pdfPath); float scale = 2.5f;//缩放比例 float rotation = 0f;//旋转角度 for (int i = 0; i < document.getNumberOfPages(); i++) { BufferedImage image = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale); PageText text = document.getPageText(0); System.out.println("pagetext"+text); RenderedImage rendImage = image; try { String imgName = i + ".png"; System.out.println(imgName); File file = new File(path + imgName); ImageIO.write(rendImage, "png", file); } catch (IOException e) { e.printStackTrace(); } image.flush(); } document.dispose(); ``` 目前是偏向于PDFbox来进行转换的,但希望竖线粗的问题能解决一下 有没有大佬能帮忙解决一下,万分感谢! 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 成长之路 设计师: Amelia_0503

分享到微信朋友圈

×

扫一扫,手机浏览