Find the first section of a document and insert into another

Last post 01-06-2011, 6:53 PM by aske012. 3 replies.
Sort Posts: Previous Next
  •  01-05-2011, 2:03 PM 276629

    Find the first section of a document and insert into another .NET

    I have broken a template into 5 documents.  These five documents are each have a paragraph (First Section) in them that I want to extract and add to the main document in a different order without line return.  I am using the insertdocument and mail merge methods that were given as examples in the documentation.  Each of the these methods keep putting a new line break in before the inserted document.  Does anyone have any helpful examples working with sections and documents?

     

    Thanks,

    Bryan

     
  •  01-05-2011, 4:42 PM 276657 in reply to 276629

    Re: Find the first section of a document and insert into another

    Hi Yuri,

    Thanks for your inquiry.

    Please see the code below which demonstrates how to append the content of documents at the paragraph level.

    public static void AppendDocumentAtParagraphLevel(Document dstDoc, Document srcDoc)

    {

        Paragraph lastPara = dstDoc.FirstSection.Body.LastParagraph;

     

        foreach (Section section in srcDoc.Sections)

        {

            foreach (Node blockNode in section.Body)

            {

                CompositeNode newNode = (CompositeNode)lastPara.Document.ImportNode(blockNode, true, ImportFormatMode.KeepSourceFormatting);

                Node insertNode = lastPara.LastChild;

     

                if (newNode.NodeType == NodeType.Paragraph || newNode.NodeType == NodeType.StructuredDocumentTag || newNode.NodeType == NodeType.CustomXmlMarkup)

                {

                    // Use GetChildNodes to find runs that are deeply nested even if the paragraph is wrapped within a SDT or CustomXml markup

                    foreach (Node inlineNode in newNode.GetChildNodes(NodeType.Run, true))

                    {

                        lastPara.InsertAfter(inlineNode, insertNode);

                        insertNode = inlineNode;

                    }

                }

                else if (newNode.NodeType == NodeType.Table)

                {

                    lastPara.ParentNode.InsertAfter(newNode, lastPara);

                }

            }

        }

    }

    If you have any further queries, please feel free to ask.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
  •  01-06-2011, 1:47 PM 276869 in reply to 276657

    Re: Find the first section of a document and insert into another

    That this works great.  I have not tried it yet but will this method keep the formating and spacing of tables that are merged in?
     
  •  01-06-2011, 6:53 PM 276902 in reply to 276869

    Re: Find the first section of a document and insert into another

    Hi Yuri,

    Thanks for your inquiry.

    Yes it should as the tables are imported as whole nodes. The method itself may take a bit of tweaking depending upon how you want the documents to be appended.

    If you run into any issues I will be glad to help you.

    Thanks,


    Adam Skelton
    Programming Writer
    Aspose Auckland Team
     
View as RSS news feed in XML