当前位置: 首页 > 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.lryc.cn/news/467525.html

相关文章:

  • WSL-默认root登录
  • ASIO网络调试助手之四:浅谈QTcpServer性能
  • 快手 日常实习一面面经
  • Cursor零基础小白教程系列 - 创建你的第一个Cursor 项目
  • 通用序列化和反序列化实现思路
  • 书生营第四期L0G2000 Python 基础知识
  • Day12-数据库服务冗余架构
  • js监听div尺寸,ResizeObserver
  • STM32与openmv的串口通信
  • C#基于SkiaSharp实现印章管理(11)
  • Spring使用@Async出现循环依赖原因以及解决方案
  • 如何训练 RAG 模型
  • 鸿蒙网络编程系列34-Wifi热点扫描及连接示例
  • LVS三种模式工作原理
  • 【二轮征稿启动】第三届环境工程与可持续能源国际会议持续收录优质稿件
  • 网络安全——防火墙技术
  • Missing classes detected while running R8报错解决方案
  • 智能指针
  • 通过DevTools逃离Chrome沙盒(CVE-2024-6778和CVE-2024-5836)
  • 手持无人机飞手执照,会组装调试入伍当兵有多香!
  • 项目经理好累好烦啊,不想干了....
  • 论技术人员“技术人格”的重要意义
  • Kafka异常重试方案小记
  • 非页面缓冲池占用过高处理方法
  • 【Linux】进程信号(下)
  • FlinkCDC 实现 MySQL 数据变更实时同步
  • JavaWeb——Maven(4/8):Maven坐标,idea集成-导入maven项目(两种方式)
  • 实现uniapp天地图边界范围覆盖
  • 思科网络设备命令
  • Egg.js使用ejs快速自动生成resetful风格的CRUD接口