完成索引

master
zhanghai 2 years ago
parent 6889df9761
commit 56362224e2

@ -58,6 +58,11 @@
<version>2.0.23</version>
<type>bundle</type>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10</version>
</dependency>
</dependencies>

@ -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;
}
}

@ -38,6 +38,10 @@ public class Roll {
piecesList.add(pieces);
}
public void putAll(List<Pieces> piecesList){
this.piecesList.addAll(piecesList);
}
}

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

@ -16,7 +16,7 @@ public class IndexPageRow {
private String piecesName;
private String nums;
private Integer nums;
private Integer start;

@ -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<Roll> 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<Roll> rollList){
this.rollList.addAll(rollList);
}
}

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

@ -31,4 +31,30 @@ public class PdfToPic {
}
}
public static void toPic(String source, String desFilePath, String desFileName, String imageType){
Pair<Boolean, Object> pair = PdfUtil.pdfToImage(source, desFilePath, desFileName, imageType);
System.out.println("PDF文档转化为图片结果" + pair.getLeft());
if (!pair.getLeft()) {
System.out.println("" + pair.getRight());
} else {
List<String> fileList = (List<String>) pair.getRight();
System.out.println("转化的文件的内容:");
fileList.forEach(System.out::println);
}
}
public static void toPicDesc(String source, String desFilePath, String desFileName, String imageType){
Pair<Boolean, Object> pair = PdfUtil.pdfToImageDesc(source, desFilePath, desFileName, imageType);
System.out.println("PDF文档转化为图片结果" + pair.getLeft());
if (!pair.getLeft()) {
System.out.println("" + pair.getRight());
} else {
List<String> fileList = (List<String>) pair.getRight();
System.out.println("转化的文件的内容:");
fileList.forEach(System.out::println);
}
}
}

@ -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<Object> fileList = new ArrayList<>();
for (int i = 0; i < pageCount; i++) {
//只有一页的时候文件名为传入的文件名大于一页的文件名为文件名_自增加数字(从1开始)
String realFileName = pageCount > 1 ? desFileName + "_" + (i + 1) : desFileName;
String realFileName = pageCount > 1 ? desFileName + "" + (i + 1) : desFileName;
//每一页通过分辨率和颜色值进行转化
BufferedImage bufferedImage = renderer.renderImageWithDPI(i, 96 * 2, ImageType.RGB);
BufferedImage bufferedImage = renderer.renderImageWithDPI(i, 150 * 2, ImageType.RGB);
String filePath = desFilePath + File.separator + realFileName + "." + imageType;
//写入文件
ImageIO.write(bufferedImage, imageType, new File(filePath));
@ -68,6 +96,58 @@ public class PdfUtil {
}
}
}
public static Pair<Boolean, Object> pdfToImageDesc(String source, String desFilePath, String desFileName, String imageType) {
//通过给定的源路径名字符串创建一个File实例
File file = new File(source);
if (!file.exists()) {
return Pair.of(false, "文件不存在,无法转化");
}
//目录不存在则创建目录
File destination = new File(desFilePath);
if (!destination.exists()) {
boolean flag = destination.mkdirs();
System.out.println("创建文件夹结果:" + flag);
}
PDDocument doc = null;
try {
//加载PDF文件
doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
//获取PDF文档的页数
int pageCount = doc.getNumberOfPages();
System.out.println("文档一共" + pageCount + "页");
List<Object> fileList = new ArrayList<>();
int j = 1;
for (int i = pageCount-1; i >= 0; i--) {
//只有一页的时候文件名为传入的文件名大于一页的文件名为文件名_自增加数字(从1开始)
String realFileName = pageCount > 1 ? desFileName + "" + j : desFileName;
//每一页通过分辨率和颜色值进行转化
BufferedImage bufferedImage = renderer.renderImageWithDPI(i, 150 * 2, ImageType.RGB);
String filePath = desFilePath + File.separator + realFileName + "." + imageType;
//写入文件
ImageIO.write(bufferedImage, imageType, new File(filePath));
//文件名存入list
fileList.add(filePath);
j++;
}
return Pair.of(true, fileList);
} catch (IOException e) {
e.printStackTrace();
return Pair.of(false, "PDF转化图片异常");
} finally {
try {
if (doc != null) {
doc.close();
}
} catch (IOException e) {
System.out.println("关闭文档失败");
e.printStackTrace();
}
}
}
}

