Aspose Column Formatting from Document Issue

My assembly process takes a dynamic list of documents, and appends each into a single document.

The list of documents contain documents with language, and blank documents with formatting only. Formatting documents in the list dictate the format of the each document following after it.

List of documents:

COVER.DOC
TOC.DOC
SPOILER-ALERT.DOC
Format1ColA.DOC (Page Layout : 1 Column, 7.5”, even spaced, no line between)
EXPLANATION.DOC
Format2ColA.DOC (Page Layout : 2 Columns, 3.5”, not even spaced, no line between, 0.5” spacing)
MAIN-CHARACTERS.DOC
SUPPORTING-CHARACTERS.DOC
ENDSECTBREAK.DOC (Page Break)
Format1ColA.DOC (Page Layout : 1 Column, 7.5”, even spaced, no line between)
GLOSSARY.DOC

The issue is that changing from 1 column to 2 columns works fine, but changing from 2 columns to 1 column never works. EXPLANATION is correctly formatting to the 1 column. MAIN-CHARACTERS and SUPPORTING-CHARACTERS are correctly formatting to the 2 columns. However, GLOSSARY is still getting the 2 column format instead of switching to the 1 column format.

Aspose Code:

if (Path.GetFileName((string)m_oFileName).ToUpper().StartsWith("FORMAT"))
{
    // go back to end of document
    m_oAsposeBuilder.MoveToDocumentEnd();

    // insert section break at end of document to get ready for this FORMAT page
    Aspose.Words.BreakType BreakType = Aspose.Words.BreakType.SectionBreakContinuous;
    m_oAsposeBuilder.InsertBreak(BreakType);

    m_oAsposeBuilder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);

}
else
{
    m_oAsposeBuilder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
}

AssembledByAspose.DOC has the results of the Document Assembly

Old Word Interop Code:

if (Path.GetFileName((string)m_oFileName).ToUpper().StartsWith("FORMAT"))
{
    // go back to end of document
    m_oWord.Selection.EndOf(ref oUnit, ref oMovement);

    // insert section break at end of document to get ready for this FORMAT page
    m_oWord.Selection.InsertBreak(ref BreakType);

    m_oWord.Selection.InsertFile(m_oFileName.ToString(), ref m_oOptional, ref tempFalse, ref m_oOptional, ref m_oOptional);
}
else
{
    // insert the next page
    m_oWord.Selection.InsertFile(m_oFileName.ToString(), ref m_oOptional, ref tempFalse, ref m_oOptional, ref m_oOptional);
}

AssembledByWordInterop.DOC has the results of the Document Assembly

Documents listed and results from both Aspose and Word Interop can be sent in zip file.

@Egwene In your case you should use Document.AppendDocument method inserted of DocumentBuilder.InsertDocument method. Please try modifying your code like this:

if (Path.GetFileName((string)m_oFileName).ToUpper().StartsWith("FORMAT"))
{
    srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
    m_oAsposeBuilder.Document.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
}
else
{
    // go back to end of document
    m_oAsposeBuilder.MoveToDocumentEnd();
    m_oAsposeBuilder.InsertDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
}

If the problem still persist, please attach your input documents here for testing. We will check the scenario on our side and provide you more information.

Hi, this technically did work for the specific issue, however, the rest of the document is not formatting as expected now. Where can I post the documents? I have all the docs in the list above, and I have results from the Word Interop (expected results), and results from both the first issue and after the suggested changes.

@Egwene You can zip and attach the document here in the forum, just drag and drop the files in the message. We will check the issue and provide you more information. Also, it would be good if you also attach your current and expected output documents.
It is safe to attach documents in the forum, only you as a topic starter and Aspose staff can see the attachments.

thank you. the issue now is that MAIN-CHARACTERS and SUPPORTING-CHARACTERS are incorrectly formatting to 1 narrow columns instead of 2. However, GLOSSARY is now correctly in the 1 column format. see attached zip file.AsposeSupportDocs.zip (463.2 KB)

@Egwene The problem occurs because the source document is inserted into an empty section, in this case DocumentBuilder applies section properties of the source section to the target section. You can work this around by copying content node by node from the source document section into the target. For example see the following code:

string[] docs = new string[] {
    @"C:\Temp\COVER.DOC",
    @"C:\Temp\TOC.DOC",
    @"C:\Temp\SPOILER-ALERT.DOC",
    @"C:\Temp\Format1ColA.DOC",
    @"C:\Temp\EXPLANATION.DOC",
    @"C:\Temp\Format2ColA.DOC",
    @"C:\Temp\MAIN-CHARACTERS.DOC",
    @"C:\Temp\SUPPORTING-CHARACTERS.DOC",
    @"C:\Temp\ENDSECTBREAK.DOC",
    @"C:\Temp\Format1ColA.DOC",
    @"C:\Temp\GLOSSARY.DOC"
};

