word to pdf

所需要的jar

链接: https://pan.baidu.com/s/1leGzU926VD0Nkovqlh9Wvw 提取码: nwfp 

参考的链接 https://blog.csdn.net/qq_29301417/article/details/78904373

package com.test;

import org.docx4j.convert.out.pdf.PdfConversion;
import org.docx4j.convert.out.pdf.viaXSLFO.Conversion;
import org.docx4j.convert.out.pdf.viaXSLFO.PdfSettings;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFont;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;

import java.io.*;
import java.util.List;

public class Word2Pdf {
    public static void main(String[] args) {
        try {

            long start = System.currentTimeMillis();
            String pathname="E:\test1.doc";
//            String pathname="E:\浦东新区环保市容局市民服务热线工单回退审核单.doc";

            InputStream is = new FileInputStream(new File(pathname));
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
            List sections = wordMLPackage.getDocumentModel().getSections();
            for (int i = 0; i < sections.size(); i++) {

                System.out.println("sections Size" + sections.size());
                wordMLPackage.getDocumentModel().getSections().get(i)
                        .getPageDimensions().setHeaderExtent(3000);
            }
            Mapper fontMapper = new IdentityPlusMapper();

            PhysicalFont font = PhysicalFonts.getPhysicalFonts().get(
                    "Comic Sans MS");

            fontMapper.getFontMappings().put("Algerian", font);

            wordMLPackage.setFontMapper(fontMapper);
            PdfSettings pdfSettings = new PdfSettings();
            PdfConversion conversion = new Conversion(wordMLPackage);
            pathname=pathname.substring(0,pathname.lastIndexOf("."));
            OutputStream out = new FileOutputStream(new File(pathname+".pdf"));
            conversion.output(out, pdfSettings);
            System.err.println("Time taken to Generate pdf  "
                    + (System.currentTimeMillis() - start) + "ms");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

注:简单的可以转换,中文会有乱码  

原文地址:https://www.cnblogs.com/wcnwcn/p/11394147.html