添加缩略,详细索引模式

master
zhanghai 2 years ago
parent ef317a67e4
commit 7601e5745c

@ -30,14 +30,14 @@ public class ExcelUtil {
} catch (Exception e) {
System.out.println(e.getMessage());
}
ExcelUtil.toPdf(fileName,"C:\\jiahsi-saomiao\\缩微\\test\\output.pdf");
ExcelUtil.toPdf1(fileName,"C:\\jiahsi-saomiao\\缩微\\test\\output.pdf");
}
public static void toPdf(String filename,String outName){
public static void toPdf1(String filename,String outName){
try (Workbook workbook = WorkbookFactory.create(new File(filename));
FileOutputStream fos = new FileOutputStream(outName)) {
// 获取第一个工作表
@ -70,7 +70,69 @@ public class ExcelUtil {
int columnIndex = cell.getColumnIndex();
if(columnIndex==4&&row.getRowNum()!=0){
if(columnIndex==6&&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);
}
}
}
// 添加表格到PDF文档
document.add(table);
// 关闭PDF文档
document.close();
writer.close();
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
public static void toPdf2(String filename,String outName){
try (Workbook workbook = WorkbookFactory.create(new File(filename));
FileOutputStream fos = new FileOutputStream(outName)) {
// 获取第一个工作表
Sheet sheet = workbook.getSheetAt(0);
// 创建PDF文档对象
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 创建PDF输出流
PdfWriter writer = PdfWriter.getInstance(document, fos);
// 打开PDF文档
document.open();
// 创建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) {
int columnIndex = cell.getColumnIndex();
if(columnIndex==6&&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);

@ -189,7 +189,7 @@ public class SuoyinService {
EasyExcel.write(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
ExcelUtil.toPdf(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
ExcelUtil.toPdf1(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
Integer page = PdfUtil.getPage(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
@ -207,7 +207,7 @@ public class SuoyinService {
EasyExcel.write(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", MesEasyExcel.class).sheet("test").doWrite(mesEasyExcels);
ExcelUtil.toPdf(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
ExcelUtil.toPdf1(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".xls", indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf");
//写入文件夹。
PdfToPic.toPic(indexPlate.getAbsolutePath() + "/" + indexPlate.getName() + ".pdf", indexPlate.getAbsolutePath() + "/片头", "01", "jpg");
@ -249,6 +249,88 @@ public class SuoyinService {
File file1 = new File(indexPlate.getAbsolutePath());
EasyExcel.write(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".xls", FullIndexPageRow.class).sheet("test").doWrite(fullIndexPageRows);
ExcelUtil.toPdf2(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".xls", file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf");
// //写入文件夹。
PdfToPic.toPic(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf", indexPlate.getAbsolutePath() + "/片头", "01", "jpg");
PdfToPic.toPicDesc(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf", indexPlate.getAbsolutePath() + "/片尾", "00", "jpg");
List<FullIndexPageRow> fullIndexPageRows2 = new ArrayList<>();
i = 1;
for (Roll roll : rollList) {
if (!roll.getName().equals("片头")) {
continue;
}
File file = new File(roll.getAbsolutePath());
List<Document> documentList = new ArrayList<>();
File[] files = file.listFiles();
for (File pdfImg : files) {
getDocumentList(documentList, pdfImg);
}
for(Document document:documentList){
FullIndexPageRow fullIndexPageRow = new FullIndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", document.getName(), document.isA3() ? "A3" : "A4", i);
fullIndexPageRows2.add(fullIndexPageRow);
i++;
}
}
for (Roll roll : rollList) {
if (roll.getName().equals("片头")) {
continue;
}
//跳过片尾
if (roll.getName().equals("片尾")) {
continue;
}
List<Pieces> piecesList = roll.getPiecesList();
for (Pieces pieces : piecesList) {
List<Document> documentList = pieces.getDocumentList();
for (Document document : documentList) {
FullIndexPageRow fullIndexPageRow = new FullIndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), pieces.getName(), document.getName(), document.isA3() ? "A3" : "A4", i);
fullIndexPageRows2.add(fullIndexPageRow);
i++;
}
}
}
for (Roll roll : rollList) {
//跳过片尾
if (!roll.getName().equals("片尾")) {
continue;
}
File file = new File(roll.getAbsolutePath());
List<Document> documentList = new ArrayList<>();
File[] files = file.listFiles();
for (File pdfImg : files) {
getDocumentList(documentList, pdfImg);
}
for(Document document:documentList){
FullIndexPageRow fullIndexPageRow = new FullIndexPageRow(indexPlate.getZongName(), indexPlate.getName(), roll.getName(), "", document.getName(), document.isA3() ? "A3" : "A4", i);
fullIndexPageRows2.add(fullIndexPageRow);
i++;
}
}
EasyExcel.write(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".xls", FullIndexPageRow.class).sheet("test").doWrite(fullIndexPageRows2);
ExcelUtil.toPdf2(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".xls", file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf");
// //写入文件夹。
PdfToPic.toPic(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf", indexPlate.getAbsolutePath() + "/片头", "01", "jpg");
PdfToPic.toPicDesc(file1.getParentFile().getAbsolutePath() + "/full-index-" + indexPlate.getName() + ".pdf", indexPlate.getAbsolutePath() + "/片尾", "00", "jpg");
}
@ -427,4 +509,44 @@ public class SuoyinService {
}
}
}
private void getDocumentList(List<Document> documentList, File file) {
//非图片模式,跳过。
if (!(file.getName().endsWith(".jpg") || file.getName().endsWith(".png") || file.getName().endsWith(".jpeg") || file.getName().endsWith(".tif") || file.getName().endsWith(".tiff")) || file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm") || file.getName().endsWith(".gif")) {
return;
}
if (file.getName().endsWith(".jp2") || file.getName().endsWith(".jpm")) {
// 读取 JPEG 2000 图像文件
try {
BufferedImage image = ImageIO.read(file);
int height = image.getHeight();
int width = image.getWidth();
Document document = new Document(width, height, 300,file.getName(),null);
documentList.add(document);
} catch (IOException e) {
throw new RuntimeException(e);
}
} else {
try {
ImageInfo imageInfo = Imaging.getImageInfo(file);
int height = imageInfo.getHeight();
int width = imageInfo.getWidth();
int physicalHeightDpi = imageInfo.getPhysicalHeightDpi();
Document document = new Document(width, height, physicalHeightDpi,file.getName(),null);
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);
}
}
}
}

Loading…
Cancel
Save