Insert HTML with <P> or <DIV> Tags in Word DOCX Document using C# .NET & Avoid Null Reference Exception

Hi,

I’m trying to insert an html content into a table cell in my document.
It throws NullReferenceException When the html contains “P” or “DIV” tags.

   System.NullReferenceException : Object reference not set to an instance of an object.
   at Aspose.Words.DocumentBuilder.InsertParagraph()
   at    . ()
   at    . ()
   at    .  (Boolean )
   at    . (Boolean )
   at    . (    , Boolean )
   at    . (    , Boolean )
   at    . (String , DocumentBuilder ,     )
   at    .       ()
   at Aspose.Words.DocumentBuilder. (String ,     )
   at Aspose.Words.DocumentBuilder.InsertHtml(String html, Boolean useBuilderFormatting)

I have attached a sample project for your reference.

TableSample.zip (109.3 KB)

Please make sure to uncomment the line number 100 in order observe the error.

Do not hesitate to contact me should you require further clarifications.

Appreciate your prompt response in this regard.

Cheers

@Efic_Helpdesk,

We have logged this problem in our issue tracking system with ID WORDSNET-20786. We will further look into the details of this problem and will keep you updated on the status of the linked issue. We apologize for your inconvenience.

@awais.hafeez,

Thanks for the prompt reply.

Please keep me posted the progress as I’m blocked by this issue.

Small clarification, is this is the same forum all the licensed users must log their issues or do they have a separate place?

Regards,

@Efic_Helpdesk

Yes, this is the forum all the licensed users ask or post their pre-sales or post-sales technical issues. Here you get a response within 12 hours and support team works with you proactively.

However, there is another Paid Support option which is handled separately: Paid Support Helpdesk - aspose.com.

Please do let us know if you have any further questions.

Thanks @shahzadlatif for the feedback.

Also, could please tell me how can I check the status of the issue created by @awais.hafeez in the tracking system? [ID: ID WORDSNET-20786]

How long do I have to wait to see a feedback for the above ticket?

Regards,

@Efic_Helpdesk,

Issue status can be viewed at the bottom of forum threads. I am attaching screenshot here for your reference:

Null Reference Exception when inserting HTML content with p or div tags

Other than that, there is no direct way that you can use to interact with the issue tracking system. But, you are welcome to ask your issue status via this forum thread. We will verify the status from our internal issue tracking system and reply you about the progress. Currently, WORDSNET-20786 is pending for analysis and is in the queue. There are no estimates (ETA etc) available at the moment. We will notify you here as soon as this issue will get resolved in future.

Thanks @awais.hafeez,

This is bit urgent as I’m blocked by this issue.

Appreciate your support in this regard.

Regards

@Efic_Helpdesk,

We have logged your concerns in our issue tracking system and will keep you updated on any further updates.

1 Like

@Efic_Helpdesk,

Regarding WORDSNET-20786, we have completed the work on this issue and concluded to close this issue with “Not a Bug” status. Please see the following analysis details:

There is an attempt to insert HTML to a Run before a Paragraph with this Run gets attached to the parent Cell. DocumentBuilder.InsertParagraph method tries to call CompositeNode.InsertAfter method for a parent node of the current Paragraph, but the current Paragraph is not yet attached, and its ParentNode is null.

So to workaround this problem, the line 117

itemDataCell.AppendChild(itemDataPara);
(line 117 in the Program.cs of the attached code) should be moved to a line before this one

builder.InsertHtml(r.Text, true);


P.S. Here is a simplified test that reproduces the same case:
DocumentBuilder builder = new DocumentBuilder();
builder.StartTable();
builder.InsertCell();

Cell itemDataCell = new Cell(builder.Document);
Paragraph itemDataPara = new Paragraph(builder.Document);
Run itemDataRun = new Run(builder.Document);
itemDataPara.AppendChild(itemDataRun);

builder.MoveTo(itemDataRun);
builder.InsertHtml("<p>Text</p>");
// !!!! This line should be placed before the previous one to make this code right
itemDataCell.AppendChild(itemDataPara);

builder.EndTable();