关于word转pdf我找到的 最简单的解决方法

我遇到的需求是 浏览器预览word。

因为浏览器并不能预览doc 因此 在不装乱起八糟的东西的情况下 我选择用poi

docx 转 pdf

使用poi4.1.2 + fr.opensagres.poi.xwpf.converter.pdf-gae2.0.2
这里注意,我在这里踩了个大坑
由于poi在easyexcel里面也有用到 因此 如果有自己引入poi 或者使用easyexcel 要把 fr.opensagres.poi.xwpf.converter.pdf-gae2.0.2里面 poi-ooxml给去除

          <dependency>
              <groupId>fr.opensagres.xdocreport</groupId>
              <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId>
              <version>2.0.2</version>
              <exclusions>
                  <exclusion>
                      <artifactId>org.apache.poi</artifactId>
                      <groupId>poi-ooxml</groupId>
                  </exclusion>
              </exclusions>
          </dependency>

代码和网上的一样。

兼容doc

doc的话 就粗暴点了 用spire.doc.free这个 虽然只有500段。
但是预览不够看的话 就自然会自己下载下来了。
spire.doc.free官网已经有demo了,所以就自己去看官网就好了。

遇到的问题

org/apache/poi/POIXMLDocumentPart 找不到这个类的解决:
我是修改fr.opensagres.xdocreport的版本 原来的版本是2.0.1 和 poi 4.1.2冲突了 要么回退poi 要么升级xdocreport 我选择升级xdocreport 如果选择回退poi 就到3.17或3.10

NoClassDefFoundError: com/lowagie/text/ExceptionConverter
IncompatibleClassChangeError: Found interface org.apache.poi.util.POILogger, but class was expected
这个我猜测是 有两个不同版本的poi-ooxml导致的 解决看开头xdocreport的导入maven

总结

这两个的效果其实都不好,但是在不下软件和忽略操作系统环境下 我能找到最好的办法了。

如果你有更好的办法,请评论。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
通过使用iTextPDF库,我们可以将Word文档换为PDF格式。使用iTextPDF进行WordPDF方法如下: 1. 首先,确保你已经在你的项目中添加了iTextPDF的依赖项。 2. 使用POI库来读取Word文档并提取文本内容。 3. 创建一个PDF文档对象。 4. 使用iTextPDF的API将文本内容逐行写入PDF文档。 5. 最后,保存PDF文档即可。 以下是使用iTextPDF实现WordPDF的示例代码: ```java import org.apache.poi.xwpf.usermodel.*; import com.itextpdf.text.*; import com.itextpdf.text.pdf.*; public class WordToPdfConverter { public static void convert(String inputFilePath, String outputFilePath) { try { // 读取Word文档 XWPFDocument document = new XWPFDocument(new FileInputStream(inputFilePath)); // 创建PDF文档 Document pdfDocument = new Document(); PdfWriter writer = PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputFilePath)); pdfDocument.open(); // 将Word文档的内容逐行写入PDF文档 for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); pdfDocument.add(new Paragraph(text)); } pdfDocument.close(); writer.close(); System.out.println("换成功!"); } catch (Exception e) { e.printStackTrace(); } } } ``` 这是一个简单的示例,你可以根据自己的需求进行扩展和优化。记得在代码中替换`inputFilePath`和`outputFilePath`为你自己的文件路径。 希望这个示例能帮到你!如果还有其他问题,请随时提问。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [利用poi+itextpdf进行wordpdf.rar](https://download.csdn.net/download/can1057/12464419)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [java使用poi、itextpdfword、pptpdf文件,并对pdf文件加盖签章](https://blog.csdn.net/qq_30436011/article/details/127737553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值