生成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();
        }
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
### 回答1: Delphi是一个面向对象的编程语言,可以通过使用第三方库来处理和操作docx和doc文件。 要打开和处理docx文件,可以使用Delphi的COM接口来使用Microsoft Office应用程序的功能。首先,需要引用Microsoft Office的COM组件,如Word.Application和Word.Document。然后,可以使用这些组件的方法和属性来打开、编辑和保存docx文件。 以下是一个简单的示例代码: ```delphi var WordApp: Variant; WordDoc: Variant; begin WordApp := CreateOleObject('Word.Application'); WordApp.Visible := False; //设置Word应用不可见 WordDoc := WordApp.Documents.Open('C:\path\to\your\file.docx'); //对文件进行操作,如插入文本、替换文字等 WordDoc.SaveAs('C:\path\to\your\file.docx'); WordDoc.Close; WordApp.Quit; end; ``` 要打开和处理doc文件,可以使用第三方库,如Aspose.Words for .NET。Aspose.Words是一个功能强大的文档处理库,可以在Delphi中使用。 首先,需要在Delphi项目中导入Aspose.Words for .NET库。然后,可以使用该库的类和方法来打开、编辑和保存doc文件。 以下是一个简单的示例代码: ```delphi var doc: TWordDocument; begin doc := TWordDocument.Create; try doc.LoadFromFile('C:\path\to\your\file.doc'); //对文件进行操作,如插入文本、替换文字等 doc.SaveToFile('C:\path\to\your\file.doc'); finally doc.Free; end; end; ``` 以上是两种使用Delphi处理打开docx和doc文件的方法。具体的实现方式可能会因为使用的库或组件而有所不同。根据实际需求和使用的库或组件,可以选择合适的处理方式。 ### 回答2: Delphi是一种面向对象的编程语言,开发环境是Borland Delphi。要在Delphi中打开.docx和.doc文件,可以使用第三方库OfficeToPDF。该库可以将.docx和.doc文件转换为PDF格式,然后在Delphi中通过PDF阅读器来打开。以下是一个简单的示例代码: 1. 下载并安装OfficeToPDF库,将其添加到Delphi项目中。 2. 在需要打开.docx或.doc文件的代码中,添加以下代码: ```delphi uses OfficeToPDF; procedure OpenDocument(const FileName: string); var Converter: TOfficeToPDF; PDFFileName: string; begin Converter := TOfficeToPDF.Create(nil); try Converter.InputFileName := FileName; // 将.docx或.doc文件转换为PDF PDFFileName := ChangeFileExt(FileName, '.pdf'); Converter.OutputFileName := PDFFileName; Converter.Convert; // 打开转换后的PDF文件 ShellExecute(0, 'open', PChar(PDFFileName), nil, nil, SW_SHOWNORMAL); finally Converter.Free; end; end; ``` 3. 在需要打开.docx或.doc文件的事件中,调用OpenDocument过程并传入文件名: ```delphi procedure TForm1.Button1Click(Sender: TObject); begin OpenDocument('path_to_file.docx'); // 替换为实际的文件路径 end; ``` 以上代码将会打开.docx或.doc文件,并将其转换为PDF格式。然后,通过ShellExecute函数在系统中默认的PDF阅读器中打开转换后的PDF文件。请确保系统中已安装合适的PDF阅读器以避免出现错误。希望这可以帮助到您! ### 回答3: Delphi 是一种应用程序开发环境,可以用于创建各种类型的应用程序,在 Delphi 中打开 docx 和 doc 文件可以采用以下方法。 首先,你需要在 Delphi 中导入 Microsoft Office 库,以便可以使用其中的对象和方法。在 Delphi 中导入库的方法是使用“Import Type Library”功能。在 Delphi 的“Component”菜单中,选择“Import Component”,然后选择“Import Type Library”选项。在弹出的对话框中,找到并选择 Microsoft Office 相关的类型库,例如 "Microsoft Word" 或 "Microsoft Office",然后按下 "Install" 按钮,完成导入。 一旦导入 Microsoft Office 库成功,就可以在 Delphi 中使用它们提供的功能了。在代码中,你可以创建一个 Word Application 对象,并打开指定的 docx 或 doc 文件,如下所示: ```Delphi uses Word2000, WordXP, Word2010; var WordApp: TWordApplication; WordDoc: TWordDocument; begin WordApp := TWordApplication.Create(nil); WordApp.Connect; WordDoc := TWordDocument.Create(nil); WordDoc.ConnectTo(WordApp.Documents.Open('path_to_file.docx')); // 或者是 'path_to_file.doc' // 这里可以对打开的文件进行操作,例如读取内容、修改内容等 WordDoc.Disconnect; WordDoc.Free; WordApp.Quit; WordApp.Disconnect; WordApp.Free; end; ``` 在代码中,我们创建了一个 WordApplication 对象和一个 WordDocument 对象。通过调用 `Connect` 方法,我们将它们连接到 Microsoft Word 应用程序。然后,通过调用 `ConnectTo` 方法,我们将 WordDocument 对象连接到指定的 docx 或 doc 文件。在这之后,你可以通过 WordDocument 对象对打开的文件进行读取或修改操作。 最后,我们需要将 WordApplication 和 WordDocument 对象断开连接,并释放它们占用的资源。调用 `Disconnect` 方法断开连接,并通过 `Free` 方法释放对象。此外,我们还调用 `Quit` 方法退出 Word 应用程序。 以上就是使用 Delphi 打开 docx 和 doc 文件的一个简单示例。当然,具体操作可能会因你所使用的 Delphi 版本和 Microsoft Office 版本而有所不同。你需要根据实际情况做相应的调整和修改。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值