/// <summary> /// Loads the ink annotations in the PDF file. /// </summary> /// <param name="pageNumber">1-based page number.</param> /// <returns></returns> public List <InkStroke> LoadInFileInkAnnotations(int pageNumber) { List <PdfLoadedInkAnnotation> inkAnnotations = sfPdf.GetInkAnnotations(pageNumber); List <InkStroke> strokes = new List <InkStroke>(); // Get page information from SF model PdfLoadedPage sfPage = sfPdf.GetPage(pageNumber); // Get page information from MS model Windows.Data.Pdf.PdfPage msPage = msPdf.GetPage(pageNumber); // Calculate page mapping PageMapping mapping = new PageMapping(msPage, sfPage); foreach (PdfLoadedInkAnnotation annotation in inkAnnotations) { strokes.Add(mapping.InkAnnotation2InkStroke(annotation)); } return(strokes); }
/// <summary> /// Save the ink annotations into the pdf file. /// </summary> /// <param name="inkDictionary"></param> /// <returns></returns> /// <remarks> /// The page size returned from Syncfusion pdf is the media box size. /// The page size displayed to the end user is the crop box size. /// The size of the ink canvas is the same as the crop box size. /// Syncfusion uses the bottom left corner as the origin, while ink canvas uses the top left corner. /// </remarks> public async Task <bool> SaveInkingToPdf(InkingManager inkManager) { // Indicate whether any ink annotation is added to the PDF file bool fileChanged = false; // Remove ereased ink annotations foreach (KeyValuePair <int, List <InkStroke> > entry in await inkManager.ErasedStrokesDictionary()) { // The key of the dictionary is page number, which is 1-based. int pageNumber = entry.Key; PdfLoadedPage sfPage = sfPdf.GetPage(pageNumber); // Get page information from MS model Windows.Data.Pdf.PdfPage msPage = msPdf.GetPage(pageNumber); PageMapping mapping = new PageMapping(msPage, sfPage); List <PdfInkAnnotation> erasedAnnotations = new List <PdfInkAnnotation>(); // Add each ink stroke to the page foreach (InkStroke stroke in entry.Value) { PdfInkAnnotation inkAnnotation = mapping.InkStroke2InkAnnotation(stroke); erasedAnnotations.Add(inkAnnotation); } if (sfPdf.RemoveInkAnnotations(sfPage, erasedAnnotations)) { fileChanged = true; } } // Add new ink annotations foreach (KeyValuePair <int, InkStrokeContainer> entry in await inkManager.InAppInkDictionary()) { PdfLoadedPage sfPage = sfPdf.GetPage(entry.Key); // Get page information from MS model Windows.Data.Pdf.PdfPage msPage = msPdf.GetPage(entry.Key); PageMapping mapping = new PageMapping(msPage, sfPage); // Add each ink stroke to the page foreach (InkStroke stroke in entry.Value.GetStrokes()) { PdfInkAnnotation inkAnnotation = mapping.InkStroke2InkAnnotation(stroke); sfPage.Annotations.Add(inkAnnotation); fileChanged = true; } } // Save the file only if there are changes. bool inkSaved = false; if (fileChanged) { try { inkSaved = await sfPdf.SaveAsync(); // Copy and replace the actual file await sfFile.CopyAndReplaceAsync(pdfFile); } catch (Exception ex) { // Try to save the file by extracting the pages. StorageFile newFile = await backupFolder.CreateFileAsync("COPY_" + pdfFile.Name, CreationCollisionOption.GenerateUniqueName); try { await sfPdf.CopyPages(newFile); inkSaved = true; // Copy and replace the actual file await newFile.CopyAndReplaceAsync(pdfFile); } catch { App.NotifyUser(typeof(ViewerPage), "Error: \n" + ex.Message, true); } } } return(!(inkSaved ^ fileChanged)); }