word to pdf
Posted wcnwcn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
注:简单的可以转换,中文会有乱码
以上是关于word to pdf的主要内容,如果未能解决你的问题,请参考以下文章
还在使用pdfword简历?简单五步实现github托管个人逼格简历