Object reference not set to an instance of an object. Error While converting DOCX to PNG

Hey i am getting issue while converting docx to png using Aspose.Words Version=25.11.0,
attaching docx file for referance
ttt.docx (35.4 KB)

@umittal

Summary
You are getting a Object reference not set to an instance of an object (NullReferenceException) when trying to convert a .docx file to PNG with Aspose.Words 25.11.0.

Why it happens
The evidence shows that rendering problems (including NullReferenceExceptions) often occur when the document contains vector graphics such as EMF/WMF images. In a similar scenario Aspose.Words users resolved the issue by switching the text‑shaper to HarfBuzz, which improves the rendering pipeline for documents that contain these graphics. The problem is not limited to PDF – the same rendering engine is used for PNG, so the same fix is applicable.

What to do next

  1. Check the document for vector graphics
  • Unzip the .docx (unzip ttt.docx -d out) and look in the word/media folder for files with extensions .emf, .wmf or .svg.
  • If you find such images, try replacing them with raster images (PNG/JPEG) and run the conversion again.
  1. Enable the HarfBuzz text‑shaper before saving
import com.aspose.words.*;

public class DocxToPng {
public static void main(String[] args) throws Exception {
Document doc = new Document("ttt.docx");

// Use HarfBuzz text shaper (helps with EMF/WMF graphics)
doc.getLayoutOptions().setTextShaperFactory(
com.aspose.words.shaping.harfbuzz.HarfBuzzTextShaperFactory.getInstance());

// Optional: hide revision marks
doc.getLayoutOptions().getRevisionOptions().setShowRevisionBars(false);
doc.getLayoutOptions().getRevisionOptions().setShowRevisionMarks(false);
doc.acceptAllRevisions();

doc.save("output.png", SaveFormat.PNG);
}
}

The same property is used in C# as document.LayoutOptions.TextShaperFactory = Aspose.Words.Shaping.HarfBuzz.HarfBuzzTextShaperFactory.Instance;.

  1. Upgrade to the latest Aspose.Words build
  • Check the Aspose website or your package manager for a newer version (e.g., 25.12.x or later).
  • Release notes often contain fixes for rendering‑related NullReferenceExceptions, especially those involving EMF images.
  1. Collect more diagnostics if the problem persists
  • Full stack trace of the exception.
  • The exact code you use for loading and saving (including any SaveOptions).
  • License status (evaluation vs. licensed).
  • JDK/.NET runtime version and operating‑system details.
  1. If no vector graphics are present
  • Ensure revisions and comments are disabled as shown in step 2.
  • Call doc.updateFields(); before saving to refresh any fields that might be corrupted.

Next steps – Start with steps 1 and 2. If the exception remains, upgrade Aspose.Words (step 3) and, if needed, provide the diagnostics listed in step 4 for further analysis.

:warning: I could not fully verify this explanation against the retrieved sources. Please double-check the behaviour in your environment and confirm the exact SDK/version.

Hey , given docx does not contains any of the *.emf , *.wmf or *.svg file and doc.updateFields(); does not work in my case
Environment : windows 11 , .net 8
License Information : License with accounting@bold.com , enterprise license
Aspose version : Aspose.Words 25.11.0 (which is the latest one)
code snippit :

 public async Task<List<byte[]>> ConvertToImageAsync(byte[] fileBytes)
 {           

     var output = await Task.Run(() =>
     {
         List<byte[]> output = new List<byte[]>();

         Document doc;
         using (MemoryStream ms = new MemoryStream(fileBytes))
         {
             doc = new Document(ms, new HtmlLoadOptions
             {
                 Encoding = Encoding.UTF8,
                 ConvertSvgToEmf = true,
                 ConvertMetafilesToPng = false
             });
         } 
		 
		ImageSaveOptions options = new ImageSaveOptions(SaveFormat.Png);
		options.Resolution = 200;
		options.Scale = 1;
		options.PixelFormat = ImagePixelFormat.Format32BppPArgb;
		 
         using (MemoryStream ms2 = new MemoryStream())
         {
             for (int page = 0; page < doc.PageCount; page++)
             {
                 Document extractedPage = doc.ExtractPages(page, 1);
                 extractedPage.Save(ms2, options);
                 output.Add(ms2.ToArray());
                 ms2.SetLength(0);         
             }

         }

         return output;

     });
     return output;

 }

Please check this we are getting issues
ttt.docx (35.4 KB)

@umittal
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-28825

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.