Example #1
0
        /// <summary>
        /// Outputs an asynchronous operation.
        /// When the operation completes, a initialized PdfModelSF object, representing the PDF, is returned.
        /// </summary>
        /// <param name="pdfStorageFile">The PDF file</param>
        /// <returns>An initialized PdfModelSF object.</returns>
        public static async Task <PdfModelSF> LoadFromFile(StorageFile pdfStorageFile, string password = null)
        {
            PdfModelSF pdfModel = new PdfModelSF(pdfStorageFile);

            pdfModel.PdfDoc = new PdfLoadedDocument();
            // Load PDF from file
            try
            {
                if (password == null)
                {
                    await pdfModel.PdfDoc.OpenAsync(pdfStorageFile);
                }
                else
                {
                    await pdfModel.PdfDoc.OpenAsync(pdfStorageFile, password);
                }
            }
            catch (Exception ex)
            {
                // Failed to load the file
                AppEventSource.Log.Debug("PDFModelSF: " + ex.Message);
                // Notify the user?
                App.NotifyUser(typeof(ViewerPage), "Failed to open the file.\n" + ex.Message, true);
                pdfModel = null;
            }
            return(pdfModel);
        }
Example #2
0
        /// <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;
        }