System.NullReferenceException when trying to convert a Word document to PDF

I am trying to convert a Word document to a PDF with GemBox.Document (version 33.0.1372), using the code below:

public string wordToPdf(string file, string encodedContent)
{
    string fileExtension = Path.GetExtension(file).ToLower().Substring(1);
    string inFileName = Path.GetTempPath() + Guid.NewGuid().ToString() + "." + fileExtension;
    string outFileName = Path.GetTempPath() + Guid.NewGuid().ToString() + ".pdf";

    byte[] fileContents = Convert.FromBase64String(encodedContent);

    try
    {
        using (var fs = new FileStream(inFileName, FileMode.Create, FileAccess.Write))
        {
            fs.Write(fileContents, 0, fileContents.Length);
        }
    }
    catch (Exception ex)
    {
        File.Delete(inFileName);
        return null;
    }

    **DocumentModel document = DocumentModel.Load(inFileName);** // EXCEPTION HERE

    foreach (Table table in document.GetChildElements(true, ElementType.Table))
    {
        table.TableFormat.Positioning.ClearPositioning();
    }

    document.Save(outFileName);
}

On line

DocumentModel document = DocumentModel.Load(inFilename);

I get the following exception:

System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=GemBox.Document
StackTrace:
at  .( , Paragraph ,  & )
at  ..( )
at  .( , InlineCollection , String , Action1 ) at  . ( ) at  .( ) at  .( , Func1 , String , Func2 ) at  . ​ ( , String ) at  ​..( ) at  .(Stream , Action1 )
at  ​.(String , Action1 ) at  ​.(String , Action1 )
at  ​.()
at  ​.(Stream , String , DocxLoadOptions )
at GemBox.Document.DocxLoadOptions.7k4lmxmhj5tk8h83eglk6fss34tasuhn​ (Stream , String )
at GemBox.Document.DocumentModel.Load(String path, LoadOptions options)

What is the problem here?

Hi,

Please send us your Word document so that we can reproduce this issue and investigate it.

Regards,
Mario

i am facing the issue here is the sample file.

Hi Parth,

I was unable to reproduce the issue, I used the current latest version:

Please try again using the latest version of GemBox.Document.
Does this solve your issue?

Regards,
Mario

1 Like

i have update the version of Gembox.document to latest but now i am facing this issue

If you’re targeting “net6.0”, try changing it to “net6.0-windows”.
Or if you’re targeting “net7.0”, try changing it to “net7.0-windows”.

For example:

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net6.0-windows</TargetFramework>
  <UseWPF>true</UseWPF>
</PropertyGroup>

Does this solve your issue?

The latest version of GemBox.Document resolves this issue, according to my testing anyway.