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

Free Support Forum - aspose.com

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);
<span style="color:#569cd6;">foreach</span> (<span style="color:#4ec9b0;">Bookmark</span> bookmark <span style="color:#569cd6;">in</span> doc<span style="color:#b4b4b4;">.</span>Range<span style="color:#b4b4b4;">.</span>Bookmarks)
{
    <span style="color:#569cd6;">if</span> (bookmark<span style="color:#b4b4b4;">.</span>Name <span style="color:#b4b4b4;">==</span> nameOfTable)
    {
        _partToInsert <span style="color:#b4b4b4;">=</span> <span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">Document</span>();

        <span style="color:#4ec9b0;">Paragraph</span> parent <span style="color:#b4b4b4;">=</span> bookmark<span style="color:#b4b4b4;">.</span>BookmarkStart<span style="color:#b4b4b4;">.</span>ParentNode<span style="color:#b4b4b4;">.</span>NextSibling <span style="color:#569cd6;">as</span> <span style="color:#4ec9b0;">Paragraph</span>;
        <span style="color:#4ec9b0;">Table</span> table <span style="color:#b4b4b4;">=</span> parent<span style="color:#b4b4b4;">.</span>NextSibling <span style="color:#569cd6;">as</span> <span style="color:#4ec9b0;">Table</span>;

        <span style="color:#4ec9b0;">Node</span> paraNode <span style="color:#b4b4b4;">=</span> _partToInsert<span style="color:#b4b4b4;">.</span>ImportNode(parent, <span style="color:#569cd6;">true</span>, <span style="color:#b8d7a3;">ImportFormatMode</span><span style="color:#b4b4b4;">.</span>KeepSourceFormatting);
        <span style="color:#4ec9b0;">Node</span> tableNode <span style="color:#b4b4b4;">=</span> _partToInsert<span style="color:#b4b4b4;">.</span>ImportNode(table, <span style="color:#569cd6;">true</span>, <span style="color:#b8d7a3;">ImportFormatMode</span><span style="color:#b4b4b4;">.</span>KeepSourceFormatting);

        _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>InsertBefore(paraNode, _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>LastParagraph);
        _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>InsertBefore(tableNode, _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>LastParagraph);

        _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>InsertBefore(<span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">Paragraph</span>(_partToInsert), _partToInsert<span style="color:#b4b4b4;">.</span>LastSection<span style="color:#b4b4b4;">.</span>Body<span style="color:#b4b4b4;">.</span>LastParagraph);

        _partToInsert<span style="color:#b4b4b4;">.</span>MailMerge<span style="color:#b4b4b4;">.</span>Execute(<span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">String</span>[] { <span style="color:#d69d85;">"TableHeader"</span> }, <span style="color:#569cd6;">new</span> <span style="color:#4ec9b0;">String</span>[] { 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,