Application crashes while creating windows.data.pdf.pdfdocument for particular PDF document

Ashok Kumar Viswanathan 71 Reputation points
2022-05-06T04:55:10.173+00:00

We have been using the PdfDocument ([windows.data.pdf.pdfdocument][1]) API to access the PDF pages and we will convert the pdf pages into Image, to view the page content in our application. We are facing exception while creating windows.data.pdf.pdfdocument for particular PDF document.

Kindly check the issue and let us know,

https://drive.google.com/file/d/19n_eLBm1huV3KKEFDgHZhIg_rsoBy4NF/view?usp=sharing

Code Snippet to replicate:

private async void BtnLoadFile_Click(object sender, RoutedEventArgs e)
{
FileOpenPicker picker = new FileOpenPicker();
picker.FileTypeFilter.Add(".pdf");
var pdfFile = await picker.PickSingleFileAsync();
if (pdfFile != null)
{
var result = pdfFile.OpenAsync(FileAccessMode.Read);
IRandomAccessStream randomStream = result.AsTask().Result;
if (randomStream != null)
{
IAsyncOperation<global::Windows.Data.Pdf.PdfDocument> result1;
result1 = global::Windows.Data.Pdf.PdfDocument.LoadFromStreamAsync(randomStream);
result1.AsTask().Wait();
Windows.Data.Pdf.PdfDocument _pdfDocument = result1.GetResults();
result = null;
}
}
}

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 31,826 Reputation points Microsoft Vendor
    2022-05-06T06:57:01.86+00:00

    Hello,

    Welcome to Microsoft Q&A!

    After testing, we could confirm that this issue is related to that specific PDF file you shared. When testing other standard PDF files, your code works correctly. The behavior you mentioned only happens when handling that specific PDF file. Then, for test purposes, I opened your PDF file in a browser and printed it as a new standard PDF file. Your code could open the newly generated PDF file correctly. So, this behavior is related to the PDF file that you are using. And the solution is that you might need to convert the target PDF file into a standard PDF file first via other ways like using a browser.

    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.

    0 comments No comments