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

Free Support Forum - aspose.com

Replace Text keeping formating in a bookmark


I’m new to aspose word but experienced with c#.

Here is what i want to use aspose word for.

I want to make an universal report engine taking word documents as templates for printing out data provided from my own software.
Until now we used crystal reports for this purpose.
But when the user wants to change the report he must use crystal reports and the software is very complex …
So most users can’t made the changes on their own. Thats what why we want to use Simple Word templates.

I already had a look at mail merge but i think it’s not complex enough for what we need. And you need masterdata in a database.

So my plan is to make something similar to mail merge in my own code.

Input is a list of templategroups (e.g. header,order,invoicesum,taxentries,footer)
Each templategroup contains a list of records (header, footer and invoicesum only one record, orders and taxentries having a list of entries)
Each entrie is a dict of key-Value Pairs.

Each templategroup in my input data has a corresponding boobkmarked section in my templatedocument.

It’s very easy to find a bookmarked text inside an template document. I want to use these bookmarks to represent an inputtemplate defintion for an record entry in my list.
For example when you want to make an invoice a bookmark named order could represent the content of an order line which is filled wirh my data.

Inside this Bookmarks there are Variable-Fields that represents Text that should be replaced with my content at runtime.
My idea was to put the variables in curly barackets to specify such an order entry.
For example an order line could look like that.

Count: {mycount@10} Articlenr: {num@5} {price@8.2} €

So the Word user can add Text and change the formatting of an order-line …

It’s easy to find the Variables and replace the values with my own values taking the Bookmark.Text Property. But when i replace the text with my own values all formatting inside the bookmark (Fonts, Color etc.) are lost.

So i guess i have to take the range.replace command to do the job. But as far as i’ve found the replace command needs a range to do the jobs and all the search replace examples i’ve found so far are doing the search and replace inside the complete document and not limited to a bookmarked section.

I tried to extract the bookmarked section to a new temporary document so that i can do the replace process afterwards.
I found the GenerateDocument Example and it seems to work … But now i have to append the temporary document to a new outputdocument.
And thats where i’m stuck.

The output Document i genearete is simple a lot of replaced templates i write one after the other …

So first take the invoice header template Do the repacement and append it at the new created empty outputdocument.
Then for each order in the data take the order template and write the replaced ordertemplates …
When you have finished orders take the template for he invoice summary write it than do the same thing with the template for all the Taxentries until we are finished. Finally save the new created output document.

I hope i could describe what i want.

Now my questions.

  1. Is there an easy way tot o the replace limited only to an bookmarked section or do i really have to make new document for each bookmarksection.
  2. What ist he best way to do the replace of variables keeping the formating in the input document. (Certainly all variables in curly brackets must have the same color and font from the opening tot he closing brackets)
  3. How can i append multiple documents to a completlly new created document. Can you give me a clue where to start best.

With kind regards

Dr. Christian Gill


Thanks for your inquiry. In your case, we suggest you please use LINQ Reporting engine to achieve your requirement. Please read following articles:
Template Syntax
LINQ Reporting Engine API

Please refer to the following article about joining documents.
Joining and Appending Documents

Please ZIP and attach your input and expected output documents. We will then provide you more information about your query along with code.