Key Not found Exception

System.Collections.Generic.KeyNotFoundException: The given key ‘00000000-0000-0000-0000-000000000000, 0’ was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at .()
at .()
at .()
at .(Stream )
at .(Stream )
at .(Stream , String )
at tream , String , Boolean )
at .(Stream , Document , LoadOptions )
at Aspose.Note.Document.(Stream , LoadOptions )
at Aspose.Note.Document…ctor(Stream inStream, LoadOptions loadOptions)
at Aspose.Note.Document…ctor(Stream inStream)

I’m getting this exception while converting onenote document to pdf on the below code, please resolve this issue:

public List TransformOneNoteFileToPdfStream(byte[] documentBytes)
{
var pdfContent = new MemoryStream();
var license = new License();
license.SetLicense(“Aspose.Note.NET.lic”);
var document = new Document(new MemoryStream(documentBytes));
var pdfDocuments = new List();
IList pages = document.GetChildNodes();
for (int i = 0; i < pages.Count(); i++)
{
PdfSaveOptions opts = new PdfSaveOptions();
opts.PageIndex = i;
opts.PageCount = 1;
document.Save(pdfContent, opts);
pdfDocuments.Add(new OneNoteConvertedFileDto
{
PageTitle = pages[i].Title?.TitleText.Text,
File = pdfContent,
PageIndex = i + 1
});
pdfContent = new MemoryStream();
}
return pdfDocuments.Count() > 0 ? pdfDocuments : null;
}

I have attached onenote file for your reference:

response.zip (70.2 KB)

@pdhakal

The KeyNotFoundException you are encountering while converting a OneNote document to PDF using Aspose.Note may be related to a specific issue with the library when handling certain OneNote files. This has been reported by other users as well, and it appears that the issue has been acknowledged by the Aspose team.

Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check for Updates: Ensure that you are using the latest version of Aspose.Note. The issue you are facing may have been fixed in a more recent release. You can check the release notes for updates here.

  2. File Compatibility: The exception may occur due to the specific OneNote file you are trying to convert. If possible, test with different OneNote files to see if the issue persists across all files or is isolated to a specific document.

  3. Error Handling: Implement error handling in your code to catch exceptions and log additional information. This can help you identify if the issue is with the document itself or the conversion process.

  4. Contact Support: If the problem continues, consider reaching out to Aspose support for further assistance. They may provide insights or workarounds specific to your case.

Here is a simplified version of your code with added error handling:

public List<OneNoteConvertedFileDto> TransformOneNoteFileToPdfStream(byte[] documentBytes)
{
    var pdfDocuments = new List<OneNoteConvertedFileDto>();
    try
    {
        using (var pdfContent = new MemoryStream())
        {
            var license = new License();
            license.SetLicense("Aspose.Note.NET.lic");
            var document = new Document(new MemoryStream(documentBytes));
            IList<Page> pages = document.GetChildNodes<Page>();

            for (int i = 0; i < pages.Count; i++)
            {
                PdfSaveOptions opts = new PdfSaveOptions
                {
                    PageIndex = i,
                    PageCount = 1
                };
                document.Save(pdfContent, opts);
                pdfDocuments.Add(new OneNoteConvertedFileDto
                {
                    PageTitle = pages[i].Title?.TitleText.Text,
                    File = new MemoryStream(pdfContent.ToArray()), // Create a new MemoryStream for each page
                    PageIndex = i + 1
                });
                pdfContent.SetLength(0); // Clear the MemoryStream for the next page
            }
        }
    }
    catch (Exception ex)
    {
        // Log the exception or handle it as needed
        Console.WriteLine($"An error occurred: {ex.Message}");
    }
    return pdfDocuments.Count > 0 ? pdfDocuments : null;
}

This code includes error handling and ensures that a new MemoryStream is created for each page to avoid issues with overlapping data.

If you continue to experience the KeyNotFoundException, it may be beneficial to provide the specific OneNote file to Aspose support for further investigation.

Sources:
[1]: KeyNotFoundException when loading the file

response.zip (70.2 KB)

please take this file as a reference for the above issue

@pdhakal,

Which version of Aspose.Note for .NET you are using. Please try using Aspose.Note for .NET 24.9. I have tested using the following line of code with Aspose.Note for .NET 24.9 and it works fine.
e.g.,

var document = new Document("e:\\test2\\response.one");

Let us know with details if you still find the issue.