Chinaunix首页 | 论坛 | 博客
  • 博客访问: 545825
  • 博文数量: 298
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 3077
  • 用 户 组: 普通用户
  • 注册时间: 2019-06-17 10:57
文章分类

全部博文(298)

文章存档

2022年(96)

2021年(201)

2019年(1)

我的朋友

分类: Java

2021-07-22 09:24:20

 添加maven依赖

点击(此处)折叠或打开

  1. <dependency>
  2.     <groupId>e-iceblue</groupId>
  3.     <artifactId>spire.doc</artifactId>
  4.     <version>2.2.0</version>
  5. </dependency>
转换代码如下

点击(此处)折叠或打开

  1. String fromFile = "E:/test.docx";
  2. String toFile = "E:/test.pdf";
  3. Document document = new Document();
  4. document.loadFromFile(fromFile);
  5. document.saveToFile(toFile, FileFormat.PDF)
注:免费版有文件大小限制,可以替换成正式版,但是正式版有版权提示如下,内容不受影响


Evaluation Warning: The document was created with Spire.Doc for JAVA.
经测试,新版本只能打印10页,且每页都有水印,旧版本2.2.0可正常使用

下面方法可完美删除版权信息

点击(此处)折叠或打开

  1. <dependency>
  2.     <groupId>com.itextpdf</groupId>
  3.     <artifactId>itextpdf</artifactId>
  4.     <version>5.5.11</version>
  5. </dependency>

点击(此处)折叠或打开

  1. Document document = new Document();
  2. //空白文档,用来将版权信息打印到这上面
  3. document.loadFromFile("E:/temp.docx");
  4. //真实文档 fhadmin.cn
  5. document.insertTextFromFile("E:/test.doc",FileFormat.Auto);
  6. //输出为PDF
  7. document.saveToFile("E:/test.pdf",FileFormat.PDF);
  8. PdfReader reader = new PdfReader("E:/test.pdf");
  9. //删除带版权信息的第一页,并重新输出为PDF fhadmin.cn
  10. List<String> pages = new ArrayList<>();
  11. for(int i = 2;i <= reader.getNumberOfPages();i++){
  12.     pages.add(String.valueOf(i));
  13. }
  14. reader.selectPages(StringUtils.join(pages,","));
  15. PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("E:/test2.pdf"));
  16. stamp.close();
  17. reader.close()


阅读(4807) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~