我有这个简化的代码,它使用com.aspose.words.DocumentBuilder
从不同的word文件创建一个大文档。
for (Document contentDocument : documents) {
...
builder.insertDocument(contentDocument, ImportFormatMode.KEEP_SOURCE_FORMATTING);
builder.insertBreak(BreakType.PAGE_BREAK);
}
在每个文档之后插入分页符。
有没有办法删除最后一个分页符?
答案 0 :(得分:1)
您可以使用旧版本的循环进行循环。 在这里,我想文件是一个列表。
ret_to_user
答案 1 :(得分:1)
我更喜欢使用POI,但请查看this。
请注意以下部分
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();
}
}
答案 2 :(得分: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
是一个数组。
答案 3 :(得分: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, " ");