Getting error while converting word document to pdf

GeneratedDoc.docx (23.0 KB)
I am trying to convert this docx file to pdf using the below method.

wordDocument.Save(pdfDocStream, pdfSaveOptions);

But I am getting null reference exception error on this line of code.

However, if I add this line of code before saving to pdf, the pdf conversion works fine
wordDocument.Styles.DefaultParagraphFormat.ClearFormatting();

I want to understand what is happening in the background, because when I compared the document.xml before and after that line of code, I did not see any difference.

What is the impact of using this line of code?
wordDocument.Styles.DefaultParagraphFormat.ClearFormatting();

@spconga

The NullReferenceException you are encountering during the conversion of a Word document to PDF is likely related to the default paragraph formatting in your document. When you call wordDocument.Styles.DefaultParagraphFormat.ClearFormatting(), it resets the formatting of the default paragraph style, which can help avoid issues with certain styles or formatting that may not be compatible with the PDF conversion process.

The reason you might not see any difference in the document.xml before and after executing that line could be due to the fact that the formatting being cleared is not explicitly visible in the XML structure. However, it can still affect how the document is processed during conversion.

By clearing the formatting, you are essentially ensuring that the document uses a clean slate for paragraph formatting, which can help prevent any hidden or problematic styles from causing issues during the conversion. This is particularly useful if the document contains complex formatting or styles that may not translate well into PDF format.

If you want to further investigate the issue, you could try the following steps:

  1. Check for any specific styles applied to paragraphs that might be causing the exception.
  2. Review the document for any unsupported features or formatting that may not be compatible with PDF conversion.
  3. Consider using a try-catch block around your conversion code to handle exceptions gracefully and log more details about the error.

If you continue to experience issues, please provide more details about the document’s content or any specific styles that are being used, and I can assist you further.

But is it advisable to use this method before converting word to pdf every time?
Will it impact any other things?
wordDocument.Styles.DefaultParagraphFormat.ClearFormatting()

@spconga
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-27985

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.