Document doc = null;
DocumentBuilder builder = null;
foreach (string docPath in docs)
{
    Document srcDoc = new Document(docPath);
    if (doc == null)
    {
        doc = srcDoc;
        builder = new DocumentBuilder(doc);
    }
    else if (Path.GetFileName(docPath).ToUpper().StartsWith("FORMAT"))
    {
        srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
        doc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
    }
    else
    {
        // go back to end of document
        builder.MoveToDocumentEnd();

        // Copy nodes from the first section of source document into the current section. 
        foreach(Node node in srcDoc.FirstSection.Body)
            builder.CurrentSection.Body.AppendChild(doc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));
    }
}

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

out.docx (30.3 KB)

Hello again, and thank you for your support. The code you gave did fix the issue, however, when we introduced a Format Header Footer page, it was not taking the Header and Footer at all. We found that the reason was due to page breaks within our Documents that were not carried over to the assembled document when appended.

We tinkered with the code and got the Header and Footer to show up, however, still not as expected.

foreach (Section section in srcDoc.Sections)
{
   if (srcDoc.Sections.IndexOf(section) >= 1)
   {
        m_oAsposeBuilder.MoveToDocumentEnd();
        m_oAsposeBuilder.InsertBreak(Aspose.Words.BreakType.SectionBreakContinuous);
    }
    foreach (Node node in section.Body)
    {
        m_oAsposeBuilder.CurrentSection.Body.AppendChild(m_oAsposeDoc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));

    }
}

Below is the same document list as before, except that after the EXPLANATION.DOC, there is an additional FORMATHEADERFOOTERV5.DOC . The expectation is that all pages after this one shall have the Header/Footer from this one. However, it only shows up on the last page for the GLOSSARY.DOC

COVER.DOC
TOC.DOC
SPOILER-ALERT.DOC
Format1ColA.DOC (Page Layout : 1 Column, 7.5”, even spaced, no line between)
EXPLANATION.DOC
FORMATHEADERFOOTERV5.DOC (Just Header / Footer)
Format2ColA.DOC (Page Layout : 2 Columns, 3.5”, not even spaced, no line between, 0.5” spacing)
MAIN-CHARACTERS.DOC
SUPPORTING-CHARACTERS.DOC
ENDSECTBREAK.DOC (Page Break)
Format1ColA.DOC (Page Layout : 1 Column, 7.5”, even spaced, no line between)
GLOSSARY.DOC

I attached the docs again, including the expected results as we have with the Word Interop, and the results from Aspose.
WithHeaderFooter-AsposeSupportDocs.zip (2.4 MB)

@Egwene The problem occurs because some of the section from your document do not inherit header/footer since they have their own header/footer. I think it would be correct to remove empty sections and page breaks from the end of the documents to avoid creation empty sections in the output document.
Regarding header/footer inheritance, the problem might be partially resolved by explicitly specifying that header/footer should be inherited:

string[] docs = new string[] {
        @"C:\Temp\COVER.DOC",
        @"C:\Temp\TOC.DOC",
        @"C:\Temp\SPOILER-ALERT.DOC",
        @"C:\Temp\Format1ColA.DOC",
        @"C:\Temp\EXPLANATION.DOC",
        @"C:\Temp\FORMATHEADERFOOTERV5.DOC",
        @"C:\Temp\Format2ColA.DOC",
        @"C:\Temp\MAIN-CHARACTERS.DOC",
        @"C:\Temp\SUPPORTING-CHARACTERS.DOC",
        @"C:\Temp\ENDSECTBREAK.DOC",
        @"C:\Temp\Format1ColA.DOC",
        @"C:\Temp\GLOSSARY.DOC"
    };

Document doc = null;
DocumentBuilder builder = null;
bool inheritHeaderFooter = false;
foreach (string docPath in docs)
{
    Document srcDoc = new Document(docPath);

    if (inheritHeaderFooter)
        srcDoc.FirstSection.HeadersFooters.LinkToPrevious(true);

    inheritHeaderFooter |= Path.GetFileName(docPath).ToUpper().StartsWith("FORMATHEADERFOOTER");

    if (doc == null)
    {
        doc = srcDoc;
        builder = new DocumentBuilder(doc);
    }
    else if (Path.GetFileName(docPath).ToUpper().StartsWith("FORMAT"))
    {
        srcDoc.FirstSection.PageSetup.SectionStart = SectionStart.Continuous;
        doc.AppendDocument(srcDoc, ImportFormatMode.KeepSourceFormatting);
    }
    else
    {
        foreach (Section section in srcDoc.Sections)
        {
            builder.MoveToDocumentEnd();
            foreach (Node node in section.Body)
                builder.CurrentSection.Body.AppendChild(doc.ImportNode(node, true, ImportFormatMode.KeepSourceFormatting));
            if (srcDoc.LastSection != section)
                builder.InsertBreak(Aspose.Words.BreakType.SectionBreakContinuous);
        }
    }
}

doc.UpdateFields();
doc.Save(@"C:\Temp\out.docx");

But an empty section and page break in EXPLANATION.DOC should be removed to get the correct output.