word转pdf
jar包
1 2 3 4 5 | < dependency > < groupId >org.docx4j</ groupId > < artifactId >docx4j</ artifactId > < version >3.0.1</ version > </ dependency > |
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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()); } }}
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· 开发者新选择:用DeepSeek实现Cursor级智能编程的免费方案
· 【译】.NET 升级助手现在支持升级到集中式包管理
· 独立开发经验谈:如何通过 Docker 让潜在客户快速体验你的系统
· Tinyfox 发生重大改版
2018-01-15 [python,2018-01-15] 冒泡法排序