Converting Word doc to html using the OpenXmlPower tool gives an error: Object Reference is not installed in the object instance

I am trying to view my document on a webpage using OpenXml. I searched and found the code to convert the document to html, and then displayed this html page on a web page. I used the following code.

string path = @"C:\Users\spadmin\Desktop\New folder\HelloHTML.docx";
string hmtlFilePath = @"C:\Users\spadmin\Desktop\New folder\HelloHTML.html";

byte[] byteArray = File.ReadAllBytes(path);
using (MemoryStream memoryStream = new MemoryStream())
{
     memoryStream.Write(byteArray, 0, byteArray.Length);
     using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true))
     {
         HtmlConverterSettings settings = new HtmlConverterSettings()
         {
             PageTitle = "BIA Sample",
         };

         OpenXmlPart main = doc.MainDocumentPart;
         XElement documentElement = main.GetXDocument().Root;

         XElement html = HtmlConverter.ConvertToHtml(doc, settings); // here I got error

         File.WriteAllText(hmtlFilePath, html.ToStringNewLineOnAttributes());
     }
}

But it causes an error Object Reference Not set to an instance of an Object. Then I added the OpenXmlPowerTool project to my main project, and then debugged the code, and I found that when I pass the method docin the ConvertToHtml()properties of the document where it doesn't exist. Here are their pictures.
Before transferring, I check the properties doc: enter image description here

When ConvertToHtml()called, I check the doc property, which was below:
enter image description here

. .
- , ?
- ?

+4

Source: https://habr.com/ru/post/1533288/


All Articles