Articles in this section

How to get the coordinates of the points of ink annotations in a PDF loaded in PdfViewer

The coordinate points by which an ink annotation is constructed can be retrieved by saving the PDF, creating a PdfLoadedDocument instance from the resultant stream, and obtaining the points from its annotation collection.

 

To start with, add two rows in MainPage.xaml of the project. The first row should contain a ToggleButton and a normal Button. The ToggleButton enables or disables the ink annotation mode, and the normal Button is used to retrieve the ink points. In the second row, add the PdfViewer control and load a PDF from the code-behind.

 

XAML

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="60" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
 
        <ToggleButton x:Name="inkToggleButton" Grid.Row="0" Content="Toggle Ink Mode" Width="150" Height="40" VerticalAlignment="Center" HorizontalAlignment="Left" Checked="inkToggleButton_Checked" Unchecked="inkToggleButton_Unchecked" Margin="300 0 0 0" />
        <Button x:Name="inkPointsButton" Grid.Row="0" Content="Get Points" Width="100" Height="40" VerticalAlignment="Center" HorizontalAlignment="Right" Click="inkPointsButton_Click" Margin="0 0 300 0" />
        <sfPdfViewer:SfPdfViewerControl x:Name="pdfViewer" Grid.Row="1" />
 
</Grid>

 

C#

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
      Stream stream = typeof(MainPage).GetTypeInfo().Assembly.GetManifestResourceStream("InkPointsDemo.Assets.Barcode.pdf");
       pdfViewer.LoadDocument(stream);
 }
 
 private void inkToggleButton_Checked(object sender, RoutedEventArgs e)
 {
      //Enable ink annotation mode
      pdfViewer.InkAnnotationCommand.Execute(true);
 }
 
 private void inkToggleButton_Unchecked(object sender, RoutedEventArgs e)
 {
      //Disable ink annotation mode
      pdfViewer.InkAnnotationCommand.Execute(false);
 }

Add a method named GetInkPointCollection that returns a dictionary containing the ink point collection of pages, indexed by the page number.

Create a PdfLoadedDocument instance from the stream passed and iterate over each page of the PdfLoadedDocument. For each page, obtain the annotation collection and iterate over it again to check whether it is ink. If yes, add the point collection of the ink to a list. At the end of the iteration for each page, add the list of point collections to the dictionary with the current page number as an index. Then return the dictionary.

C#

private Dictionary<int, List<List<List<float>>>> GetInkPointCollection(Stream stream)
{
      PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(stream);
 
      // Stores the collection of all ink points indexed by the page numbers
      Dictionary<int, List<List<List<float>>>> listOfAllInkPointCollection = new Dictionary<int, List<List<List<float>>>>();
      for (int i = 0; i < pdfLoadedDocument.PageCount; i++)
      {
            // Stores the collection of all ink points present in the current page
            List<List<List<float>>> pointCollectionOfSinglePage = new List<List<List<float>>>();
              PdfLoadedAnnotationCollection collection = pdfLoadedDocument.Pages[i].Annotations;
              for (int j = 0; j < collection.Count; j++)
              {
                   PdfLoadedInkAnnotation pdfLoadedInkAnnotation = collection[j] as PdfLoadedInkAnnotation;
 
                    // Check whether this annotation is really an ink 
                    if (pdfLoadedInkAnnotation != null)
                    {
                        // If yes, add it to the ink points collection 
                        pointCollectionOfSinglePage.Add(pdfLoadedInkAnnotation.InkPointsCollection);
                    }
                }
 
                listOfAllInkPointCollection.Add(i, pointCollectionOfSinglePage);
      }
 
            return listOfAllInkPointCollection;
}

 

In the “inkPointsButton” clicked event method, save the currently loaded PDF to a Stream using the PdfViewerControl’s Save method and call the above method “GetInkPointCollection” to retrieve the point collection of all ink annotations added on all pages.

C#

private void inkPointsButton_Click(object sender, RoutedEventArgs e)
{
    // Save the PDF to a stream
    Stream stream = pdfViewer.Save();
 
    // Get the points of all ink annotations in all pages
    Dictionary<int, List<List<List<float>>>> listOfAllInkPointCollection = GetInkPointCollection(stream);
}

 

Sample link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/InkPointsDemo-949677758.zip

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied