From 56362224e2e89946942e2242713e89cd3d08710c Mon Sep 17 00:00:00 2001
From: zhanghai <120228220@qq.com>
Date: Sat, 11 Nov 2023 17:03:12 +0800
Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=B4=A2=E5=BC=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 +
.../com/docus/sw/fenpan/MyApplication.java | 43 ++-
src/main/java/com/docus/sw/fenpan/Roll.java | 4 +
.../java/com/docus/sw/souyin/ExcelUtil.java | 82 ++++--
.../com/docus/sw/souyin/IndexPageRow.java | 2 +-
.../java/com/docus/sw/souyin/IndexPlate.java | 21 +-
.../com/docus/sw/souyin/MesEasyExcel.java | 17 +-
.../java/com/docus/sw/souyin/PdfToPic.java | 26 ++
.../java/com/docus/sw/souyin/PdfUtil.java | 84 +++++-
.../com/docus/sw/souyin/SuoyinService.java | 255 ++++++++++++++++--
10 files changed, 496 insertions(+), 43 deletions(-)
diff --git a/pom.xml b/pom.xml
index bbc2f02..9f1d6a9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -58,6 +58,11 @@
2.0.23
bundle
+
+ com.google.code.gson
+ gson
+ 2.10
+
diff --git a/src/main/java/com/docus/sw/fenpan/MyApplication.java b/src/main/java/com/docus/sw/fenpan/MyApplication.java
index 1fe8080..a3522c3 100644
--- a/src/main/java/com/docus/sw/fenpan/MyApplication.java
+++ b/src/main/java/com/docus/sw/fenpan/MyApplication.java
@@ -1,6 +1,7 @@
package com.docus.sw.fenpan;
import com.docus.sw.Config;
+import com.docus.sw.souyin.SuoyinService;
import javax.swing.*;
import java.awt.*;
@@ -308,7 +309,9 @@ public class MyApplication {
leftIndexGbc.gridy = 2;
leftIndexGbc.gridwidth = 2;
leftIndexGbc.anchor = GridBagConstraints.CENTER;
- leftIndexPanel.add(new JButton("开始生成索引"), leftIndexGbc);
+ JButton jButton = new JButton("开始生成索引");
+ leftIndexPanel.add(jButton, leftIndexGbc);
+
JTextArea indexLogTextArea = new JTextArea();
JScrollPane indexLogScrollPane = new JScrollPane(indexLogTextArea);
@@ -319,6 +322,44 @@ public class MyApplication {
indexPanel.add(leftIndexPanel, BorderLayout.WEST);
indexPanel.add(rightIndexPanel, BorderLayout.CENTER);
+
+
+ jButton.addActionListener(new ActionListener() {
+ @Override
+ public void actionPerformed(ActionEvent e) {
+ DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
+ // 在EDT上执行更新操作
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+
+ indexLogTextArea.append(df.format(new Date())+": 索引开始。。。"+ "\n");
+ }
+ });
+ // 启动一个异步任务
+
+ Thread workerThread = new Thread(() -> {
+ // 执行长时间运行的业务操作
+
+ // 模拟耗时操作
+ new SuoyinService().index(filePathField.getText());
+
+ SwingUtilities.invokeLater(new Runnable() {
+ @Override
+ public void run() {
+ // 在EDT上更新UI
+ indexLogTextArea.append(df.format(new Date())+": 索引结束。。。"+ "\n");
+ }
+ });
+ });
+
+ workerThread.start(); // 启动线程
+
+ }
+
+ });
+
+
return indexPanel;
}
}
diff --git a/src/main/java/com/docus/sw/fenpan/Roll.java b/src/main/java/com/docus/sw/fenpan/Roll.java
index 2d82660..ec0d442 100644
--- a/src/main/java/com/docus/sw/fenpan/Roll.java
+++ b/src/main/java/com/docus/sw/fenpan/Roll.java
@@ -38,6 +38,10 @@ public class Roll {
piecesList.add(pieces);
}
+ public void putAll(List piecesList){
+ this.piecesList.addAll(piecesList);
+ }
+
}
diff --git a/src/main/java/com/docus/sw/souyin/ExcelUtil.java b/src/main/java/com/docus/sw/souyin/ExcelUtil.java
index 1bf0f22..c6147f4 100644
--- a/src/main/java/com/docus/sw/souyin/ExcelUtil.java
+++ b/src/main/java/com/docus/sw/souyin/ExcelUtil.java
@@ -2,16 +2,19 @@ package com.docus.sw.souyin;
import com.alibaba.excel.EasyExcel;
+import com.itextpdf.text.*;
+import com.itextpdf.text.Font;
+import com.itextpdf.text.pdf.BaseFont;
+import com.itextpdf.text.pdf.PdfPCell;
+import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Cell;
-import org.apache.poi.ss.usermodel.Row;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@@ -29,35 +32,70 @@ public class ExcelUtil {
} catch (Exception e) {
System.out.println(e.getMessage());
}
- ;
+ ExcelUtil.toPdf(fileName,"C:\\jiahsi-saomiao\\缩微\\test\\output.pdf");
- try {
- FileInputStream excelFile = new FileInputStream(new File(fileName));
- Workbook workbook = new HSSFWorkbook(excelFile);
- // 选择要转换的工作表(这里假设你选择第一个工作表)
+ }
+
+
+
+ public static void toPdf(String filename,String outName){
+ try (Workbook workbook = WorkbookFactory.create(new File(filename));
+ FileOutputStream fos = new FileOutputStream(outName)) {
+ // 获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
- // 创建一个输出流,用于将Excel内容写入PDF文件
- FileOutputStream pdfOutput = new FileOutputStream("C:\\jiahsi-saomiao\\缩微\\test\\output.pdf");
+ // 创建PDF文档对象
+ Document document = new Document(PageSize.A4, 50, 50, 50, 50);
+
+ // 创建PDF输出流
+ PdfWriter writer = PdfWriter.getInstance(document, fos);
- // 使用iText库创建一个PDF文档
- com.itextpdf.text.Document pdfDocument = new com.itextpdf.text.Document();
- PdfWriter.getInstance(pdfDocument, pdfOutput);
- pdfDocument.open();
+ // 打开PDF文档
+ document.open();
- // 读取Excel单元格内容并将其写入PDF
+ // 创建PDF表格对象
+ PdfPTable table = new PdfPTable(sheet.getRow(0).getLastCellNum());
+
+ // 设置表格宽度
+ table.setWidthPercentage(100);
+
+ // 设置表格标题
+ com.itextpdf.text.Font font = FontFactory.getFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED,10f, com.itextpdf.text.Font.NORMAL, BaseColor.BLACK);
+ Paragraph title = new Paragraph("",font);
+ title.setAlignment(Element.ALIGN_CENTER);
+ document.add(title);
+
+ // 添加表格内容
for (Row row : sheet) {
for (Cell cell : row) {
- pdfDocument.add(new com.itextpdf.text.Paragraph(cell.toString()));
+
+ int columnIndex = cell.getColumnIndex();
+
+ if(columnIndex==4&&row.getRowNum()!=0){
+ PdfPCell pdfCell = new PdfPCell(new Paragraph(String.valueOf((int)cell.getNumericCellValue()), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
+ pdfCell.setBorderWidth(1f);
+ pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
+ pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
+ table.addCell(pdfCell);
+ }else{
+ PdfPCell pdfCell = new PdfPCell(new Paragraph(cell.getStringCellValue(), new Font(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED), 12)));
+ pdfCell.setBorderWidth(1f);
+ pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
+ pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
+ table.addCell(pdfCell);
+ }
+
}
}
- pdfDocument.close();
- pdfOutput.close();
+ // 添加表格到PDF文档
+ document.add(table);
- System.out.println("Excel文件已成功转换为PDF文件。");
- } catch (Exception e) {
+ // 关闭PDF文档
+ document.close();
+ writer.close();
+ } catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
@@ -71,7 +109,7 @@ public class ExcelUtil {
m.setPlateName("盘2");
m.setRollName("卷名");
m.setPieces("档号");
- m.setNums("80");
+ m.setNums(80);
m.setStartNum("28");
list.add(m);
}
diff --git a/src/main/java/com/docus/sw/souyin/IndexPageRow.java b/src/main/java/com/docus/sw/souyin/IndexPageRow.java
index be5b025..cebdcb9 100644
--- a/src/main/java/com/docus/sw/souyin/IndexPageRow.java
+++ b/src/main/java/com/docus/sw/souyin/IndexPageRow.java
@@ -16,7 +16,7 @@ public class IndexPageRow {
private String piecesName;
- private String nums;
+ private Integer nums;
private Integer start;
diff --git a/src/main/java/com/docus/sw/souyin/IndexPlate.java b/src/main/java/com/docus/sw/souyin/IndexPlate.java
index 28e8717..5b0d762 100644
--- a/src/main/java/com/docus/sw/souyin/IndexPlate.java
+++ b/src/main/java/com/docus/sw/souyin/IndexPlate.java
@@ -1,9 +1,12 @@
package com.docus.sw.souyin;
+import com.docus.sw.fenpan.Roll;
import lombok.AllArgsConstructor;
import lombok.Data;
-@AllArgsConstructor
+import java.util.ArrayList;
+import java.util.List;
+
@Data
public class IndexPlate {
@@ -11,4 +14,20 @@ public class IndexPlate {
private String name;
+ private List rollList;
+
+ private String zongName;
+
+
+ public IndexPlate(String absolutePath, String name,String zongName) {
+ this.absolutePath = absolutePath;
+ this.name = name;
+ this.zongName = zongName;
+ rollList = new ArrayList<>();
+ }
+
+ public void putAll(List rollList){
+ this.rollList.addAll(rollList);
+ }
+
}
diff --git a/src/main/java/com/docus/sw/souyin/MesEasyExcel.java b/src/main/java/com/docus/sw/souyin/MesEasyExcel.java
index 6ce38d4..1483b94 100644
--- a/src/main/java/com/docus/sw/souyin/MesEasyExcel.java
+++ b/src/main/java/com/docus/sw/souyin/MesEasyExcel.java
@@ -1,12 +1,16 @@
package com.docus.sw.souyin;
import com.alibaba.excel.annotation.ExcelProperty;
+import lombok.AllArgsConstructor;
import lombok.Data;
+import lombok.NoArgsConstructor;
/**
* @author 未来码匠
*/
@Data
+@AllArgsConstructor
+@NoArgsConstructor
public class MesEasyExcel {
@ExcelProperty("全宗名")
@@ -22,9 +26,20 @@ public class MesEasyExcel {
private String pieces;
@ExcelProperty("数量")
- private String nums;
+ private Integer nums;
@ExcelProperty("起始缩微号")
private String startNum;
+
+ public static MesEasyExcel create(IndexPageRow indexPageRow){
+
+ String startNum = "";
+ Integer start = indexPageRow.getStart();
+ if(start!=null){
+ startNum = start.toString();
+ }
+ return new MesEasyExcel(indexPageRow.getZongName(),indexPageRow.getPhNum(),indexPageRow.getRollName(),indexPageRow.getPiecesName(),indexPageRow.getNums(),startNum);
+ }
+
}
diff --git a/src/main/java/com/docus/sw/souyin/PdfToPic.java b/src/main/java/com/docus/sw/souyin/PdfToPic.java
index 96154b3..5f3b8f2 100644
--- a/src/main/java/com/docus/sw/souyin/PdfToPic.java
+++ b/src/main/java/com/docus/sw/souyin/PdfToPic.java
@@ -31,4 +31,30 @@ public class PdfToPic {
}
}
+
+ public static void toPic(String source, String desFilePath, String desFileName, String imageType){
+ Pair pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);
+ System.out.println("PDF文档转化为图片结果:" + pair.getLeft());
+ if (!pair.getLeft()) {
+ System.out.println("" + pair.getRight());
+ } else {
+ List fileList = (List) pair.getRight();
+ System.out.println("转化的文件的内容:");
+ fileList.forEach(System.out::println);
+ }
+ }
+
+
+ public static void toPicDesc(String source, String desFilePath, String desFileName, String imageType){
+ Pair pair = PdfUtil.pdfToImageDesc(source, desFilePath, desFileName, imageType);
+ System.out.println("PDF文档转化为图片结果:" + pair.getLeft());
+ if (!pair.getLeft()) {
+ System.out.println("" + pair.getRight());
+ } else {
+ List fileList = (List) pair.getRight();
+ System.out.println("转化的文件的内容:");
+ fileList.forEach(System.out::println);
+ }
+ }
+
}
diff --git a/src/main/java/com/docus/sw/souyin/PdfUtil.java b/src/main/java/com/docus/sw/souyin/PdfUtil.java
index d49c9c6..aee30f9 100644
--- a/src/main/java/com/docus/sw/souyin/PdfUtil.java
+++ b/src/main/java/com/docus/sw/souyin/PdfUtil.java
@@ -14,6 +14,34 @@ import java.util.List;
public class PdfUtil {
+
+ public static Integer getPage(String source) {
+ File file = new File(source);
+
+
+ PDDocument doc = null;
+ try {
+ //加载PDF文件
+ doc = PDDocument.load(file);
+ PDFRenderer renderer = new PDFRenderer(doc);
+ //获取PDF文档的页数
+ int pageCount = doc.getNumberOfPages();
+ return pageCount;
+ } catch (IOException e) {
+ e.printStackTrace();
+ throw new RuntimeException(e);
+ } finally {
+ try {
+ if (doc != null) {
+ doc.close();
+ }
+ } catch (IOException e) {
+ System.out.println("关闭文档失败");
+ e.printStackTrace();
+ }
+ }
+ }
+
/**
* @param source 原文件
* @param desFilePath 生成图片的路径
@@ -44,9 +72,9 @@ public class PdfUtil {
List