0

我有这个简化的代码,它使用com.aspose.words.DocumentBuilder.

for (Document contentDocument : documents) {
    ...
    builder.insertDocument(contentDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
    builder.insertBreak(BreakType.PAGE_BREAK);
}

在每个文档之后插入一个分页符。

有没有办法删除最后一个分页符?

4

4 回答 4

1

您可以使用旧版本的循环循环。在这里,我假设文档是一个列表。

for (int i = 0; i < documents.size(); i++) {
    Document contentDocument = documents.get(i);
    builder.insertDocument(contentDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);

    if (i < documents.size() - 1) {
         builder.insertBreak(BreakType.PAGE_BREAK);
    }
}
于 2016-08-25T13:17:34.290 回答
1

我更喜欢使用 POI,但看看这个

请注意以下部分

 private static void removeSectionBreaks(Document doc) throws Exception
{
    // Loop through all sections starting from the section that precedes the last one
    // and moving to the first section.
    for (int i = doc.getSections().getCount() - 2; i >= 0; i--)
    {
        // Copy the content of the current section to the beginning of the last section.
        doc.getLastSection().prependContent(doc.getSections().get(i));
        // Remove the copied section.
        doc.getSections().get(i).remove();
    }
}
于 2016-08-25T13:19:34.827 回答
1

不会是这样的:

for (int i=0; i< documents.length; i++) {
    ...
    builder.insertDocument(documents[i], ImportFormatMode.KEEP_SOURCE_FORMATTING);
    if (i == documents.length - 1) {
       continue;
    } 
    builder.insertBreak(BreakType.PAGE_BREAK);
}

工作?或者避免在每次迭代时进行检查:

for (int i=0; i< documents.length -1; i++) {
    ...
    builder.insertDocument(documents[i], ImportFormatMode.KEEP_SOURCE_FORMATTING);
    builder.insertBreak(BreakType.PAGE_BREAK);
}
builder.insertDocument(documents[documents.length - 1], ImportFormatMode.KEEP_SOURCE_FORMATTING);

提供的解决方案假定这documents是一个数组。

于 2016-08-25T13:34:20.830 回答
0

您可以使用以下代码删除文档的最后一个分页符:

        var doc = new Aspose.Words.Document();
        //last page break in document
        var run = doc.GetChildNodes(NodeType.Run, true)
                .Cast<Run>().Where(obj => obj.Text.Contains(ControlChar.PageBreak)).LastOrDefault();
           //Replace Page break chracter with empty string
            run.Text = run.Text.Replace("" + ControlChar.PageBreak, " ");
于 2017-01-16T11:47:36.713 回答