欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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è)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Mybatis實(shí)現(xiàn)分表插件

    Mybatis實(shí)現(xiàn)分表插件

    隨著系統(tǒng)的發(fā)展,數(shù)據(jù)量也會(huì)越來(lái)越大,分庫(kù)分表可以有效的緩解數(shù)據(jù)庫(kù)的壓力,本文主要介紹了Mybatis實(shí)現(xiàn)分表插件,感興趣的可以了解一下
    2021-05-05
  • Java Spring詳解如何配置數(shù)據(jù)源注解開(kāi)發(fā)以及整合Junit

    Java Spring詳解如何配置數(shù)據(jù)源注解開(kāi)發(fā)以及整合Junit

    Spring 是目前主流的 Java Web 開(kāi)發(fā)框架,是 Java 世界最為成功的框架。該框架是一個(gè)輕量級(jí)的開(kāi)源框架,具有很高的凝聚力和吸引力,本篇文章帶你了解如何配置數(shù)據(jù)源、注解開(kāi)發(fā)以及整合Junit
    2021-10-10
  • Java集合框架之Map詳解

    Java集合框架之Map詳解

    這篇文章主要為大家詳細(xì)介紹了Java集合框架之Map,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • JDBC使用小結(jié)

    JDBC使用小結(jié)

    JDBC是一個(gè)Java應(yīng)用程序接口,作用是封裝了對(duì)數(shù)據(jù)庫(kù)的各種操作。JDBC由類和接口組成,使用Java開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用都需要4個(gè)主要的接口:Driver、Connection、Statement、ResultSet,這些接口定義了使用SQL訪問(wèn)數(shù)據(jù)庫(kù)的一般架構(gòu),下面我們來(lái)詳細(xì)探討下jdbc的使用。
    2016-05-05
  • Java基礎(chǔ)之JDBC的數(shù)據(jù)庫(kù)連接與基本操作

    Java基礎(chǔ)之JDBC的數(shù)據(jù)庫(kù)連接與基本操作

    這篇文章主要介紹了Java基礎(chǔ)之JDBC的數(shù)據(jù)庫(kù)連接與基本操作,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)java基礎(chǔ)的小伙伴們也有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Springboot如何通過(guò)路徑映射獲取本機(jī)圖片資源

    Springboot如何通過(guò)路徑映射獲取本機(jī)圖片資源

    項(xiàng)目中對(duì)圖片的處理與查看是必不可少的,本文將講解如何通過(guò)項(xiàng)目路徑來(lái)獲取到本機(jī)電腦的圖片資源,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-08-08
  • Java SSM框架如何添加寫(xiě)日志功能

    Java SSM框架如何添加寫(xiě)日志功能

    這篇文章主要介紹了Java SSM框架如何添加寫(xiě)日志功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 使用mybatis log plugin插件展示出數(shù)據(jù)庫(kù)查詢語(yǔ)句方式

    使用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-11
  • SpringBoot項(xiàng)目實(shí)戰(zhàn)之加載和讀取資源文件

    SpringBoot項(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
  • Java案例分享-集合嵌套

    Java案例分享-集合嵌套

    這篇文章主要介紹了Java案例分享-集合嵌套,通過(guò)案例創(chuàng)建一個(gè)ArrayList集合,存儲(chǔ)三個(gè)元素,每一個(gè)元素都是HashMap,每一個(gè)HashMap的鍵和值都是String,并遍歷,實(shí)際操作內(nèi)容需要的小伙伴可以參考一下
    2022-04-04

最新評(píng)論