Reputation: 2199
I am dynamically generating a word document and need to replace my special tags using a html content which is generated using CKeditor control. Here the word document i am using is a template which contains pre entered texts and for generating document i am using Microsoft.Office.Interop.Word. Now i can find my special tag using interop's doc.range.Find methord. But when i want to replace this tag with a HTML content without losing its style. how can i do this?
using doc.range.Find.replacement.text is not practical because it will just replace the whole tag with html text.
Example: In my word document i have a special tag like shown below
##<Special Conditions Frag>##
during document generation i need to accept some text from user which is entered via ckeditor control(It will be in HTML format with body tag and all). And this html content i need to replace with above special conditions frag tag in word document.
My HTML content looks like below
<html>
<head>
<title></title></head>
<body>
<p>
<strong>Sample text</strong>
</p>
</body>
</html>
Upvotes: 2
Views: 8694
Reputation: 676
From XML, to OOXML, to Interop, indeed there are a lot of possible variants.
One is to use the Clipboard to do that.
string html = Clipboard.GetText(TextDataFormat.Html);
Where you can manipulate your html string, while sending to Clipboard any html code that you want.
//save the html content to a file
File.WriteAllText(temporaryFilePath, html);
You can save it in a file and open the file, you'll see in the browser the real transformation of the html code.
If you want to implement it into Word now, you can insert that file content with:
s.Range.InsertFile(temporaryFilePath);
If you want to go through all your document, you might use a loop and check for specific place replacement
foreach (NetOffice.WordApi.InlineShape s in docWord.InlineShapes)
{
if (s.Type==NetOffice.WordApi.Enums.WdInlineShapeType.wdInlineShapePicture && s.AlternativeText.Contains("|"))
{
//save the html content to a file
File.WriteAllText(temporaryFilePath, html);
s.Range.InsertFile(temporaryFilePath);
}
}
That's a way to integrate HTML into Word. Another way, ugly way, is to transform your html code into a picture and insert the picture, which I wouldn't recommand that one. If you plan to extend that for Excel, it works the same way, if you try to implement html into Powerpoint, it won't work, but you can do it in 2steps. First paste your html into Excel, select the pasted content and paste it into Powerpoint, it works fine. If you paste a table, no problem, just do it and later, you write a 2nd loop to create a new table instead of that one you took from Excel into Powerpoint.
Edited: According to the error In your main you should add :
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
The issue can be backtracked here: C# WinForms: How to set Main function STAThreadAttribute
Upvotes: 2