We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Missing page break after append document

I have some generic code we use to merge rtf documents into a final document. its based on code that you guys sent me after a previous bug.

attached are the two documents, if you create a blank doc, append the worksheet.rtf and then append the privacy.rtf, it fails to insert a page break between the two documents, is there any thing I can do ?

here is my code:

Document TheDoc = new Document();`

TheDoc.RemoveAllChildren();
foreach (var MergeDocName in Filenames)
{
    try
    {
        var SubDoc = new Document(MergeDocName);
        TheDoc.AppendDocument(SubDoc, ImportFormatMode.KeepSourceFormatting);
    }
    catch (Exception Ex)
    {
        // TODO: insert dummy error page.
    }
}

//change header(footer) height to 0
foreach (Section section in TheDoc.Sections)
{
    if (section.HeadersFooters.Count == 0)
    {
        PageSetup pageSetup = section.PageSetup;
        pageSetup.FooterDistance = 0;
        pageSetup.HeaderDistance = 0;
    }
    section.HeadersFooters.LinkToPrevious(false);
}

//Save doc file.
SaveOptions Options = SaveOptions.CreateSaveOptions(DestinationFormat);
Options.TempFolder = TmpFolder;

TheDoc.Save(OutFile, Options);

docs.zip (142.6 KB)
here are the docs.

@chris.turner2e199,

In this case, you need to specify that each Section in your final document starts from a new page. Here is the code to workaround this issue:

Document doc1 = new Document("D:\\temp\\Docs\\worksheet.rtf");
Document doc2 = new Document("D:\\temp\\Docs\\privacy.rtf");

Document TheDoc = new Document();
TheDoc.RemoveAllChildren();

TheDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
TheDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);

//change header(footer) height to 0
foreach (Section section in TheDoc.Sections)
{
    section.PageSetup.SectionStart = SectionStart.NewPage;
    if (section.HeadersFooters.Count == 0)
    {
        PageSetup pageSetup = section.PageSetup;
        pageSetup.FooterDistance = 0;
        pageSetup.HeaderDistance = 0;
    }
    section.HeadersFooters.LinkToPrevious(false);
}

TheDoc.Save("D:\\temp\\Docs\\18.9.rtf");

can you clarify what is a section ? Is it a document ? Is there a way that sections are created in other ways ?
This fix means we are ending up with page breaks in the middle of documents, not just at the end.

@chris.turner2e199,

To learn about Sections, please refer to the following article:
Working with Sections

so a document can already have multiple sections ? I dont need a page break between all sections, I just need a page break between inserted documents.

How do I only do a page break between documents ? Do I need to only find the first section of each new document ?

@chris.turner2e199,

Yes, a Document can have multiple Sections. In your case, you can set SectionStart.NewPage flag for the Sections inside Source/Inserted Documents only.

Document doc1 = new Document("D:\\temp\\Docs\\worksheet.rtf");
Document doc2 = new Document("D:\\temp\\Docs\\privacy.rtf");

foreach (Section section in doc1.Sections)
    section.PageSetup.SectionStart = SectionStart.NewPage;

foreach (Section section in doc2.Sections)
    section.PageSetup.SectionStart = SectionStart.NewPage;

Document TheDoc = new Document();
TheDoc.RemoveAllChildren();

TheDoc.AppendDocument(doc1, ImportFormatMode.KeepSourceFormatting);
TheDoc.AppendDocument(doc2, ImportFormatMode.KeepSourceFormatting);

foreach (Section section in TheDoc.Sections)
{   
    if (section.HeadersFooters.Count == 0)
    {
        PageSetup pageSetup = section.PageSetup;
        pageSetup.FooterDistance = 0;
        pageSetup.HeaderDistance = 0;
    }
    section.HeadersFooters.LinkToPrevious(false);
}

TheDoc.Save("D:\\temp\\Docs\\18.9.rtf");