This project has moved. For the latest updates, please go here.

Html To OpenXML with Office 2007

Oct 4, 2012 at 10:22 AM

Hello to all..

Hi need an urgent help..How can I use Html To OpenXML with Office 2007..

It's working fine with 2010 but not with 2007. What can I do? Is there any version to deal with this?

Hope you can help.

 

Best Regards,

VL

Coordinator
Oct 4, 2012 at 12:39 PM

I tend to avoid replying to urgent message, mainly if you insist by a PM. I'm not a company and do not gain ANY money for this library.
Moreover, the library works with Office 2007 (which is based on OpenXml too). Maybe if you give more details about which errors you get, you will get a better reply.

Oct 6, 2012 at 6:15 PM
Edited Oct 6, 2012 at 6:37 PM

Hi.. First of all
I apologize but it was the first time I did this kind of help request and I had no intention of being inconvenient.
My problem is when I try to use my method to convert Html To Docx in a web application (in my localhost it works but when I deploy in another server I get some problems)..

 


   (.....)     if (File.Exists(tempFile)) File.Delete(tempFile);

                using (MemoryStream generatedDocument = new MemoryStream())
                {
                    using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
                    {
                        MainDocumentPart mainPart = package.MainDocumentPart;
                        if (mainPart == null)
                        {
                            mainPart = package.AddMainDocumentPart();
                            new Document(new Body()).Save(mainPart);
                        }

                        HtmlConverter converter = new HtmlConverter(mainPart);
                        Body body = mainPart.Document.Body;
                        System.Collections.Generic.IList<DocumentFormat.OpenXml.OpenXmlCompositeElement> paragraphs = converter.Parse(html);

                        for (int i = 0; i < paragraphs.Count; i++)
                        {
                            body.Append(paragraphs[i]);
                        }
                        mainPart.Document.Save();
                        
                    }

                    File.WriteAllBytes(tempFile, generatedDocument.ToArray());
                    generatedDocument.Close();
                }

                System.Diagnostics.Process p = System.Diagnostics.Process.Start(tempFile);
                p.WaitForExit(20000);
                // Close process by sending a close message to its main window.
                p.CloseMainWindow();
                // Free resources associated with process.
                p.Close();
                
                
                //Get converted file to prepare output
                FileStream fr = new FileStream(tempFile, FileMode.Open);
                byte[] pdfContent = new byte[fr.Length];
                fr.Read(pdfContent, 0, pdfContent.Length);
(.....)


After running System.Diagnostics.Process.Start(tempFile); and trying to get the created file into a File Stream (as a requirement
I need to get the binary data of the generated file) I get some access problems and the code get stopped in this method.
I even try to grant Full control access to "Everyone" in the Temporary Files but still no success..
Can anyone help me on this?

Best Regards,
VL

 

 

Coordinator
Oct 8, 2012 at 8:01 AM

from the code I see, you really don't need to call Process.Start. As I understand, you are in a Web Server, you generate a document and returns it to the user.
So you can keep this snippet:

using (MemoryStream generatedDocument = new MemoryStream())
{
    using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
    {
        ... 
    }

    SetContentType(context.Request, context.Response, templateName);
    generatedDocument.Position = 0L; // reset buffer position
    generatedDocument.WriteTo(context.Response.OutputStream);
    context.Response.Flush();
}

private void SetContentType(System.Web.HttpRequest request, System.Web.HttpResponse response, String reportName)
{
    if (reportName.Length > 128) reportName = reportName.Substring(0, 128);
    string encodedFilename = reportName.Replace(';', ' ');

    // Avoid accent encoding bug
    if (request.Browser.Browser.Contains("IE"))
    {
        encodedFilename = Uri.EscapeDataString(Path.GetFileNameWithoutExtension(encodedFilename)).Replace("%20", " ") + Path.GetExtension(encodedFilename);
    }

    // use the correct content-type (not 'application/msword') or FF will append .doc to the filename
    response.AppendHeader("Content-Disposition", "attachment;filename=\"" + encodedFilename + "\"");
    response.ContentType = "application/vnd.ms-word.document";

    // IE cannot download an MS Office document from a website using SSL if the response
    // contains HTTP headers such as:   Pragram: no-cache   and/or    Cache-control: no-cache,max-age=0,must-revalidate
    // http://support.microsoft.com/kb/316431/
    if (!(request.IsSecureConnection && request.Browser.Browser.Contains("IE")))
     response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
}