【问题标题】:Printing different values (Copy number) in different copies of a particular document在特定文档的不同副本中打印不同的值(副本编号)
【发布时间】:2015-04-09 08:13:56
【问题描述】:

我的应用程序使用带有合并字段的RTF file 作为源,并使用Aspose.Words 创建一个PDF file。此应用程序的用户将生成的文档提供给他们的客户,因此将为他们的每个客户打印相同文档的副本。然而,这些副本只有一个区别,那就是每个文档副本末尾的副本编号。

现在;假设有 4 个客户,因此将打印相同文档的 4 个副本,只有副本编号不同。我通过创建相同的文档 4 次来实现这一点,每次我插入我的 html 文本、合并字段并添加副本号然后附加文档。最后,我有一个大文档,其中附加了所有 4 个创建的文档。

这是我的代码块,那里有很多代码,所以我尝试将它们缩小到仅相关部分:

import com.aspose.words.*

Document docAllAppended = new Document(loadDocument("/documents/" + RTFFileName));
Document docTemp=null;
for (int i = 1; i <= copyNumber; i++) {
  docTemp = new Document(loadDocument("/documents/" + RTFFileName));

  DocumentBuilder builder = new DocumentBuilder(docTemp);
  //insert html which includes file context
  builder.insertHtml(htmlText);

  //insert Copy number
  builder.moveToBookmark("sayfa");
  Font font = builder.getFont();
  font.setBold(true);
  font.setSize(8);
  builder.write("Copy Number-" + i+ " / ");
  font.setBold(false);

  docAllAppended.appendDocument(docTemp,ImportFormatMode.USE_DESTINATION_STYLES);
}

这看起来非常不必要并且性能低下。此外,每次我的用户尝试更改要打印的副本数时,我的应用程序都会从​​一开始就计算整个事情。我要问的是,有没有办法让它更快,或者当要打印的副本数发生变化时如何不重新创建整个东西?到目前为止,我还没有找到太多。

提前致谢。

【问题讨论】:

    标签: java printing rtf aspose aspose.words


    【解决方案1】:

    如果唯一的区别是副本数,那么您可以通过插入 HTML、合并等方式准备一次文档。

    然后,在 for 循环中,设置副本数并将文档保存为 docx 或 pdf。不需要在循环中附加文档,您可以将每个副本保存为不同的名称。

    import com.aspose.words.*
    
    Document docAllAppended = new Document(loadDocument("/documents/" + RTFFileName));
    Document docTemp=null;
    docTemp = new Document(loadDocument("/documents/" + RTFFileName));
    
    DocumentBuilder builder = new DocumentBuilder(docTemp);
    //insert html which includes file context
    builder.insertHtml(htmlText);
    
    // In for loop, only update the copy number
    for (int i = 1; i <= copyNumber; i++) {
        // Use DocumentBuilder for font setting
        builder.moveToBookmark("sayfa");
        Font font = builder.getFont();
        font.setBold(true);
        font.setSize(8);
        builder.write("dummy value");
        font.setBold(false);
    
        // Use Bookmark for setting the actual value
        Bookmark bookmark = docAllAppended.getRange().getBookmarks().get("sayfa");
        bookmark.setText("Copy Number-" + i + " / ");
    
        // Save the document for each client
        docAllAppended.save(Common.DATA_DIR + "Letter-Client-" + i + ".docx");
    }
    

    我与 Aspose 合作,担任开发人员宣传员。

    【讨论】:

    • 感谢您的回复,使用您的解决方案,我需要在一次调用中单独打印每个副本(我首先尝试使用宏),我猜这会产生其他挑战。监控涉及的操作的最佳方式是什么?我使用 YourKit 分析器来监控在操作上花费的 CPU 时间。但是,当添加调用计数等详细信息时,花在 aspose 操作以及其他一些操作上的时间会急剧增加。整个过程的 1 分钟变成了 30 分钟,所以我认为它不可靠。有没有更精确的方法来监控涉及的操作。
    • 没有分析需要这么长时间吗? Aspose.Words 依赖于 .NET 框架中的类,并广泛调用 .NET 类库中的方法。分析器可能也在监视来自依赖项的调用并列出一个庞大的列表。
    • 不,如果不进行分析,大约需要 1 分钟(虽然这也有点长)。正如你所说的分析器正在做的事情。在处理 aspose 相关操作时,你有没有使用任何方法来监控幕后发生的事情。
    • 通常,具有大尺寸图像或大量页面的文档需要时间来处理。详细分析只能由我们的产品团队完成。如果您觉得需要更多时间,请将示例文档发布到 Aspose.Words 论坛aspose.com/community/forums/aspose.words-product-family/75/…,支持团队会与您联系
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-04
    相关资源
    最近更新 更多