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();
            }
        }
    }

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

  • 相关阅读:
    Fedora 8 三维特效美化全攻略
    用C++编写简单绘图语言的语法分析器
    linux tar 的使用
    jquery基础
    hibernate4中主要的配置文件配置
    在对List集合进行remove()等操作重写equals()和hashCode()方法的必要性
    jquery基础2
    javascript时间格式化
    linux之shell编程shell基础
    html会移动的文字
  • 原文地址:https://www.cnblogs.com/wcnwcn/p/11394147.html
Copyright © 2011-2022 走看看