Loading certain pdf file using Windows.Data.Pdf.PdfDocument causes AggregateException

ManojKumar 0 Reputation points
2023-12-07T06:50:04.18+00:00

Hi,

We are trying to load certain PDF documents using Windows.Data.Pdf.PdfDocument but we were unable to output IAsyncOperation it shows System.AggregateException: 'One or more errors occurred. while loading these PDFs. other PDFs are loading correctly.

I have attached code to replicate this issue.

we have also attached files that cause issues on the following link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/202303_ad542d9a-799861967

 private async Task<IRandomAccessStream> ConvertToRandomAccessStream(MemoryStream stream)  
{      
	try      
	{          
		var bytes = new byte[stream.Length];  
        stream.Position = 0;          
		stream.Read(bytes, 0, bytes.Length);
           StorageFolder folder = 			global::Windows.Storage.ApplicationData.Current.TemporaryFolder;
          string m_localDocumentID = "temp" + Guid.NewGuid().ToString() + ".pdf";          StorageFile sampleFile = await folder.CreateFileAsync(m_localDocumentID, CreationCollisionOption.ReplaceExisting);           global::Windows.Storage.Streams.IRandomAccessStream fileStream = await sampleFile.OpenAsync(FileAccessMode.ReadWrite);          
			Stream streamToBeWritten = fileStream.AsStreamForWrite();
          streamToBeWritten.SetLength(0);          
		streamToBeWritten.Write(bytes, 0, bytes.Length); 
         streamToBeWritten.Flush();          
		streamToBeWritten.Dispose(); 
          StorageFile file = await ApplicationData.Current.TemporaryFolder.GetFileAsync(m_localDocumentID);          return await file.OpenAsync(FileAccessMode.ReadWrite);
      }      
catch (Exception)      
{          
return null;      
}  
}   

private async void Loadbtn_Click(object sender, RoutedEventArgs e)  
{
      Stream FileStream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream($"SampleApplication.Assets.2023─Ω03╘┬┴∙╝╢╜Γ╬÷╚½3╠╫.pdf");      
MemoryStream memoryStream = new MemoryStream();      
FileStream.Position = 0;      
FileStream.CopyTo(memoryStream);      
IRandomAccessStream randomStream;      
randomStream = await ConvertToRandomAccessStream(memoryStream as MemoryStream);      IAsyncOperation<global::Windows.Data.Pdf.PdfDocument> result;      
result = global::Windows.Data.Pdf.PdfDocument.LoadFromStreamAsync(randomStream);      result.AsTask().Wait(); 
}
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Junjie Zhu - MSFT 19,931 Reputation points Microsoft Vendor
    2023-12-07T09:23:02.42+00:00

    Hi @ManojKumar ,

    Welcome to Microsoft Q&A!

    It is recommended to convert these two files into PDF file again.

    This should be a problem with the PDF file format, one characteristic of these files is that they all have specific file permissions. I'm not sure yet what's causing the issue.

    User's image

    In my test, I used Edge to open your two files, used the print(ctrl+p) function in Edge, and after re-exporting the pdf, the API worked.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.