Replace placeholders in word document using OpenXML in C#

  • Thread starter Thread starter MayuriB
  • Start date Start date
M

MayuriB

Guest
I have a requirement in my MVC application where I want to programmatically replace placeholders with text or images from DB in a predefined document template.
Scenario: The application needs to generate one report based on some survey questionnaire answers. Report should be generated from a predefined document template(.docx) file available with all styling and formatting applied. Now I want to read this template, search for the placeholders and replace it with appropriate data without disturbing its formation and style.

How to achieve this using OpenXML SDK methods? I don’t want to use any third party tools. I did some R&D on google and tried few options. Tried using Stream, reading all text and then replace but it disturbs the document format. Also tried using searching placeholders in paragraphs, Runs, Text Descendants but with this it does not replace all the placeholders. some placeholders get ignored. Also replaced text is not getting reflected in final report document. How do we save word documents using OpemXML?

Please suggest as I have already spent lot of time figuring out the proper solution to my query.

Continue reading...
 
Back
Top