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

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 page
    double 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 page
    for(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文档中的格式进行处理
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lgbisha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值