InsertBefore/After page break problem

Hi,
I’m building a document using the aspose word control and at some point I have to append other documents to the beginning or the end of the main one. My problem is that some of these documents have or not break page on the top respectively on the end of the document, but when I import the sections, all the documents are treated equally, meaning that all of them have break page. I tried to set the "SectionStart"to continuous on the main document and on the source document, but I didn’t get any result. The main issue with this is that we want to allow our users to be able to set themselves the page breaks when they see fit.
Here is a code sample:

Document doc = new Document(content);
DocumentBuilder builder = new DocumentBuilder(doc);
foreach(var include in includes)
{
    Document includeDoc = new Document(include[1]);
    if (includeType == IncludeTypes.FirstPage)
        builder.MoveToDocumentStart();
    else
        builder.MoveToDocumentEnd();
    foreach(Section section in includeDoc)
    {
        Node newSection = doc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
        if (includeType == IncludeTypes.FirstPage)
            doc.InsertBefore(newSection, builder.CurrentSection);
        else
            doc.InsertAfter(newSection, builder.CurrentSection);
    }
}

Hi

Thanks for your request. Could you please attach your documents here for testing? I will check the problem and provide you more information.
Best regards,

Example 1:

content1 = content;

content2= break page* + content; <= include type = last page;

content3 = content; <= include type = first page;

*break page is located before content

expected resulting document:

content 3, content1, break page, content 2

actual resulting document:

content 3, break page, content1, break page, content 2

Example 2:

content1 = content;

content2=content; <= include type = last page;

content3 = content; <= include type = first page;

expected resulting document:

content 3, content1, content 2

actual resulting document:

content 3, break page, content1, break page, content 2

For the examples higher, you can take content as 1 page text document. If you need actual example i can provide that as well, but its reproducible with any document.

Hi
Thank you for additional information. Please try using the following code to insert document at the end and at the beginning of the main document:

///
/// Append one document to another. Souce document will be included at the end of the destination document.
///
private void AppendDocument(Document dstDocument, Document srcDocument, bool includePageBreakBefore)
{
    // Set section start of the first section of the source document.
    srcDocument.FirstSection.PageSetup.SectionStart = includePageBreakBefore ? SectionStart.NewPage : SectionStart.Continuous;
    // Append source document to the destination.
    dstDocument.AppendDocument(srcDocument, ImportFormatMode.KeepSourceFormatting);
}

///
/// Prepend one document to another. Souce document will be included at the beginning of the destination document.
///
private void PrependDocument(Document dstDocument, Document srcDocument, bool includePageBreakAfter)
{
    // Set section start of the first section of the destination document.
    dstDocument.FirstSection.PageSetup.SectionStart = includePageBreakAfter ? SectionStart.NewPage : SectionStart.Continuous;
    NodeImporter importer = new NodeImporter(srcDocument, dstDocument, ImportFormatMode.KeepSourceFormatting);
    // Insert source document at the beginning of the destination document.
    for (Section sect = srcDocument.LastSection; sect != null; sect = (Section)sect.PreviousSibling)
        dstDocument.Sections.Insert(0, importer.ImportNode(sect, true));
}

Hope this helps.
Best regards,

I’m sorry, but you got it wrong; there’s no break page from word editor set and no aspose page break set. But when i am building the document(see example 1 and 2 higher), the end result document has page break.
The continuous section start doesn’t work. (already tried that before even posting here).
I will be soon adding documents to exemplify this state of fact.

Hi

Thanks for your inquiry. I used the following code and the output document looks as expected. There is blank page at the beginning of the LastPage document.

Document firstDoc = new Document(@"Test001\FirstPage.doc");
Document lastDoc = new Document(@"Test001\LastPage.doc");
Document mainDoc = new Document(@"Test001\MainDocument.doc");
PrependDocument(mainDoc, firstDoc, false);
AppendDocument(mainDoc, lastDoc, false);
mainDoc.Save(@"Test001\out.doc");

Could you please also attach expected output? I will check it and provide you more information.
Best regards,

please note the difference between the two documents.

