生成doc文档目录,aspose word技术实现

生成doc文档目录,比较了Spire.Doc和aspose word两个技术,Spire.Doc生成目录过多的话会出现问题,使用aspose word解决此问题。

//输出到文件
String dir ="路径";
String docName = "报告.docx";
docFile = new File(dir, docName);
String pdfName ="报告.pdf";
File pdfFile = new File(dir + pdfName);
//生成目录,给目录添加文字样式
appendToc1(docFile, pdfFile);
/**
 * 生成目录
 *
 * @param docFile
 */
private void appendToc1(File docFile, File pdfFile) {
    InputStream inputStream = null;
    try {
        if (!AsposeUtil.getLicense(AsposeUtil.Word)) {
            System.out.println("Word license load error!");
        }
        inputStream = new FileInputStream(docFile);
        com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
        DocumentBuilder builder = new DocumentBuilder(doc);
        Node node = doc.getSections().get(1).getBody().getChild(NodeType.PARAGRAPH, 0, true);
        builder.moveTo(node);
        //设置目录的格式
        //“目录”两个字居中显示、加粗
        //插入分页符
        builder.insertBreak(com.aspose.words.BreakType.PAGE_BREAK);
        builder.getCurrentParagraph().getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
        builder.setBold(true);
        builder.getFont().setName("微软雅黑");
        builder.getFont().setSize(22);
        builder.writeln("目录");
        //清除所有样式设置
        builder.getParagraphFormat().clearFormatting();
        //目录居左
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
        //插入目录,这是固定的
        builder.insertTableOfContents("\\o \"1-3\" \\h \\z \\u");
        //插入分页符
        builder.insertBreak(com.aspose.words.BreakType.PAGE_BREAK);
        // 更新域
        doc.updateFields();

        //TOC_1:一级目录 TOC_2:二级目录 。。。
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setSize(14);
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setBold(true);
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setName("微软雅黑");
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getParagraphFormat().setLineSpacing(11.5);

        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_2).getFont().setSize(10.5);
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_2).getFont().setName("微软雅黑");
        doc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_2).getParagraphFormat().setLineSpacing(11.5);

        // 目录字体样式变更后再次更新域
        doc.updateFields();
        com.aspose.words.Paragraph firstParagraph = doc.getSections().get(1).getBody().getFirstParagraph();
        for (int i = 0; i < firstParagraph.getRuns().getCount(); i++) {
            //获取段落里面的文本片段,删除换页符
            Run run = firstParagraph.getRuns().get(i);
            if("\f".equals(run.getText())){
                run.remove();
            }
        }
        //改动目录的相关内容都得更新目录域
        doc.updateFields();
        doc.save(pdfFile.getAbsolutePath(), SaveFormat.PDF);

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值