Could not create windows.data.pdf.pdfdocument instance for a particular PDF document in UWP Platform

Ashok Kumar 1 Reputation point
2020-10-19T04:50:03.11+00:00

We have been using the PdfDocument API to access the PDF pages and we will convert the pdf pages into Image, to view the page content in our control. However, while creating the PdfDocument instance, we are facing exception and we could see the same behavior in Microsoft Edge as they have used same API to display the PDF pages.

Please find the below code snippet,

private async void Button_Click(object sender, RoutedEventArgs e)
{
var picker = new FileOpenPicker();
Stream fileStream;
picker.FileTypeFilter.Add(".pdf");
var file = await picker.PickSingleFileAsync();
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
fileStream = stream.AsStreamForRead();
fileStream.Position = 0;
IAsyncOperation<global::Windows.Data.Pdf.PdfDocument> result;
result = global::Windows.Data.Pdf.PdfDocument.LoadFromStreamAsync(stream);
result.GetResults();
}

33187-sample.pdf

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,851 Reputation points
    2020-10-19T11:02:54.953+00:00

    hello @Ashok Kumar , Welcome to Microsoft Q&A

    When I open above attached pdf file with PdfDocument api, it throw "Exception from HRESULT: 0x80048040", it looks above pdf file is not standard pdf file. For this scenario, we suggest you use Microsoft Edge pdf viewer to open pdf file then print pdf as Microsoft PDF standard document.

    Steps:

    Open pdf file with Microsoft Edge

    Press Print button show print dialog

    Select Microsoft Print to PDF option within Printer combobox

    Click Print to save as new pdf file.

    You will open the new pdf file with PdfDocument api successfully.

    Thanks
    Nico Zhu


    If the response is helpful, please click "Accept Answer" and upvote it.
    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