aspose-word for java word转pdf 解决遇到的问题

具体问题

  • 在项目中使用aspose-word 把word转换为pdf
  • 有次一份63页的文档转换出来的pdf为72页,正常情况下63页word转换出来为63页pdf

分析过程

  • 分析行间距,字段间距,字间距,缩进情况,字体是否缺失,字体大小,页眉页脚,表格,图片等
  • 尝试使用各种通用处理方案
//方法一,去除所有格式转换
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
ParagraphFormat pf = doc.getStyles().getDefaultParagraphFormat();
pf.clearFormatting();
doc.save(os, SaveFormat.PDF);
//方法二,重新新增样式模板再导出
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
DocumentBuilder builder = new DocumentBuilder(doc);
//新建再复制样式
Document document = new Document();
document.removeAllChildren();
document.appendDocument(doc,ImportFormatMode.USE_DESTINATION_STYLES;
document.save(os, SaveFormat.PDF);
//方法三,对word文档主体设置各个参数,具体可以看官方api查看
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
DocumentBuilder builder = new DocumentBuilder(doc);
//文档主体内容设置段后和行距
builder.moveToDocumentStart();
builder.getParagraphFormat().setLeftIndent(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToHeaderFooter(HeaderFooterType.HEADER_PRIMARY);
builder.getParagraphFormat().setLineSpacing(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getParagraphFormat().setLineSpacing(12);
builder.getParagraphFormat().setSpaceAfter(0);
builder.moveToDocumentStart();
document.save(os, SaveFormat.PDF);
//方法四,网上查的自定义宽高,以及取消隐藏数据的方法
FileOutputStream os = new FileOutputStream(targetFile);
Document doc = new Document(sourceFile);
NodeCollection tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : (Iterable<Table>) tables) {double tableWidth = 0;double tableHeight = 0;Section section = (Section) table.getAncestor(NodeType.SECTION);for (Row row : table.getRows()) {double rowWidth = 0;boolean composite = table.isComposite();if (!composite){table.remove();continue;}double height = row.getRowFormat().getHeight();tableHeight += height;for (Cell cell : row.getCells()) {for(Run run : (Iterable<Run>) cell.getChildNodes(NodeType.RUN, true)){String text = run.getText();String name = run.getFont().getName();boolean hidden = run.getFont().getHidden();if (hidden){run.setText("");}int color = run.getFont().getColor().getRGB();int backgroundPattern = run.getFont().getShading().getBackgroundPatternColor().getRGB();if (color == backgroundPattern){
//                            run.setText("");}}rowWidth += cell.getCellFormat().getWidth();cell.getCellFormat().setFitText(true);}if (rowWidth > tableWidth) {tableWidth = rowWidth;}}//Calculate the width of the pagedouble pageWidth = section.getPageSetup().getPageWidth() - (section.getPageSetup().getLeftMargin() + section.getPageSetup().getRightMargin());//In the second run set each cell in the row proportionally to the width of the pagefor(Row row : table.getRows()) {for(Cell cell : row.getCells()){//Calculate the ratio of each cell to the row width and then translate this ratio to the page width.double cellRatio = cell.getCellFormat().getWidth() / tableWidth;cell.getCellFormat().setWidth(cellRatio * pageWidth);}if (row.getRowFormat().getHeight() > tableHeight ){continue;}double height = row.getRowFormat().getHeight() / tableHeight;row.getRowFormat().setHeight(height * tableHeight);}
}
document.save(os, SaveFormat.PDF);

官方文档api

结果处理

  • 基本上以上几种方法处理完后大部分问题可以解决,其他如果还有问题,目前是没办法,只能去修改word文档中的格式进行处理

aspose-word for java word转pdf 解决遇到的问题相关推荐

  1. leetcode word break java,Word Break leetcode java

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  2. java word转pdf,docx4j转pdf,docx4j导出pdf乱码,docx4j导出pdf丢失插画和图片,aspose将word转pdf 一共两种方法

    前言:一共有docx4j转pdf,aspose转pdf两种方式,不需要设置模板!!! java转pdf目前本人使用有两种方法,下面是方法代码 ps:因为本人是云桌面开发,所以只作截图,具体代码需要自己 ...

  3. java word转pdf linux_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

  4. java word在线预览_java实现word转pdf在线预览(前端使用PDF.js;后端使用openoffice、aspose)...

    背景 之前一直是用户点击下载word文件到本地,然后使用office或者wps打开.需求优化,要实现可以直接在线预览,无需下载到本地然后再打开. 随后开始上网找资料,网上资料一大堆,方案也各有不同,大 ...

  5. 福利!使用Aspose.Words在Java中以编程方式将Word格式转换为PDF完整指南

    在共享文档之前,通常使用Word到PDF的转换.可以使用各种在线Word到PDF转换器,可以转换单个或有限数量的Word文档.但是,随着新兴的MS Word自动化和报告生成解决方案,Word到PDF的 ...

  6. java word转pdf,可以解决转pdf后word横版变窄问题(pdf显示不全)

    友情提醒,这个是目前不支持Linux,只支持wind. 最近公司业务需要在pdf中显示表格,但是表格内容列数有点长,导致 word 转pdf的时候 显示不全: 如上图这种显示不全的情况(截图中没有放表 ...

  7. 关于FreeMarker生成word文档后转换为pdf得解决方法及常见问题

    关于FreeMarker生成word文档后转换为pdf得解决方法及常见问题 最近在做一个项目要求之前下载出的word简历直接变成pdf 格式进行展现.因为格式比较复杂,所以采用的时模板并用Freema ...

  8. java word转pdf三种方法(附有需要的jar)

    一.jacob 1.jar下载 jacob.jar和jacob-1.17-x64.dll下载 提取码:0121 2.在jdk/bin目录下引入.dll文件(64位:jacob-1.17-x64.dll ...

  9. Aspose.Word企业案例:Pesearch Engineers 使用 Aspose.Words for Java 查看和转换 Word 文档

    关于Pesearch Engineers Pesearch Engineers不同类型的用例开发.销售和支持工程软件 Aspose.Words for Java 最新下载 问题 Pesearch En ...

最新文章

  1. 【非专业评测】发蜡、发膏、发泥、发油、啫喱、发膜、发胶、干胶、发棒、摩丝
  2. php写网页6,thinkphp6输出原始html内容 - 旗云号
  3. densenet tensorflow 中文汉字手写识别
  4. Windows Phone + VB 程序员=好的移动应用程序
  5. knn人脸识别判断_WEEK1_LRamp;KNN
  6. 陈炳藻用计算机研究,计算机闯入“大观园”
  7. 使用PHP得到所有的HTTP请求头
  8. 深入理解分布式系统原理与设计
  9. Mybatis之执行自定义SQL举例
  10. 松下机器人找原点步骤_松下机器人操作规程2017
  11. Python转换图片格式 -- PIL库的使用
  12. deque(双向队列)
  13. 欧姆龙PLC以太网modbusTCP通讯
  14. gentoo适合做服务器系统吗,Mandriva、Gentoo发行版
  15. imap服务器收缓存pst文件夹,Outlook 转移OST数据文件 IMAP账户
  16. 《测绘管理与法律法规》——测绘资质资格管理
  17. 转载之-中值滤波均值滤波
  18. 利用python进行身份证号码大全_2分钟就能学会的3个函数,快速从身份证中提取出生日期、年龄...
  19. 纯真ip数据库 转mysql_如何把纯真ip数据库导入到MySQL数据表中
  20. 玩转「Wi-Fi」系列之测试工具(三)

热门文章

  1. Mybatis-Plus学习(一):实现增删改查CRUD
  2. 高德地图 自定义路径
  3. 贝灵机器人智能点读笔好吗_贝灵点读笔怎么样好用吗 贝灵点读笔使用测评
  4. python tkinter获取屏幕大小_Python开发GUI工具介绍,实战:将图片转化为素描画
  5. 基于EMQX云服务器的环境调节系统(微信小程序连接MySQL篇)
  6. 为了研究蜘蛛纸牌的规则,我玩了200局!
  7. 将IphotoDraw标注好的xml文件转成txt文件(三)
  8. 多变量变送器工作原理及独特的性能优势简述
  9. 图片修改 + 文字修改
  10. MATLAB与STK互联40:仿真案例7—近地圆轨道卫星的抵近操作及相对轨迹显示设置(无互联,手动设置)