diff --git a/wpf/Pdf-Viewer/Working-with-Annotations/Working-with-Annotations-Programmatically.md b/wpf/Pdf-Viewer/Working-with-Annotations/Working-with-Annotations-Programmatically.md index dd08a438e5..0cff613932 100644 --- a/wpf/Pdf-Viewer/Working-with-Annotations/Working-with-Annotations-Programmatically.md +++ b/wpf/Pdf-Viewer/Working-with-Annotations/Working-with-Annotations-Programmatically.md @@ -8,6 +8,53 @@ documentation: ug --- # Working with annotations programmatically +## Add an annotation + +The PDF Viewer allows users to add annotations programmatically without user interaction. Annotations can be added to the PDF document using the [PdfViewerControl’s loadedDocument](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.PdfViewer.PdfViewerControl.html#Syncfusion_Windows_PdfViewer_PdfViewerControl_LoadedDocument) property in the code behind. + +The following code snippet explains how to add an ink annotation at runtime to the PDF document. + +{% tabs %} +{% highlight C# %} + +//Adding ink annotation at runtime to the PDF document. +private void AddAnnotation() +{ + //Get the instance of the loadedDocument from the PdfViewerControl. + PdfLoadedDocument loadedDocument = pdfViewer.LoadedDocument; + + //Specify the ink points + List inkPoints = new List { 40, 300, 60, 100, 40, 50, 40, 300 }; + //Specify the bounds of an annotation + RectangleF rectangle = new RectangleF(0, 0, 300, 400); + //Create a new ink annotation + PdfInkAnnotation inkAnnotation = new PdfInkAnnotation(rectangle, inkPoints); + + //Add the ink annotation to the desired page of the PdfLoadedDocument property instance. + loadedDocument.Pages[0].Annotations.Add(inkAnnotation); +} + +{% endhighlight %} +{% highlight VB %} + +'Adding ink annotation at runtime to the PDF document. +private void AddAnnotation() +{ + 'Get the instance of the loadedDocument from the PdfViewerControl. + Dim loadedDocument As PdfLoadedDocument = pdfViewer.LoadedDocument + + 'Specify the ink points + Dim inkPoints As List(Of Single) = New List(Of Single) From {40, 300, 60, 100, 40, 50, 40, 300} + 'Specify the bounds of an annotation + Dim rectangle As RectangleF = New RectangleF(0, 0, 300, 400) + 'Create a new ink annotation + Dim inkAnnotation As PdfInkAnnotation = New PdfInkAnnotation(rectangle, inkPoints) + + 'Add the ink annotation to the desired page of the PdfLoadedDocument property instance. + loadedDocument.Pages(0).Annotations.Add(inkAnnotation) +} +{% endhighlight %} +{% endtabs %} ## Select an annotation @@ -154,7 +201,9 @@ private void PdfViewer_DocumentLoaded(object sender, EventArgs args) {% endtabs %} ## Modify an annotation +The PDF Viewer allows users to modify annotations programmatically without user interaction in the following ways. +### Modify an annotation using annotation changed event settings Annotation’s properties can be modified programmatically through `Settings` in respective annotation changed event. The following code snippet explains how to modify the selected ink annotation’s properties. Similarly, we can implement for all other annotations. @@ -177,6 +226,48 @@ private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedE {% endhighlight %} {% endtabs %} +### Modify an annotation using loadedDocument + +Annotations can be modified in the PDF document using the [PdfViewerControl’s loadedDocument](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.PdfViewer.PdfViewerControl.html#Syncfusion_Windows_PdfViewer_PdfViewerControl_LoadedDocument) property in the code behind. + +The following code snippet demonstrates how to modify an ink annotation at runtime. + +{% tabs %} +{% highlight C# %} + +//Modify the ink annotation in the PDF Viewer runtime. +private void ModifyAnnotation() +{ + //Get the instance of the loadedDocument from the PdfViewerControl. + PdfLoadedDocument loadedDocument = pdfViewer.LoadedDocument; + //Check whether the annotation is PdfInkAnnotation. + if(loadedDocument.Pages[0].Annotations[0] is PdfInkAnnotation) + { + PdfInkAnnotation inkAnnotation = loadedDocument.Pages[0].Annotations[0] as PdfInkAnnotation; + //Modify the color of the ink annotation + inkAnnotation.Color = new PdfColor(System.Drawing.Color.Blue); + } +} + +{% endhighlight %} +{% highlight VB %} + +'Modify the ink annotation in the PDF Viewer runtime. +private void ModifyAnnotation() +{ + 'Get the instance of the loadedDocument from the PdfViewerControl. + Dim loadedDocument As PdfLoadedDocument = pdfViewer.LoadedDocument + Dim inkAnnotation As PdfInkAnnotation = Nothing + 'Check whether the annotation is PdfInkAnnotation. + If TypeOf loadedDocument.Pages(0).Annotations(0) Is PdfInkAnnotation Then + inkAnnotation = TryCast(loadedDocument.Pages(0).Annotations(0), PdfInkAnnotation) + 'Modify the color of the ink annotation. + inkAnnotation.Color = New PdfColor(System.Drawing.Color.Blue) + End If +} +{% endhighlight %} +{% endtabs %} + ## Hide an annotation PDF Viewer allows the user to hide the annotation programmatically without user interaction. This functionality returns true if any annotation is found and hidden, otherwise it returns false. The annotation [Name](https://help.syncfusion.com/cr/wpf/Syncfusion.Windows.PdfViewer.AnnotationChangedEventArgs.html#Syncfusion_Windows_PdfViewer_AnnotationChangedEventArgs_Name) property is used to identify the annotation. Refer this [UG link](https://help.syncfusion.com/wpf/pdf-viewer/working-with-annotations/select-and-modify-annotations#how-to-get-and-set-name-of-an-annotation) to get and set an annotation Name property.