Hi
Thank you for additional information. However, I suppose you created both document manually in MS Word. Is that right? Have you tried using the code I provided? In my output document there is no page break after the document include at the beginning of the main document.
Please provide me simple code or application (including test files), which you use for testing.
Best regards,

Hi again,
We have used the code sample you provided; however its the same result .
I have the content and a first page include.
Lets assume first page contains two lines, the content is 2 pages long. I want the output document to have no break after the two lines of the first page. That code you have provided doesn’t work to do this.
Using “continous” is not working and code sample or/and test files are hard to provide at this point, because we are working/testing with a production enviroment. Stripping out the confidential content is quite imposible at this point because our deadline is very close.
This is the code used:

doc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
if (includeTypeID == (int) IncludeTypesEnum.LastPage)
    doc.AppendDocument(includeDoc, ImportFormatMode.KeepSourceFormatting);
else
{
    NodeImporter importer = new NodeImporter(includeDoc, doc, ImportFormatMode.KeepSourceFormatting);
    for (Section sect = includeDoc.LastSection; sect != null; sect = (Section) sect.PreviousSibling)
        doc.Sections.Insert(0, importer.ImportNode(sect, true));
}

Hi

Thanks for your inquiry. I think, there can be only three possible reasons why page break is inserted after the first page document:

  1. First Page document contain this page break at its end.
  2. Main document contains page break at its beginning.
  3. You insert this page break during building the final document.

As I mentioned earlier I cannot reproduce this issue on my side. I used the same code I provided earlier. Please see the attached documents. Input documents are the same as you provided earlier.
Best regards,

I think our problem just got worse; please see the attachment:
two output files.

output1 = corect document, done by code deploy 1
output2 = wrong document, done by code deploy 2

the code and the db structure is the same for the two deploys; even more, i switched between code 1 and code 2 and the same output.
Replicated on vista and xp. On both the good version works, on the wrong one doesn’t.
Also, on the output2, the word says its continous section break (check it in outline view ), but it treats it as a section next page break.
what could it be the issue? could it be the office 2003 format at fault?
Aditional information:
OS for output1: win 2008
OS for output2: win Xp

Hi

Thanks for your inquiry. The problem can occur if paper size of sections is different. To resolve the problem you can use two approaches:

  1. Set the same paper size of the source document’s sections as specified in the destination document. Please see the following code:
///
/// Prepend one document to another. Source document will be included at the beginning of the destination document.
///
private void PrependDocument(Document dstDocument, Document srcDocument, bool includePageBreakAfter)
{
    // Set section start of the first section of the destination document.
    dstDocument.FirstSection.PageSetup.SectionStart = includePageBreakAfter ? SectionStart.NewPage : SectionStart.Continuous;
    // Set size of the last section of the source document.
    srcDocument.LastSection.PageSetup.PageWidth = dstDocument.FirstSection.PageSetup.PageWidth;
    srcDocument.LastSection.PageSetup.PageHeight = dstDocument.FirstSection.PageSetup.PageHeight;
    NodeImporter importer = new NodeImporter(srcDocument, dstDocument, ImportFormatMode.KeepSourceFormatting);
    // Insert source document at the beginning of the destination document.
    for (Section sect = srcDocument.LastSection; sect != null; sect = (Section)sect.PreviousSibling)
        dstDocument.Sections.Insert(0, importer.ImportNode(sect, true));
}
  1. If you do not need to retain sections from the source document (copy only content to the destination document), you can try using the following code:
///
/// Prepend one document to another. Source document will be included at the beginning of the destination document.
///
private void PrependDocument(Document dstDocument, Document srcDocument, bool includePageBreakAfter)
{
    // Set section start of the first section of the destination document.
    dstDocument.FirstSection.PageSetup.SectionStart = includePageBreakAfter ? SectionStart.NewPage : SectionStart.Continuous;
    NodeImporter importer = new NodeImporter(srcDocument, dstDocument, ImportFormatMode.KeepSourceFormatting);
    // Insert source document at the beginning of the destination document.
    for (Section sect = srcDocument.LastSection; sect != null; sect = (Section)sect.PreviousSibling)
        dstDocument.FirstSection.PrependContent((Section)importer.ImportNode(sect, true));
}

Hope this helps.
Best regards,