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);
}
}
}