Hi,
i am using "Microsoft.Office.Interop.Word" to convert .doc files to html. but these are not processing images . please help me.. this is my code
filename = Server.MapPath("ServicesDoc.docx");
Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
//Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
object documentFormat = 10;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".html";
try
{
//Open the word document in background
ApplicationClass applicationclass = new ApplicationClass();
applicationclass.Documents.Open(ref filename);
applicationclass.Visible = false;
Document doc = applicationclass.ActiveDocument;
//doc = AC.Documents.Open(ref filename);
//Save the word document as HTML file
doc.SaveAs2(ref htmlFilePath, ref documentFormat);
doc.Close(ref missing, ref missing, ref missing);
byte[] bytes;
using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
{
BinaryReader reader = new BinaryReader(fs);
bytes = reader.ReadBytes((int)fs.Length);
fs.Close();
}
Response.BinaryWrite(bytes);
Response.Flush();
Microsoft.Office.Interop.Word.ApplicationClass AC = new Microsoft.Office.Interop.Word.ApplicationClass();
//Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
object readOnly = false;
object isVisible = true;
object missing = System.Reflection.Missing.Value;
object documentFormat = 10;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".html";
try
{
//Open the word document in background
ApplicationClass applicationclass = new ApplicationClass();
applicationclass.Documents.Open(ref filename);
applicationclass.Visible = false;
Document doc = applicationclass.ActiveDocument;
//doc = AC.Documents.Open(ref filename);
//Save the word document as HTML file
doc.SaveAs2(ref htmlFilePath, ref documentFormat);
doc.Close(ref missing, ref missing, ref missing);
byte[] bytes;
using (FileStream fs = new FileStream(htmlFilePath.ToString(), FileMode.Open, FileAccess.Read))
{
BinaryReader reader = new BinaryReader(fs);
bytes = reader.ReadBytes((int)fs.Length);
fs.Close();
}
Response.BinaryWrite(bytes);
Response.Flush();
Darren KangPosted Jul 25, 2014, 3:54 AM
Hi,
Try the following:
using Microsoft.Office.Interop.Word;
// ...
var application = new Application();
var document = application.Documents.Open(FileName: "Sample.docx");
document.SaveAs2(FileName: "Sample.html", FileFormat: WdSaveFormat.wdFormatFilteredHTML);
document.Close();
application.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
This will result in creating a "Sample.html" file together with a "Sample_files" folder that will contain its images.
But to be honest I'm not sure if you should use a C# code with word automation calls on what I presume is your web application, see here why.
Also to add I once had a need to actually have these images embedded to HTML file, rather than having them as a separate image files, and I could not figure it out if this is even possible with Interop.Word, but I managed to it with this .NET library that operates with Word files. So here is another way that you can use to achieve the conversion from a word file to a html file with C# code:
var document = DocumentModel.Load("Sample.docx");
document.Save("Sample.html", new HtmlSaveOptions() { EmbedImages = true };
hazem altawilPosted Feb 27, 2014, 3:11 AM
http://www.leadtools.com/help/leadtools/v18/dh/to/leadtools.topics.printer~pr.topics.programmingwithvirtualprinterdriver.html