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

 

PDFBox是一个用于处理PDF文件的Java库,它提供了一系列的API,可以用来读取、创建和修改PDF文件。虽然PDFBox本身并不直接支持将PDF换为Word文档,但可以通过其他方式实现这个功能。 一种常见的方法是使用PDFBox读取PDF文件的内容,并将其换为纯文本格式。然后,可以使用Apache POI或其他类似的库来创建一个新的Word文档,并将纯文本内容写入其中。 以下是一个简单的示例代码,演示了如何使用PDFBox和Apache POI将PDF换为Word文档: ```java import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class PDFToWordConverter { public static void main(String[] args) { try { // 加载PDF文件 PDDocument document = PDDocument.load(new FileInputStream("input.pdf")); // 创建Word文档 XWPFDocument wordDocument = new XWPFDocument(); // 提取PDF内容 PDFTextStripper stripper = new PDFTextStripper(); String text = stripper.getText(document); // 将内容写入Word文档 XWPFParagraph paragraph = wordDocument.createParagraph(); XWPFRun run = paragraph.createRun(); run.setText(text); // 保存Word文档 FileOutputStream out = new FileOutputStream("output.docx"); wordDocument.write(out); out.close(); // 关闭PDF文档 document.close(); System.out.println("PDF换为Word成功!"); } catch (IOException e) { e.printStackTrace(); } } } ``` 请注意,这只是一个简单的示例,实际应用中可能需要更复杂的处理逻辑和错误处理。另外,换的结果可能会因PDF文件的结构和格式而有所差异。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软测小生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值