Excel转PDF

川长思鸟来 2023-02-22 12:21 162阅读 0赞
  1. import com.aspose.cells.*;
  2. import com.aspose.cells.PdfSaveOptions;
  3. import com.itextpdf.text.*;
  4. import com.itextpdf.text.pdf.*;
  5. import lombok.extern.slf4j.Slf4j;
  6. import java.io.*;
  7. /**
  8. * 在项目创建一个libs目录,aspose-cells的包放入该目录(aspose-cells的包无法直接拉取依赖)
  9. * 在build.gradle引入libs的包:compile fileTree(dir:'libs',include:['*.jar'])
  10. * jar
  11. * 1: aspose-cells-20.6.jar
  12. * 2: itextpdf-5.5.1.jar
  13. * 3: lombok-1.18.10.jar
  14. *
  15. * @Description
  16. * @Author 不爱写代码的码农
  17. * @Date 2020/6/28 14:02
  18. */
  19. @Slf4j
  20. public class ISPTest {
  21. // Excel转PDF,支持多sheet页,水印覆盖
  22. public static void main(String[] args) throws Exception {
  23. String sourceFilePath = "D:/test/质量系数以及缺陷分析表V2_0.xls";
  24. String tmpFilePath = "D:/test/tmp.pdf";
  25. String taskFilePathd = "D:/test/result.pdf";
  26. excel2pdf(sourceFilePath,tmpFilePath, taskFilePathd);
  27. }
  28. public static void excel2pdf(String sourceFilePath,String tmpFilePath ,String taskFilePathd) throws Exception {
  29. try {
  30. Workbook wb = new Workbook(sourceFilePath);
  31. wb.save(tmpFilePath,SaveFormat.PDF);
  32. PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
  33. pdfSaveOptions.setOnePagePerSheet(true);
  34. // 所有sheet页都设置可见和自动缩放
  35. allSheetVisibleAndAutoZoom(wb);
  36. FileOutputStream fileOS = new FileOutputStream(tmpFilePath);
  37. wb.save(fileOS, pdfSaveOptions);
  38. fileOS.flush();
  39. fileOS.close();
  40. PdfReader reader = new PdfReader(tmpFilePath);
  41. PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(taskFilePathd));
  42. // 处理水印
  43. waterMaker(reader, stamper);
  44. } catch (Exception e) {
  45. log.info("Excel to PDF task fail");
  46. }
  47. }
  48. // 处理水印
  49. private static void waterMaker(PdfReader reader, PdfStamper stamper) throws DocumentException, IOException {
  50. int numberOfPages = reader.getNumberOfPages();
  51. System.out.println(numberOfPages);
  52. // 覆盖每一页的水印
  53. if (numberOfPages > 0){
  54. for (int i = 1; i <= numberOfPages; i++) {
  55. System.out.println("a: "+i);
  56. PdfContentByte canvas = stamper.getOverContent(i);// 获取PDF内容
  57. Rectangle pageSize = reader.getPageSize(i);
  58. float height = pageSize.getHeight();
  59. System.out.println(canvas.getHorizontalScaling());
  60. float x,y,w,h;
  61. x = 0;
  62. y = height -15;
  63. w = 430;
  64. h = 15;
  65. canvas.saveState();
  66. canvas.setColorFill(BaseColor.WHITE); //遮挡层颜色:白色
  67. canvas.rectangle(x, y, w, h);
  68. canvas.fill();
  69. canvas.restoreState();
  70. }
  71. }
  72. stamper.close();
  73. reader.close();
  74. }
  75. /**
  76. * 所有sheet页都设置可见
  77. * 所有sheet页都等比缩放
  78. * @param wb
  79. */
  80. private static void allSheetVisibleAndAutoZoom(Workbook wb){
  81. for (int i= 1; i < wb.getWorksheets().getCount(); i++) {
  82. // 每个sheet文件都设置可见
  83. wb.getWorksheets().get(i).setVisible(true);
  84. // 设置等比缩放
  85. wb.getWorksheets().get(i).getHorizontalPageBreaks().clear();
  86. wb.getWorksheets().get(i).getVerticalPageBreaks().clear();
  87. }
  88. }
  89. }

发表评论

表情:
评论列表 (有 0 条评论,162人围观)

还没有评论,来说两句吧...

相关阅读

    相关 itext excelpdf

    要将iText和Excel文件转换为PDF,需要使用iText库来读取Excel文件,并使用该库生成PDF文档。以下是一些示例代码,展示了如何使用iText和Apache PO

    相关 ExcelPDF

    本文将介绍在Java程序中如何将Excel工作簿转为PDF文档的,包括: 将整个工作簿转为PDF 将指定工作表转为PDF 使用工具:Free Spire.XLS