java實(shí)現(xiàn)word轉(zhuǎn)pdf or直接生成pdf文件
1、需要引入的maven坐標(biāo)
<!-- https://mvnrepository.com/artifact/com.itextpdf/kernel -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.2.4</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.itextpdf/layout -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.2.4</version>
</dependency>2、讀取word轉(zhuǎn)換為pdf
/**
* 文件
*/
public void createFile() {
XWPFDocument xwpfDocument = null;
try {
xwpfDocument = new XWPFDocument(new FileInputStream("/Users/chenjx/Downloads/zipceshi/createYuWord.docx"));
} catch (IOException e) {
throw new RuntimeException(e);
}
;
List<XWPFParagraph> paragraphs = xwpfDocument.getParagraphs();
List<XWPFTable> tables = xwpfDocument.getTables();
// 創(chuàng)建 一個(gè) PdfWriter,用于定義 pdf 的路徑地址
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===1111");
try {
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
//通過讀取word進(jìn)行轉(zhuǎn)換
//讀取文本內(nèi)容
for (XWPFParagraph paragraph : paragraphs) {
String text = paragraph.getText();
document.add(new Paragraph(text));
}
//讀取表格內(nèi)容
for (XWPFTable table : tables) {
final Table[] table1 = {null};
List<XWPFTableRow> rows = table.getRows();
rows.forEach(row -> {
int i = rows.indexOf(row);
List<XWPFTableCell> tableCells = row.getTableCells();
if (i == 0) {
table1[0] = new Table(tableCells.size());
}
tableCells.stream().map(XWPFTableCell::getParagraphs).forEach(x -> x.stream().map(XWPFParagraph::getRuns).forEach(runs -> {
if (i == 0) {
table1[0].setWidth(500);
table1[0].addHeaderCell(runs.stream().map(XWPFRun::text).collect(Collectors.joining()));
} else {
table1[0].addCell(runs.stream().map(XWPFRun::text).collect(Collectors.joining()));
}
}));
});
document.add(table1[0]);
}
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}生成效果:
- word模版:

- 生成的pdf:

3、自己設(shè)置格式生成 共有三種格式、直接上util類
package com.zjjw.jxtest.util.util;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFStyle;
import org.apache.poi.xwpf.usermodel.XWPFStyles;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
/**
* @author: chenjiaxiang
* @create: 2022/11/21 17:00
**/
public class FilePreviewUtil {
private static final String FONTS1 = "/Users/chenjx/Library/Fonts/SIMFANG.TTF";
private static final String FONTS = "/Users/chenjx/Library/Fonts/SIMSUN.TTC,1";
public static void main(String[] args) {
new FilePreviewUtil().createFile();
new FilePreviewUtil().createWordTableFile();
new FilePreviewUtil().createPictureFile();
}
/**
* 文本
*/
public void createFile() {
// 創(chuàng)建 一個(gè) PdfWriter,用于定義 pdf 的路徑地址
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===1111");
try {
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
//純英文
document.add(new Paragraph(("Hello China")));
document.add(new Paragraph(("Hello China 中文")));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* word表格
*/
public void createWordTableFile() {
try {
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===222");
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
//這里不要搞錯(cuò),找到你電腦上的這個(gè)文件,復(fù)制這個(gè)文件的絕對路徑
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
Table table = new Table(4);
table.setWidth(500);
table.addHeaderCell("header 1").addHeaderCell("header 2").addHeaderCell("header 3").addHeaderCell("header 4");
for (int i = 0; i < 16; i++) {
table.addCell("cell " + i);
}
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 圖片
*/
public void createPictureFile() {
try {
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===3333");
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
ImageData imageData = ImageDataFactory.create("/Users/chenjx/Downloads/圖片/1616377073865測試.jpg");
Image img = new Image(imageData);
document.add(img.setAutoScale(true));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}4、方法調(diào)用
生成純文本pdf
/**
* 文本
*/
public void createFile() {
// 創(chuàng)建 一個(gè) PdfWriter,用于定義 pdf 的路徑地址
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===1111");
try {
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
//純英文
document.add(new Paragraph(("Hello China")));
document.add(new Paragraph(("Hello China 中文")));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}- createFile()方法生成:

生成純表格pdf
private static final String FONTS = "/Users/chenjx/Library/Fonts/SIMSUN.TTC,1";
/**
* word表格
*/
public void createWordTableFile() {
try {
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===222");
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
//這里不要搞錯(cuò),找到你電腦上的這個(gè)文件,復(fù)制這個(gè)文件的絕對路徑
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
Table table = new Table(4);
table.setWidth(500);
table.addHeaderCell("header 1").addHeaderCell("header 2").addHeaderCell("header 3").addHeaderCell("header 4");
for (int i = 0; i < 16; i++) {
table.addCell("cell " + i);
}
document.add(table);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}- createWordTableFile()方法生成

生成圖片pdf
/**
* 圖片
*/
public void createPictureFile() {
try {
String filename = String.format("/Users/chenjx/Downloads/zipceshi/pdf/%s.pdf", UUID.randomUUID() + "===3333");
PdfWriter pdfWriter = new PdfWriter(filename);
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
PdfFont font = PdfFontFactory.createFont(FONTS);
Document document = new Document(pdfDocument).setFont(font);
ImageData imageData = ImageDataFactory.create("/Users/chenjx/Downloads/圖片/1616377073865測試.jpg");
Image img = new Image(imageData);
document.add(img.setAutoScale(true));
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}- createPictureFile()

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java實(shí)現(xiàn)Word轉(zhuǎn)PDF的全過程
- java根據(jù)模板實(shí)現(xiàn)填充word內(nèi)容并轉(zhuǎn)換為pdf
- Java調(diào)用py或者exe文件實(shí)現(xiàn)Word轉(zhuǎn)PDF
- Java實(shí)現(xiàn)在Word模板指定位置添加二維碼并生成?PDF
- Java實(shí)現(xiàn)一鍵將Word文檔轉(zhuǎn)為PDF
- Java實(shí)現(xiàn)WORD和PDF互相轉(zhuǎn)換以及數(shù)據(jù)填充示例
- Java使用aspose實(shí)現(xiàn)pdf轉(zhuǎn)word
相關(guān)文章
Mybatis如何傳入多個(gè)參數(shù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Mybatis如何傳入多個(gè)參數(shù)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
詳解Struts2動(dòng)態(tài)方法調(diào)用
這篇文章主要介紹了詳解Struts2動(dòng)態(tài)方法調(diào)用,涉及調(diào)用方法的代碼,具有一定參考價(jià)值,需要的朋友可以了解下。2017-09-09
PostgreSQL Docker部署+SpringBoot集成方式
本文介紹了如何在Docker中部署PostgreSQL和pgadmin,并通過SpringBoot集成PostgreSQL,主要步驟包括安裝PostgreSQL和pgadmin,配置防火墻,創(chuàng)建數(shù)據(jù)庫和表,以及在SpringBoot中配置數(shù)據(jù)源和實(shí)體類2024-12-12
Java8中Stream使用的一個(gè)注意事項(xiàng)
最近在工作中發(fā)現(xiàn)了對于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中Stream使用過程中的一個(gè)注意事項(xiàng),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Java實(shí)現(xiàn)高效隨機(jī)數(shù)算法的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)高效隨機(jī)數(shù)算法的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
servlet之session工作原理簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了servlet之session工作原理簡介,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
深入淺出的學(xué)習(xí)Java ThreadLocal
本文會(huì)基于實(shí)際場景介紹ThreadLocal如何使用以及內(nèi)部實(shí)現(xiàn)機(jī)制。 具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Springboot整合ShardingJdbc實(shí)現(xiàn)分庫分表方式
這篇文章主要介紹了Springboot整合ShardingJdbc實(shí)現(xiàn)分庫分表方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-06-06

