码迷,mamicode.com
首页 > 其他好文 > 详细

word to pdf

时间:2019-08-22 14:44:02      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:except   path   参考   string   com   span   substring   article   cep   

所需要的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();
        }
    }
}

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

word to pdf

标签:except   path   参考   string   com   span   substring   article   cep   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!