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) 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) 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文档中的格式进行处理

你可能感兴趣的:(java,word,java,pdf)