当前位置: 首页 > news >正文

itext 转换word文档转pdf

itext 转换word文档转pdf

<dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>4.1.2</version><scope>compile</scope></dependency>
<dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>4.1.2</version></dependency><!-- iText --><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.2</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.9</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext7-core</artifactId><version>7.2.6</version> <!-- 使用最新版本 --></dependency>
package com.msun.csm.service.proj.disastercloud;import java.io.FileInputStream;
import java.io.FileOutputStream;import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;import cn.hutool.core.util.StrUtil;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();// 将PDF文档写入输出文件PdfWriter.getInstance(pdfDocument, new FileOutputStream(outputFilePath));// 打开PDF文档pdfDocument.open();// 将Word文档内容写入PDF文档WordToPdfWriter writer = new WordToPdfWriter(pdfDocument);writer.write(document);// 关闭PDF文档pdfDocument.close();System.out.println("Word转PDF成功!");} catch (Exception e) {System.out.println("Word转PDF失败:" + e.getMessage());}}public static void main(String[] args) {String inputFilePath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\aa.docx";String outputFilePath = "C:\\\\Users\\\\Administrator\\\\Desktop\\\\aa.pdf";convert(inputFilePath, outputFilePath);}
}class WordToPdfWriter {private Document pdfDocument;WordToPdfWriter(Document pdfDocument) {this.pdfDocument = pdfDocument;}public void write(XWPFDocument document) throws Exception {//设置基础中文字体BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);//给字体添加样式Font fontChinese = new Font(bfChinese, 15, Font.BOLD);Font normal = new Font(bfChinese, 15, Font.NORMAL);//设置文字导出时的字体// 逐页将Word文档内容写入PDF文档
//        for (XWPFParagraph paragraph : document.getParagraphs()) {
            pdfDocument.add(new com.itextpdf.text.Paragraph(paragraph.getText()));
//            pdfDocument.add(new Paragraph(paragraph.getText(), fontChinese));
//        }for (XWPFParagraph paragraph : document.getParagraphs()) {String text = paragraph.getText();Paragraph pdfParagraph;if (text.contains("$a")) {pdfParagraph = new Paragraph(text.replace("$a", StrUtil.EMPTY), fontChinese);pdfParagraph.add("\n");pdfParagraph.setAlignment(Element.ALIGN_CENTER);} else if (text.contains("$b")) {pdfParagraph = new Paragraph(text.replace("$b", StrUtil.EMPTY), normal);pdfParagraph.setFirstLineIndent(30);} else if (text.contains("$c")) {pdfParagraph = new Paragraph(text.replace("$c", StrUtil.EMPTY), normal);pdfParagraph.add("\n");pdfParagraph.setAlignment(Element.ALIGN_RIGHT);} else if (text.contains("$d")) {pdfParagraph = new Paragraph(text.replace("$d", StrUtil.EMPTY), normal);pdfParagraph.add("\n");pdfParagraph.setAlignment(Element.ALIGN_RIGHT);} else if (text.contains("$e")) {pdfParagraph = new Paragraph(text.replace("$e", StrUtil.EMPTY));pdfParagraph.add("\n");} else {pdfParagraph = new Paragraph(text, normal);Chunk chunk = new Chunk();chunk.setLineHeight(12);pdfParagraph.add(chunk);}
//            pdfParagraph.setPaddingTop(40);pdfParagraph.setMultipliedLeading(2);
//            for (XWPFRun run : paragraph.getRuns()) {
                Text pdfText = new Text(run.getText(0) != null ? run.getText(0) : "");
//                String text = run.getText(0); // 注意:这里可能丢失格式化文本,但对于简单文档通常足够
//                if (text == null) {
//                    text = "";
//                } else {
//                    text = sb.toString();
//                }
//                Chunk chunk = new Chunk(text);
//
//                // 设置样式
//                if (run.isBold()) {
//                    chunk.setFont(fontChinese);
//                }
//                if (run.isItalic()) {
//                    chunk.setFont(fontChinese);
//                }
//                if (run.getFontSize() != -1) {
//                    chunk.setFont(fontChinese);
//                }
                chunk.setFontSize(run.getFontSize() != -1 ? run.getFontSize() : 12);
//                pdfParagraph.add(chunk);
//
                pdfDocument.add(new Paragraph(run.getText(0), fontChinese));
//            }pdfDocument.add(pdfParagraph);}}
}

http://www.mrgr.cn/news/52732.html

相关文章:

  • 231321321
  • Spring Boot 中的 @RequestMapping 和 Spring 中的 @RequestMapping 有什么区别?
  • Python | Leetcode Python题解之第492题构造矩形
  • AI学习指南深度学习篇-预训练模型的数学原理
  • TCP/IP 寻址
  • 【ShuQiHere】Logic Programming:探索逻辑编程的奇妙世界
  • 在FastAPI网站学python:虚拟环境创建和使用
  • Python列表专题:插入元素性能分析
  • Pytest库应用详解
  • 数据库优化策略
  • 可编辑73页PPT | 企业智慧能源管控平台建设方案
  • 发布专利的基本流程:
  • 使用 Python 的 mock 库进行依赖注入
  • 设计一个支持自动化测试执行的测试框架
  • DS链式二叉树的遍历(11)
  • OpenCV学习笔记5——图像的数值计算
  • stm32 单片机使用 rt-thread 的syswatch 系统守护软件包
  • 使用 pytest 进行测试驱动开发(TDD)
  • 在FastAPI网站学python:Python 并发 async / await
  • C++算法练习-day5——59.螺旋矩阵2