Can't save to stream

I am trying to merge several pdf documents from disk in an UWP app. I keep getting an execption when saving to the stream. (System.NullReferenceException: 'Object reference not set to an instance of an object.'). Why doesn't this work??? I can see that 
mergeDocument is valid and has the right pagecount etc.

    PdfDocument mergeDocument = new PdfDocument();

    foreach (StorageFile pdfFile in files)
    {
        PdfLoadedDocument loadedDocument = new PdfLoadedDocument();
        await loadedDocument.OpenAsync(pdfFile);
        PdfDocumentBase.Merge(mergeDocument, loadedDocument);
        loadedDocument.Close(true);
    }

     MemoryStream ms = new MemoryStream();
     await mergeDocument.SaveAsync(ms);
     mergeDocument.Close(true);
     Save(ms, "Samengevoegd.pdf");

Best regards,
Frank

1 Reply

FR Frank Roelofs June 26, 2020 02:50 PM UTC

I discovered my mistake. Forgot this line:
     mergeDocument.EnableMemoryOptimization = true
Everything works fine now.

Best regards, Frank

Loader.
Up arrow icon