2016-08-25 5 views
0

У меня есть этот упрощенный код, который создает один большой документ из разных файлов слов, используя com.aspose.words.DocumentBuilder.Как удалить разрыв страницы?

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

После каждого документа вставляется разрыв страницы.

Есть ли способ удалить последний разрыв страницы?

+0

почему бы не работать с с индексом основой для и пропустить последний документ? –

+0

Да, я думал об этом, но не знаю, как мой код закончится. Может быть, цикл изменится или часть «вставить» закончится в отдельном методе ... просто подумал, что это может быть еще одно решение просто удалить последний разрыв страницы. –

ответ

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); 
    } 
} 
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(); 
    } 
} 
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 является массивом.

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, " ");