This project has moved and is read-only. For the latest updates, please go here.

Null refrence exception at converter,parse(html) on encountering an image tag <Img

Sep 3, 2012 at 1:35 PM

Hi ,

I am getting a null reference exception at converter.parse(html) when i try to process an image with a refrenced Url in sharepoint Rich text field  

following is code i have used :

 HtmlConverter converter = new HtmlConverter(mainPart);               

converter.ImageProcessing = NotesFor.HtmlToOpenXml.ImageProcessing.ManualProvisioning;

 converter.BaseImageUrl = new Uri("~rootsiteurlstring~`");                converter.ConsiderDivAsParagraph = true;     

  converter.ProvisionImage += converter_ProvisionImage;

converter_ProvisionImage(object sender, NotesFor.HtmlToOpenXml.ProvisionImageEventArgs e)

{// Read the image from the url:     

  WebClient client = new WebClient();

e.Data = client.DownloadData(e.ImageUrl);    }

sb.Append("<div><img style=\"width:139px;height:80px\" alt=\"null\" src=\"\" width=\"387\" height=\"110\" /></div> ");                string html = sb.ToString();


var rParagraphO = converter.Parse(html); // this is where i get Object not set to refrence exception message 

Could anyone please give me some inputs as to what might be the issue ??

Thanks in advance :)

Sep 3, 2012 at 2:21 PM

I just have tested your code and got no errors.
Sorry for the stupid question but is sb was instancied before calling Append()?
You know in Visual Studio, the NullReferenceException always displays the next line...

Sep 3, 2012 at 3:25 PM

thanks for the prompt reply :) yes it was instantiated before using the method sb.append()

Sep 3, 2012 at 3:27 PM
Edited Sep 3, 2012 at 3:36 PM

That is pretty strange ! i get an error as soon as converter.parse() is invoked only if the image is to be downloaded in the word doc

Could this issue be due to network permissions ? when I reference the URL directly in Rich text field the picture appears but the same url cannot be accessed for download by the code .. I am absolutely clueless here and relatively quite new to all this :) thank you in advance


Sep 3, 2012 at 4:09 PM

Why don't you let the library download automatically for you the image?
Btw, I recommend you to read the documentation to resolve many bugs (image relative, proxy, relative url, ...).

If your WebClient should have failed, it should have thrown a WebException. Can you send me a sample of your project at onizet at
Which version do you use: the "release" one or the latest source code?

Sep 4, 2012 at 5:32 AM

I am using htmlToOpenXML 1.4 , and i tried automatic download , got the same exception there too

the code sample that I am using in my project , it doesnot throw errors on encountering any other html tags like table or font / styles , but only at img .

I have tried automatic download too and it throws the same exception .

Any help would be appreciated ! thanks in advance .

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using DocumentFormat.OpenXml;

using DocumentFormat.OpenXml.Wordprocessing;

using DocumentFormat.OpenXml.Packaging;

using NotesFor.HtmlToOpenXml;

using System.Net;

using System.IO;

namespace ConsoleWordDoc

{   class Program  {

       static public void converter_ProvisionImage(object sender, NotesFor.HtmlToOpenXml.ProvisionImageEventArgs e)

       { // Read the image from the url:    webClient client = new WebClient();

            e.Data = client.DownloadData(e.ImageUrl);       }

       static void Main(string[] args)

       {   string documentName = "StudyBriefTest" + ".docx";

           using (WordprocessingDocument myDoc = WordprocessingDocument.Create(documentName, WordprocessingDocumentType.Document))

           {  MainDocumentPart mainPart = myDoc.AddMainDocumentPart();

               //Create Document tree for simple document.

               mainPart.Document = new Document();

               //Create Body (this element contains

               //other elements that we want to include

               Body body = new Body();

               //Create paragraph

               Paragraph paragraph = new Paragraph();

               Run run_paragraph = new Run();

               // we want to put that text into the output document

               Text text_paragraph = new Text("Study Brief");

               //Append elements appropriately.




                //HtmlConverter class

               HtmlConverter converter = new HtmlConverter(mainPart);

               converter.ImageProcessing = NotesFor.HtmlToOpenXml.ImageProcessing.ManualProvisioning;

               converter.ProvisionImage += converter_ProvisionImage;

               //converter.BaseImageUrl = new Uri("~rootsiteurlstring~`");

                 Table table = new Table();

              TableProperties tblPr = new TableProperties();

               TableBorders tblBorders = new TableBorders();



               TableRow tr;

               TableCell tc;

                tr = new TableRow();

               tc = new TableCell();

               StringBuilder sb = new StringBuilder();

               sb.Append("<div><div><img style=\"width:139px;height:80px\" alt=\"null\" src=\"\" width=\"387\" height=\"110\"></img></div></div> ");

               string html = sb.ToString();

               tc.Append(new Paragraph(new Run(new Text("Objective"))));


               TableCell tcRichO = new TableCell();


               {   var rParagraphO = converter.Parse(html);

                   for (int p = -1; p < rParagraphO.Count; p++)

                   {  tcRichO.Append(rParagraphO[p]);

                   }   tr.Append(tcRichO);  }

               catch (Exception ex){ }   table.Append(tr);  } }}}

Sep 6, 2012 at 5:48 PM


Sorry for delay.
The reason your code fails is because the provided document miss the Body element. You forget to add it to "Document"

Here is the correct version:

using (WordprocessingDocument myDoc = WordprocessingDocument.Create(documentName, WordprocessingDocumentType.Document))
       MainDocumentPart mainPart = myDoc.AddMainDocumentPart();
       new Document(new Body()).Save(mainPart);

              new Paragraph(
                     new Run(new Text("Study Brief"))


by the way, your loop will failed with "for (int p=-1;)" where it should starts a 0.

Sep 7, 2012 at 8:28 AM

hey , thank you for the reply 
the above code is missing a snippet actually






I get an error at the Converter,parse(html ) as soon as the <img> tag is encountered , rest of  the tags work fine , really cant figure out the reason :(  

Sep 7, 2012 at 8:46 AM

the reason the <img> tag is crashing is because I expect to call mainPart.Document.Body but the later is null.
If you want, you can replace your code related to table into the html snippet. The lib deals with <table>.