linkedinfacebooktwittermenuarrow-up

GemBox Support Forum

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