Browse Source

word/excel导出PDF与PDF合并开发

anderx 5 months ago
parent
commit
6b52396494

+ 8 - 8
src/main/java/com/goafanti/common/controller/PublicController.java

@@ -1035,14 +1035,14 @@ public class PublicController extends CertifyApiController {
 	public Result mergeDocumentsToPdf(HttpServletResponse  response) throws DocumentException, IOException {
 
 		// 转换Word为PDF文件
-		PDFUtils.convertWordToPDF("E:/4.docx", "E:/4-PDF.pdf");
-//		PDFUtils.convertXlsxToPDF("E:/3.xlsx", "E:/3-PDF.pdf");
-
-		List<String> list = Arrays.asList(
-				"E:/tmp/3-PDF.pdf",
-				"E:/tmp/4-PDF.pdf"
-		);
-		DocumentToPDFMerger.mergePdfFiles(list,  "E:/tmp/"+System.currentTimeMillis()+".pdf");
+//		PDFUtils.convertWordToPDF("E:/4.docx", "E:/4-PDF.pdf");
+		PDFUtils.convertXlsxToPDF("E:/tmp/0811.xlsx", "E:/tmp/0811-PDF.pdf");
+
+//		List<String> list = Arrays.asList(
+//				"E:/tmp/3-PDF.pdf",
+//				"E:/tmp/4-PDF.pdf"
+//		);
+//		DocumentToPDFMerger.mergePdfFiles(list,  "E:/tmp/"+System.currentTimeMillis()+".pdf");
 		return new Result<>();
 	}
 }

+ 5 - 4
src/main/java/com/goafanti/common/utils/pdf/PDFUtils.java

@@ -234,7 +234,6 @@ public class PDFUtils {
             throws DocumentException {
 
         // 添加文档标题(文件名)
-        document.add(new Paragraph("Excel文档转换", PDFUtils.getTitleFont()));
         document.add(Chunk.NEWLINE);
 
         // 处理每个工作表
@@ -242,9 +241,9 @@ public class PDFUtils {
             Sheet sheet = workbook.getSheetAt(i);
 
             // 添加工作表名称
-            Paragraph sheetTitle = new Paragraph("工作表: " + sheet.getSheetName(), PDFUtils.getBigFont());
-            document.add(sheetTitle);
-            document.add(Chunk.NEWLINE);
+//            Paragraph sheetTitle = new Paragraph("工作表: " + sheet.getSheetName(), PDFUtils.getBigFont());
+//            document.add(sheetTitle);
+//            document.add(Chunk.NEWLINE);
 
             // 计算需要的列数
             int columnCount = 0;
@@ -280,6 +279,8 @@ public class PDFUtils {
                         if (rowCount == 0) {
                             pdfCell.setBackgroundColor(BaseColor.LIGHT_GRAY);
                             pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
+                            //第一行合并
+                            pdfCell.setColspan(columnCount);
                         }
 
                         table.addCell(pdfCell);