java實(shí)現(xiàn)word轉(zhuǎn)pdf or直接生成pdf文件
更新時(shí)間:2025年04月23日 10:17:55 作者:C__jx
這篇文章主要介紹了java實(shí)現(xiàn)word轉(zhuǎn)pdf or直接生成pdf文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
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); //通過(guò)讀取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è)文件的絕對(duì)路徑 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測(cè)試.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è)文件的絕對(duì)路徑 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測(cè)試.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的全過(guò)程
- 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)文章
Java Spring詳解如何配置數(shù)據(jù)源注解開(kāi)發(fā)以及整合Junit
Spring 是目前主流的 Java Web 開(kāi)發(fā)框架,是 Java 世界最為成功的框架。該框架是一個(gè)輕量級(jí)的開(kāi)源框架,具有很高的凝聚力和吸引力,本篇文章帶你了解如何配置數(shù)據(jù)源、注解開(kāi)發(fā)以及整合Junit2021-10-10Java基礎(chǔ)之JDBC的數(shù)據(jù)庫(kù)連接與基本操作
這篇文章主要介紹了Java基礎(chǔ)之JDBC的數(shù)據(jù)庫(kù)連接與基本操作,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們也有很好的幫助,需要的朋友可以參考下2021-05-05Springboot如何通過(guò)路徑映射獲取本機(jī)圖片資源
項(xiàng)目中對(duì)圖片的處理與查看是必不可少的,本文將講解如何通過(guò)項(xiàng)目路徑來(lái)獲取到本機(jī)電腦的圖片資源,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-08-08使用mybatis log plugin插件展示出數(shù)據(jù)庫(kù)查詢語(yǔ)句方式
本文介紹了如何安裝和配置MyBatis日志插件,包括集成log4j、配置log4j.xml文件和在application.properties中添加數(shù)據(jù)庫(kù)打印配置,通過(guò)這些步驟,可以在調(diào)試時(shí)查看預(yù)編譯的數(shù)據(jù)庫(kù)語(yǔ)句和實(shí)際查詢語(yǔ)句2024-11-11SpringBoot項(xiàng)目實(shí)戰(zhàn)之加載和讀取資源文件
在項(xiàng)目的開(kāi)發(fā)中,我們知道的是SpringBoot框架大大減少了我們的配置文件,但是還是留下了一個(gè)application.properties文件讓我們可以進(jìn)行一些配置,下面這篇文章主要給大家介紹了關(guān)于SpringBoot項(xiàng)目實(shí)戰(zhàn)之加載和讀取資源文件的相關(guān)資料,需要的朋友可以參考下2021-10-10