zoukankan      html  css  js  c++  java
  • [java,2019-01-15] word转pdf

    word转pdf

    jar包

    <dependency>
    	<groupId>org.docx4j</groupId>
    	<artifactId>docx4j</artifactId>
    	<version>3.0.1</version>
    </dependency>
    

     

    代码

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.List;
    
    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;
    
    public class Word2Pdf {
        public static void main(String[] args) {
            try {
    
                long start = System.currentTimeMillis();
    
                InputStream is = new FileInputStream(
                        new File("E:\1234.docx"));
                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();
                org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(
                        wordMLPackage);
    
                OutputStream out = new FileOutputStream(new File(
                        "E:\1234.pdf"));
                conversion.output(out, pdfSettings);
                System.out.println("Time taken to Generate pdf  "
                        + (System.currentTimeMillis() - start) + "ms");
                System.out.println("end--------------------------"+System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
                System.err.println("error--------------------------"+System.currentTimeMillis());
            }
        }
    }
    

      好像只支持docx,不支持doc,原因正在查找中



    import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.util.List;
    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;
    public class Word2Pdf {    public static void main(String[] args) {        try {
                long start = System.currentTimeMillis();
                InputStream is = new FileInputStream(                    new File("E:\1234.docx"));            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();            org.docx4j.convert.out.pdf.PdfConversion conversion = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(                    wordMLPackage);
                OutputStream out = new FileOutputStream(new File(                    "E:\1234.pdf"));            conversion.output(out, pdfSettings);            System.out.println("Time taken to Generate pdf  "                    + (System.currentTimeMillis() - start) + "ms");            System.out.println("end--------------------------"+System.currentTimeMillis());        } catch (Exception e) {            e.printStackTrace();            System.err.println("error--------------------------"+System.currentTimeMillis());        }    }}

  • 相关阅读:
    我的WCF之旅(7):面向服务架构(SOA)和面向对象编程(OOP)的结合——如何实现Service Contract的继承(转载)
    我的WCF之旅(4):WCF中的序列化(Serialization) Part I(转载)
    我的WCF之旅(1):创建一个简单的WCF程序(转载)
    我的WCF之旅(3):在WCF中实现双向通信(Bidirectional Communication)(转载)
    我的WCF之旅(2):Endpoint Overview(转载)
    C#中Attribute特性的用法(转载)
    我的WCF之旅(5):Service Contract中的重载(Overloading)(转载)
    我的WCF之旅(6):在Winform Application中调用Duplex Service出现TimeoutException的原因和解决方案(转载)
    [转] 81条经典话语~~~当裤子失去皮带,才懂得什麽叫做依赖
    HP大中华区总裁孙振耀退休十五天后九大感言,快毕业或者已经毕业的都来看看,与其浑浑噩噩一辈子,不如花这半小时看看
  • 原文地址:https://www.cnblogs.com/shijt/p/10272572.html
Copyright © 2011-2022 走看看