/// <summary> /// Renders the first page of a PDF document from a stream. /// </summary> /// <param name="stream"></param> /// <param name="renderWidth"></param> /// <returns></returns> public static async Task <BitmapImage> RenderFirstPageFromStream(IRandomAccessStream stream, uint renderWidth) { PdfModelMS msPdf = new PdfModelMS(); msPdf.PdfDoc = await PdfDocument.LoadFromStreamAsync(stream); return(await msPdf.RenderPageImage(1, renderWidth)); }
/// <summary> /// Renders a page as image in the memory. /// </summary> /// <param name="pageNumber">1-based page number.</param> /// <param name="renderWidth">Width of the image.</param> /// <returns></returns> public async Task <BitmapImage> RenderPageImage(int pageNumber, uint renderWidth) { // Load exiting annotations if (sfPdf.AnnotationCount(pageNumber) == 0) { return(await msPdf.RenderPageImage(pageNumber, renderWidth)); } else { MemoryStream stream = sfPdf.ExtractPageWithoutInking(pageNumber); return(await PdfModelMS.RenderFirstPageFromStream(stream.AsRandomAccessStream(), renderWidth)); } }
/// <summary> /// Initializes the components of a new PDFModel instance. /// </summary> /// <param name="dataFolder">The folder storing the in-app user data.</param> /// <returns></returns> private async Task InitializeComponents(StorageFolder dataFolder) { // Create backup folder backupFolder = await dataFolder.CreateFolderAsync(BACKUP_FOLDER, CreationCollisionOption.OpenIfExists); // Delete existing backup copies foreach (StorageFile file in await backupFolder.GetFilesAsync()) { try { await file.DeleteAsync(); } catch { } } // Create a backup copy backupFile = await pdfFile.CopyAsync(backupFolder, pdfFile.Name, NameCollisionOption.GenerateUniqueName); sfFile = await pdfFile.CopyAsync(backupFolder, "SF_" + pdfFile.Name, NameCollisionOption.GenerateUniqueName); // Load the file to Microsoft PDF document model // The Microsoft model is used to render the PDF pages. msPdf = await PdfModelMS.LoadFromFile(backupFile); // Return null if failed to load the file to Microsoft model if (msPdf == null) { return; } // Load the file to Syncfusion PDF document model // The Syncfusion model is used to save ink annotations. if (msPdf.IsPasswordProtected) { sfPdf = await PdfModelSF.LoadFromFile(sfFile, msPdf.Password); } else { sfPdf = await PdfModelSF.LoadFromFile(sfFile); } // Return null if failed to load the file to Syncfusion model if (sfPdf == null) { return; } ScaleRatio = sfPdf.GetPage(1).Size.Width / msPdf.GetPage(1).Dimensions.MediaBox.Width; }
/// <summary> /// Outputs an asynchronous operation. /// When the operation completes, a PdfModelMS object, representing the PDF, is returned. /// Use this method to initialize a MSPdfModel instance. /// </summary> /// <param name="pdfStorageFile">The PDF file</param> /// <returns></returns> public static async Task <PdfModelMS> LoadFromFile(StorageFile pdfStorageFile) { PdfModelMS msPdf = new PdfModelMS(); 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); } } msPdf.PageCount = (int)msPdf.PdfDoc.PageCount; return(msPdf); }
public async Task ReloadFile() { msPdf = await PdfModelMS.LoadFromFile(pdfFile); }