Copy Content at spezific position between documents

Hi,

i have 2 documents. One is full with tableTemplates and the other contains header/footer.

The Templates in doc1 have all the same structure:

-Firs Line: A static, individual name for that table
-Next Line: A mergeField, which will be the heading after the template is filled
-Next Lines: The Table itself, containing 2 rows, x columns, and a mergeField in each cell

Now i need to search doc1 for the template i need, using just the name of it. Then i need to copy the header-to-be and the table below it (not the name above) and insert it in doc2 where i fill the mergeFields.

doc1 should be able do contain an almost infinte amount of templates and not only tables. I hope there is an possibility to copy anything with the same logic used to copy the tables,

Greetings,

Crazkur

Hi,

Thanks for your inquiry. To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word documents (One is full with tableTemplates and the other contains header/footer)
  • Aspose.Words generated output DOCX file showing the undesired behavior
  • Please attach your expected document here for our reference. We will investigate the structure of your expected document as to how you want your final output be generated like. You can create expected document using Microsoft Word.
  • Please create a standalone console application (source code without compilation errors) that helps us reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we’ll start investigation into your issue and provide you code to achieve the same by using Aspose.Words. Thanks for your cooperation.

PS: To attach these resources, please zip them and Click ‘Reply’ button that will bring you to the ‘reply page’ and there at the bottom you can include any attachments with that post by clicking the ‘Add/Update’ button.

Best regards,

Hi,

sorry for late response. I attached my input Documents, an example for excpected output and a little ConsoleApplication with my try.

I am not yet able to produce an ouput with Aspose.Words because of the problems you can see in the code. I gave my best to comment it.

Filling the Tables, once the are copied isnt the problem. I did this with manually added tableTemplates. Therefore only my try to copy the template(s) is included in the attached Application.

If you need any more information about my request, feel free to ask.

PS: The solution is named “Sandbox” as i use it to test whatever i need to test. You find the requested project “TemplateConsoleApp” in it.

Greetings,

Crazkur

Hi Crazkur,

Thanks for your inquiry. Please try using the following code and see attached output:

Document headersFooters = new Document(MyDir + @"Templates\HeadersFooters.docx");
Document tableTemplates = new Document(MyDir + @"Templates\TableTemplates.docx");
foreach (Field field in tableTemplates.Range.Fields)
{
    if (field.Type == FieldType.FieldMergeField)
    {
        FieldMergeField mf = (FieldMergeField)field;
        Paragraph parent = mf.Start.ParentParagraph;
        if (parent.NextSibling != null && parent.NextSibling.NodeType == NodeType.Table)
        {
            Table table = (Table)parent.NextSibling;
            Node paraNode = headersFooters.ImportNode(parent, true, ImportFormatMode.KeepSourceFormatting);
            Node tableNode = headersFooters.ImportNode(table, true, ImportFormatMode.KeepSourceFormatting);
            headersFooters.LastSection.Body.InsertBefore(paraNode, headersFooters.LastSection.Body.LastParagraph);
            headersFooters.LastSection.Body.InsertBefore(tableNode, headersFooters.LastSection.Body.LastParagraph);
            headersFooters.LastSection.Body.InsertBefore(new Paragraph(headersFooters), headersFooters.LastSection.Body.LastParagraph);
        }
    }
}
headersFooters.Save(MyDir + @"Templates\17.2.0.docx");

Hope, this helps.

Best regards,

Well this copies all tables. But i need to copy a spezific one, selected by the name written over it.

It seems not possible to search for plain text in a word document and then copy something right behind it.
Maybe using bookmarks as mark for each table is a possibility.

Edit: its working with bookmarks now:

using (FileStream templatesSteam = new FileStream(templatesPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
    Document doc = new Document(templatesSteam);
    foreach (Bookmark bookmark in doc.Range.Bookmarks)
    {
        if (bookmark.Name == nameOfTable)
        {
            _partToInsert = new Document();

            Paragraph parent = bookmark.BookmarkStart.ParentNode.NextSibling as Paragraph;
            Table table = parent.NextSibling as Table;

            Node paraNode = _partToInsert.ImportNode(parent, true, ImportFormatMode.KeepSourceFormatting);
            Node tableNode = _partToInsert.ImportNode(table, true, ImportFormatMode.KeepSourceFormatting);

            _partToInsert.LastSection.Body.InsertBefore(paraNode, _partToInsert.LastSection.Body.LastParagraph);
            _partToInsert.LastSection.Body.InsertBefore(tableNode, _partToInsert.LastSection.Body.LastParagraph);

            _partToInsert.LastSection.Body.InsertBefore(new Paragraph(_partToInsert), _partToInsert.LastSection.Body.LastParagraph);

            _partToInsert.MailMerge.Execute(new String[] { "TableHeader" }, new String[] { tableHeader });
        }
    }
}

Hi Crazkur,

It is great you were able to resolve this issue by using Bookmarks. Please let us know any time you have any further queries.

Best regards,