gibberish text output in word output when generated using the template (.dotx) file

  • Thread starter Thread starter KrunalC
  • Start date Start date
K

KrunalC

Guest
This is word interop related question.

I have collection of RTF text and I need to generate the report in word by pasting this RTF content from collection in loop. I use word template (i.e. .dotx) file as a base template file so as to generate report in structured manner. In base template file, I have some placeholder texts which needs to be replaced with the RTF content. While replacing the placeholder text with RTF content I'm facing following two issues :

  1. maintaining the source formatting i.e. to apply format of RTF content (i.e. font, color, bold, etc.) in generated word report
  2. pasting of RTF text in proper order with proper content

In order to maintain source formatting while copying the RTF content, I use

Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting)

This ensures that while I copy the RTF content from my collection, original source formatting (i.e. as per RTF text) gets applied. There are few other methods to paste the content i.e. Paste, PasteSpecial but they don't maintain the source formatting and upon pasting, it takes the font as the default font of the base word template file. Problem using the PasteAndFormat method is, generated output it bit gibberish when report is generated using template file while generated output is proper when not generated using template file.

I have created the sample project (available at https://drive.google.com/open?id=1es1aBgewbJvQxmOAQF3FMhx3inu3keVy) which exactly reproduces the problem I face. In sample application, when you enter 1, it will generate the report without using the template file and that is the kind of final report I want. When you enter 2, it generates the report with gibberish text and I'm not able to figure out the reason for such output.

Can anyone pls help me to figure out what is the problem with the sample application code and help me to generate the report with option 2 same as the one generated with option 1?


Krunal C

Continue reading...
 
Back
Top