zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }

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

  • 相关阅读:
    html头部属性全接触
    js中的window.onload和jquery中的load区别
    关机时,自动清除运行框的记录的设置方法
    MVC问题小总结,不断更新中...
    iis6 元数据库与iis6 配置的兼容 出错问题
    MVC对异步 Controller 的支持
    SQL Server2008安装报错,解决方案
    JavaScript有5个原始类型
    ASP.NET MVC中的拦截器
    F5负载均衡
  • 原文地址:https://www.cnblogs.com/wcnwcn/p/11394147.html
Copyright © 2011-2022 走看看