• 2024-04-18
宇哥博客 后端开发 Java将Word文档转换成PDF文件

Java将Word文档转换成PDF文件

将Word文档转成PDF文件,直接放代码:

package com.test;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class Test {
        public static void main(String[] args) throws IOException {
                
                InputStream src =new FileInputStream("E:/222.docx");
                byte[] s=docxToPdf(src);
                // 创建FileOutputStream对象
        FileOutputStream fos = new FileOutputStream("e:/test-222.pdf", false);
        // 将byte数据写入文件
        fos.write(s);
        // 关闭文件
        fos.close();
                
        }
        
        public static byte[] docxToPdf(InputStream src) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] resBytes = null;
        String result;
        try {
            // pdf文件的尺寸
            Document pdfDocument = new Document(PageSize.A3, 72, 72, 72, 72);
            PdfWriter pdfWriter = PdfWriter.getInstance(pdfDocument, baos);
            XWPFDocument doc = new XWPFDocument(src);
            pdfWriter.setInitialLeading(20);
            java.util.List<XWPFParagraph> plist = doc.getParagraphs();
            pdfWriter.open();
            pdfDocument.open();
            
            /*
            List<XWPFTable> tables = doc.getTables();
            //System.out.println("===tables=="+tables);
            for (int i = 0; i < tables.size(); i++) {
                                System.out.println("==="+tables.get(i));
                                XWPFTable table = tables.get(i);
                //读取每一行数据
                for (int j = 0; j < table.getNumberOfRows(); j++) {
                        System.out.println("==="+table.getRow(j));
                }
                        }
                        */
            
            for (int i = 0; i < plist.size(); i++) {
                XWPFParagraph pa = plist.get(i);
                java.util.List<XWPFRun> runs = pa.getRuns();
                for (int j = 0; j < runs.size(); j++) {
                    XWPFRun run = runs.get(j);
                    java.util.List<XWPFPicture> piclist = run.getEmbeddedPictures();
                    Iterator<XWPFPicture> iterator = piclist.iterator();
                    while (iterator.hasNext()) {
                        XWPFPicture pic = iterator.next();
                        XWPFPictureData picdata = pic.getPictureData();
                        byte[] bytepic = picdata.getData();
                        Image imag = Image.getInstance(bytepic);
                        imag.scalePercent(80);//缩放百分比
                        pdfDocument.add(imag);
                    }
                    
                    // 中文字体的解决,否则没有中文
                    BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
                    //Font font = new Font(bf, 11.0f, Font.NORMAL, BaseColor.BLACK);
                    Font font = new Font(bf);
                    String text = run.getText(-1);
                    byte[] bs;
                    if (text != null) {
                        bs = text.getBytes();
                        String str = new String(bs);
                        Chunk chObj1 = new Chunk(str, font);
                        pdfDocument.add(chObj1);
                    }
                }
                pdfDocument.add(new Chunk(Chunk.NEWLINE));
            }
            //需要关闭,不然无法获取到输出流
            pdfDocument.close();
            pdfWriter.close();
            resBytes = baos.toByteArray();
        } catch (Exception e) {
            //log.error("docx转pdf文件异常:{}",e);
                System.out.println("docx转pdf文件异常:" + e);
        }finally {
            try{
                if(baos != null){
                    baos.close();
                }
            }catch (IOException e){
                //log.error("docx转pdf关闭io流异常:{}",e);
            }
        }
        return resBytes;
    } 
}

本次测试用到的poi jar包版本3.16,itextpdf版本5.5.13,itext-asian版本5.2.0。

jar包下载:

  • https://mvnrepository.com/artifact/org.apache.poi/poi
  • https://mvnrepository.com/artifact/com.itextpdf/itextpdf
  • https://mvnrepository.com/artifact/com.itextpdf/itext-asian

当前的问题是word中的表格未读取解析。

原Word文档截图
导出PDF,A4格式
导出PDF,A3格式
本文来自网络,不代表本站立场,转载请注明出处。http://www.ygbks.com/4133.html

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

返回顶部