InsertDocument- source document section page orientation not copying into target document

Dear Aspose Team,

We are inserting multiple source word documents inside a main word document at a bookmarked place.
We are using the following method provided by Aspose words samples to achieve this.
https://docs.aspose.com/words/java/insert-and-append-documents/

This is working fine. Now the issue is that our source word documents have different page orientation for different sections (like portrait, landscape etc.) and InsertDocument() method not able to copy these page orientation. How can we achieve this?

We do not want to append source documents at the end of main/target document. We need to insert contents inside at a bookmark place and want source document sections’ page orientation also applied. This is very critical item for us to complete asap.

We are using latest Aspose words 13.3.0.0 and .Net 4.

Thanks and regards,
Chandra

Hi Chandra,

Thanks for you inquiry. The InsertDocument method inserts contents of one document at a specified location in another document without page orientation. In your case, I suggest you please use following method to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

static void InsertDocumentWithSectionFormatting(Node insertAfterNode, Document srcDoc)
{
    // Make sure that the node is either a pargraph or table.
    if ((!insertAfterNode.NodeType.Equals(NodeType.Paragraph)) &
    (!insertAfterNode.NodeType.Equals(NodeType.Table)))
        throw new ArgumentException("The destination node should be either a paragraph or table.");
    // Document to insert srcDoc into.
    Document dstDoc = (Document)insertAfterNode.Document;
    // To retain section formatting, split the current section into two at the marker node and then import the content from srcDoc as whole sections.
    // The section of the node which the insert marker node belongs to
    Section currentSection = (Section)insertAfterNode.GetAncestor(NodeType.Section);
    // Don't clone the content inside the section, we just want the properties of the section retained.
    Section cloneSection = (Section)currentSection.Clone(false);
    // However make sure the clone section has a body, but no empty first paragraph.
    cloneSection.EnsureMinimum();
    cloneSection.Body.FirstParagraph.Remove();
    // Insert the cloned section into the document after the original section.
    insertAfterNode.Document.InsertAfter(cloneSection, currentSection);
    // Append all nodes after the marker node to the new section. This will split the content at the section level at
    // the marker so the sections from the other document can be inserted directly.
    Node currentNode = insertAfterNode.NextSibling;
    while (currentNode != null)
    {
        Node nextNode = currentNode.NextSibling;
        cloneSection.Body.AppendChild(currentNode);
        currentNode = nextNode;
    }
    // This object will be translating styles and lists during the import.
    NodeImporter importer = new NodeImporter(srcDoc, dstDoc, ImportFormatMode.UseDestinationStyles);
    // Loop through all sections in the source document.
    foreach (Section srcSection in srcDoc.Sections)
    {
        Node newNode = importer.ImportNode(srcSection, true);
        // Append each section to the destination document. Start by inserting it after the split section.
        dstDoc.InsertAfter(newNode, currentSection);
        currentSection = (Section)newNode;
    }
}

Thanks. This is working fine. But it is NOT copying header footer of source documents into destimation documents.

  1. Can you please modify above code such that header footer are also copied from each source document (containing sections with different orientation, portrait, landscape etc.) into target document?
  2. We will have multiple source documents that we will insert into target document at a bookmarked position. Hence we need to copy header footers from each sections from each source document into related sections in target document.
    Regards,
    Chandra

Hi Chandra,

Thanks for you inquiry. The above code copies the header/footer for all sections. Could you please attach your input and expected output Word document here for testing? I will investigate as to how you want your final Word output be generated like. We will then provide you more information on this along with code.

Please note that a Word document can contain one or more sections. At the end of the section, there is a section break that separates one section from the next in a document. Each section has its own set of properties that specify page size, orientation, margins, the number of text columns, headers and footers and so on.

Section can have one Body and maximum one HeaderFooter** of each HeaderFooterType. Body and HeaderFooter nodes can be in any order inside Section. Please read following documentation link for your kind reference.
https://docs.aspose.com/words/net/working-with-sections/

Header footer also copying. Thanks.
How can we remove blank page generated after all source document sections get copy?
Also how can we ascertain if section orientation is landscape if we move to a bookmark in that section in a document?
Regards,
Chandra

Hi Chandra,

Thanks for your inquiry.
HDF:
How can we remove blank page generated after all source document sections get copy?
Please note that MS Word documents are flow documents and there is no page concept in MS Word documents. Pages are created by MS Word on the fly and unfortunately there is no direct way to remove a Blank page.

You can try removing empty paragraphs from the end of your output documents. Please use the following code snippet to achieve your requirements. Hope this helps you.

// Open template.
Document doc = new Document(@"Test001\in.doc");
// Remove the empty paragraphs if necessary.
while (!doc.LastSection.Body.LastParagraph.HasChildNodes)
{
    if (doc.LastSection.Body.LastParagraph.PreviousSibling != null &&
    doc.LastSection.Body.LastParagraph.PreviousSibling.NodeType != NodeType.Paragraph)
        break;
    doc.LastSection.Body.LastParagraph.Remove();
    // If the current section becomes empty, we should remove it.
    if (!doc.LastSection.Body.HasChildNodes)
        doc.LastSection.Remove();
    // We should exit the loop if the document becomes empty.
    if (!doc.HasChildNodes)
        break;
}
// Save output.
doc.Save(@"Test001\out.doc");

HDF:
Also how can we ascertain if section orientation is landscape if we move to a bookmark in that section in a document?

Please use the following code snippet to get the section’s orientation after moving the cursor to a bookmark.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
builder.MoveToBookmark("bm");
Node insertAfterNode = builder.CurrentParagraph;
Section section = (Section)insertAfterNode.GetAncestor(NodeType.Section);
Aspose.Words.Orientation secOrientation = section.PageSetup.Orientation;