|
|
@@ -1,9 +1,8 @@
|
|
|
package me.zhengjie.base.util;
|
|
|
|
|
|
-
|
|
|
import java.io.*;
|
|
|
|
|
|
-import org.apache.pdfbox.io.RandomAccessBuffer;
|
|
|
+import org.apache.pdfbox.multipdf.PDFMergerUtility;
|
|
|
import org.apache.pdfbox.pdfparser.PDFParser;
|
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
@@ -13,50 +12,33 @@ import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
|
|
import org.apache.pdfbox.text.PDFTextStripper;
|
|
|
|
|
|
public class PdfUtils {
|
|
|
-
|
|
|
-
|
|
|
- public String readPdf(String fileName) {
|
|
|
-
|
|
|
- File file = new File(fileName);
|
|
|
- FileInputStream in = null;
|
|
|
- String content = null;
|
|
|
- try {
|
|
|
- in = new FileInputStream(fileName);
|
|
|
- PDFParser parser = new PDFParser(new RandomAccessBuffer(in)); // 新建一个PDF解析器对象
|
|
|
- parser.parse(); // 对PDF文件进行解析
|
|
|
- PDDocument pdfdocument = parser.getPDDocument(); // 获取解析后得到的PDF文档对象
|
|
|
- PDFTextStripper stripper = new PDFTextStripper(); // 新建一个PDF文本剥离器
|
|
|
- content = stripper.getText(pdfdocument); // 从PDF文档对象中剥离文本
|
|
|
-
|
|
|
- } catch (Exception e) {
|
|
|
- System.out.println("读取PDF文件" + file.getAbsolutePath() + "生失败!" + e);
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- return content;
|
|
|
- }
|
|
|
-
|
|
|
- public String createPDF (String fileName, String content) throws IOException{
|
|
|
-
|
|
|
- PDDocument document = new PDDocument();
|
|
|
- PDPage page = new PDPage();
|
|
|
- document.addPage(page);
|
|
|
- PDFont font = PDType1Font.HELVETICA;
|
|
|
-
|
|
|
- PDPageContentStream contentStream = new PDPageContentStream(document, page);
|
|
|
- contentStream.beginText();
|
|
|
- contentStream.setFont(font, 14);
|
|
|
- contentStream.moveTextPositionByAmount(100, 700);
|
|
|
- contentStream.drawString(content);
|
|
|
-
|
|
|
- contentStream.endText();
|
|
|
- contentStream.close();
|
|
|
-
|
|
|
- try {
|
|
|
- document.save(fileName);
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- document.close();
|
|
|
- return "";
|
|
|
- }
|
|
|
+ private static String[] getPdfs(String fileAddress) throws IOException {
|
|
|
+ File file = new File(fileAddress);
|
|
|
+ String[] pdfs;
|
|
|
+
|
|
|
+ if (file.isDirectory()) {
|
|
|
+ pdfs = file.list();
|
|
|
+ return pdfs;
|
|
|
+ } else {
|
|
|
+ throw new IOException("输入的路径有问题");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+
|
|
|
+// PDFMergerUtility mergePdf = new PDFMergerUtility();
|
|
|
+// System.out.println("请输入要合并的PDF文件所在的文件夹路径");
|
|
|
+// String fileAddress = in.nextLine();
|
|
|
+//
|
|
|
+// System.out.println("你输入的路径是:" + fileAddress);
|
|
|
+// String destinationFileName = "javaweb2020.pdf";
|
|
|
+//
|
|
|
+// String[] pdfs = getPdfs(fileAddress);
|
|
|
+// for (int i = 0; i < pdfs.length; i++)
|
|
|
+// mergePdf.addSource(fileAddress + File.separator + pdfs[i]);
|
|
|
+// mergePdf.setDestinationFileName(destinationFileName);
|
|
|
+// System.out.println("合并比较费时间,请等待个几分钟吧!");
|
|
|
+// mergePdf.mergeDocuments();
|
|
|
+// System.out.print("合并完成");
|
|
|
+ }
|
|
|
}
|