Reputation: 741
I am using C# to generate reports in MS Word. I have a predefined .docx template and I use mail merge fields which are then updated by my app.
I retrieve small sections of HTML content from a database and insert them into the appropriate points in the MS Word document as follows:
I retrieve the HTML content
I wrap the content with tags:
<HTML>
<TITLE></TITLE>
<BODY>
... content ...
</BODY>
</HTML>
Save html content into a temporary file
Use the Word._Application.Selection.InsertFile() method to insert the HTML file
Example of converted HTML code:
<html>
<title></title>
<body>
<table border="0" cellpadding="0" cellspacing="0" style="width: 700px; ">
<tbody>
<tr>
<td style="padding-top: 0cm; padding-right: 5.4pt; padding-bottom: 0cm; padding-left: 5.4pt; text-align: center; vertical-align: middle; width: 150px; ">
<img alt="see attached image: screenshot-3.jpg" src="XXXXX/screenshot-3.jpg" style="width: 60px; height: 84px; " />
</td>
</tr>
</tbody>
</table>
<br />
</body>
</html>
The above system works, but I find that tables and images are 'auto-scaled' by MS Word, with images being rendered with their original dimensions as opposed to the embedded style="width, height" attributes.
My HTML is rendered 100% in a browser, but not in MS Word.
Any ideas on how to get my scaling right would be most appreciated.
Many thanks
Upvotes: 1
Views: 1675
Reputation: 354576
In my experience HTML may be the easiest way of getting rich text into a Word document, but it's also prone to get formatting wrong and you don't have control over the finer details.
Have you tried using the width
and height
attributes for the img
element? Another option might be to try using RTF (which is on par with Word, feature-wise).
Upvotes: 1