@ -1,13 +1,23 @@
package com.docus.sw.souyin;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.FileUtils;
import com.docus.sw.Config;
import com.docus.sw.fenpan.Document;
import com.docus.sw.fenpan.FileTypeEnum;
import com.docus.sw.fenpan.Pieces;
import com.docus.sw.fenpan.Plate;
import com.docus.sw.fenpan.Roll;
import com.google.gson.Gson;
import org.apache.commons.imaging.ImageInfo;
import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SuoyinService {
@ -18,33 +28,238 @@ public class SuoyinService {
//统计出需要使用的 胶片量
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Config.loadConfig();
String path = "C:\\jiahsi-saomiao\\0001";
new SuoyinService().index(path);
}
public void test(){
ArrayList<IndexPlate> indexPlates = new ArrayList<>();
findAllDir("",indexPlates);
public void index(String path) {
// ArrayList<IndexPlate> indexPlates = new ArrayList<>();
Map<String,IndexPlate> map = new HashMap<>();
findAllDir(path, map);
//计算出宗
//计算出每个盘的目录。
for (IndexPlate indexPlate : map.values()) {
String absolutePath = indexPlate.getAbsolutePath();
File file = new File(absolutePath);
File[] files = file.listFiles();
List<Roll> rollList = new ArrayList<>();
//卷
for (File rollFile : files) {
if (rollFile.getName().equals("片头")) {
//填充卷
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
rollList.add(roll);
} else if (rollFile.getName().equals("片尾")) {
//填充卷
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
rollList.add(roll);
} else {
//
List<Pieces> pieceList = new ArrayList<>();
File[] piecesFile = rollFile.listFiles();
for(File piece:piecesFile){
List<Document> documentList = new ArrayList<>();
Pieces pieces = new Pieces(FileTypeEnum.JPG, piece.getAbsolutePath(), piece.getName());
for (File docfile : piece.listFiles()) {
try {
ImageInfo imageInfo = Imaging.getImageInfo(docfile);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi);
documentList.add(document);
} catch (IOException e) {
// FileUtils.delete(file);
throw new RuntimeException("非图片格式", e);
} catch (ImageReadException e) {
// FileUtils.delete(file);
throw new RuntimeException(e);
} catch (IllegalArgumentException e) {
// FileUtils.delete(file);
}
}
pieces.put(documentList);
pieceList.add(pieces);
}
//填充卷
Roll roll = new Roll(rollFile.getName(), rollFile.getAbsolutePath());
roll.putAll(pieceList);
rollList.add(roll);
}
}
indexPlate.putAll(rollList);
}
//生成索引目录
for(IndexPlate indexPlate : map.values()){
List<Roll> rollList = indexPlate.getRollList();
//计算出总size
Double totalPage = 0d;
for(Roll roll:rollList){
List<Pieces> piecesList = roll.getPiecesList();
totalPage+=piecesList.size();
}
Integer pianPageNum = 0;
Integer totalSize = 0;
for(Roll roll:rollList){
//跳过片头
if(roll.getName().equals("片头")){
continue;
}
//跳过片尾
if(roll.getName().equals("片尾")){
continue;
}
List<Pieces> piecesList = roll.getPiecesList();
for(Pieces pieces:piecesList){
Integer size = pieces.getDocumentList().size();
totalSize+=size;
}
}
List<IndexPageRow> indexPageRows= indexGen(indexPlate, rollList, pianPageNum, totalSize);
//修正数据
System.out.println(new Gson().toJson(indexPageRows));
//生成excel
List<MesEasyExcel> mesEasyExcels = new ArrayList<>();
for(IndexPageRow indexPageRowTemp:indexPageRows){
mesEasyExcels.add(MesEasyExcel.create(indexPageRowTemp));
}
EasyExcel.write(indexPlate.getAbsolutePath()+"\\index.xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
ExcelUtil.toPdf(indexPlate.getAbsolutePath()+"\\index.xls",indexPlate.getAbsolutePath()+"\\index.pdf");
Integer page = PdfUtil.getPage(indexPlate.getAbsolutePath() + "\\index.pdf");
indexPageRows= indexGen(indexPlate, rollList, page, totalSize);
//修正数据
System.out.println(new Gson().toJson(indexPageRows));
//生成excel
mesEasyExcels = new ArrayList<>();
for(IndexPageRow indexPageRowTemp:indexPageRows){
mesEasyExcels.add(MesEasyExcel.create(indexPageRowTemp));
}
EasyExcel.write(indexPlate.getAbsolutePath()+"\\index.xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
ExcelUtil.toPdf(indexPlate.getAbsolutePath()+"\\index.xls",indexPlate.getAbsolutePath()+"\\index.pdf");
//写入文件夹。
PdfToPic.toPic(indexPlate.getAbsolutePath()+"\\index.pdf",indexPlate.getAbsolutePath()+"/片头","0000","jpg");
PdfToPic.toPicDesc(indexPlate.getAbsolutePath()+"\\index.pdf",indexPlate.getAbsolutePath()+"/片尾","0010","jpg");
//再重算一次索引,以及将文件进行替换。
}
//读取片头
//读取片尾
//生成excel
//生成图片
//
}
private List<IndexPageRow> indexGen(IndexPlate indexPlate, List<Roll> rollList, Integer pianPageNum, Integer totalSize) {
List<IndexPageRow> indexPageRows = new ArrayList<>();
int start = 1;
//添加片头
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片头")){
File file = new File(roll.getAbsolutePath());
int fileasize = file.listFiles().length;
fileasize+=pianPageNum;
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", fileasize, start);
start+=fileasize;
indexPageRows.add(indexPageRow);
start+=1;
}
}
//处理偏中
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片头")){
continue;
}
//跳过片尾
if(roll.getName().equals("片尾")){
continue;
}
List<Pieces> piecesList = roll.getPiecesList();
for(Pieces pieces:piecesList){
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), pieces.getName(), pieces.getDocumentList().size(), start);
start+=pieces.getDocumentList().size();
indexPageRows.add(indexPageRow);
}
start+=1;
}
//添加片尾
for(Roll roll: rollList){
//跳过片头
if(roll.getName().equals("片尾")){
File file = new File(roll.getAbsolutePath());
int fileasize = file.listFiles().length;
fileasize+=pianPageNum;
IndexPageRow indexPageRow = new IndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", fileasize, start);
start+=fileasize;
indexPageRows.add(indexPageRow);
start+=1;
}
}
//加上统计
IndexPageRow indexPageRow = new IndexPageRow("", "", "", "有效画幅数", totalSize, null);
indexPageRows.add(indexPageRow);
return indexPageRows;
}
private void findAllDir(String absolutePath, Map<String,IndexPlate> map) {
private void findAllDir(String absolutePath, List<IndexPlate> allDirectory) {
File sourceFile = new File(absolutePath);
File[] files = sourceFile.listFiles();
for (File o : files) {
if (o.isDirectory()) {
findAllDir(o.getAbsolutePath(), allDirectory);
findAllDir(o.getAbsolutePath(), map);
}
if (o.isFile()) {
@ -58,14 +273,24 @@ public class SuoyinService {
} else if (o.getName().endsWith(".jpg") || o.getName().endsWith(".png")
|| o.getName().endsWith(".jpeg") || o.getName().endsWith(".tif")
|| o.getName().endsWith(".tiff")) {
//件
if(o.getParentFile().getName().equals("片头")||o.getParentFile().getName().equals("片尾")){
continue;
}
//件
File parentFile = o.getParentFile()
//卷
.getParentFile()
//盘
.getParentFile();
IndexPlate indexPlate = new IndexPlate(parentFile.getAbsolutePath(), parentFile.getName());
allDirectory.add(indexPlate);
//卷
.getParentFile()
//盘
.getParentFile();
if(map.get(parentFile.getAbsolutePath())==null){
IndexPlate indexPlate = new IndexPlate(parentFile.getAbsolutePath(), parentFile.getName(),parentFile.getParentFile().getName());
map.put(parentFile.getAbsolutePath(),indexPlate);
}
} else {
FileUtils.delete(new File(o.getAbsolutePath()));
System.out.println("请删除无效的文件:" + o.getAbsolutePath());

Loading…
Cancel
Save