/// <summary> /// Outputs an asynchronous operation. /// When the operation completes, a MSPdfModel object, representing the PDF, is returned. /// </summary> /// <param name="pdfStorageFile">The PDF file</param> /// <returns></returns> public static async Task<MSPdfModel> LoadFromFile(StorageFile pdfStorageFile) { MSPdfModel msPdf = new MSPdfModel(); try { // Try to load the file without a password msPdf.PdfDoc = await PdfDocument.LoadFromFileAsync(pdfStorageFile); } catch { // Ask the user to enter password PasswordContentDialog passwordDialog = new PasswordContentDialog(); bool failedToLoad = false; if (await passwordDialog.ShowAsync() == ContentDialogResult.Primary) { // Try to load the file with a password try { msPdf.PdfDoc = await PdfDocument.LoadFromFileAsync(pdfStorageFile, passwordDialog.Password); // Store the password of the file msPdf.Password = passwordDialog.Password; msPdf.isPasswordProtected = true; } catch (Exception ex) { // Failed to load the file App.NotifyUser(typeof(ViewerPage), "Failed to open the file.\n" + ex.Message, true); failedToLoad = true; } } else { // User did not enter a password failedToLoad = true; } // Return null if failed to load the file if (failedToLoad) { return null; } } return msPdf; }
public async Task ReloadFile() { msPdf = await MSPdfModel.LoadFromFile(pdfFile); }