PDF: Edit PDF annotations of PDF document in WinForms
In This Topic
The
PdfAnnotationTool class allows to edit annotations of PDF page in WinForms image viewer. The class can edit the PDF annotations in two modes: 'Markup' and 'Edit' (
PdfAnnotationTool.InteractionMode).
In 'Markup' mode the
PdfAnnotationTool class:
- Allows to view PDF annotations, change markup annotations and fill PDF interactive fields.
- Raises and processes trigger events (Activate, MouseDown, Keystoke, etc) of annotations and interactive fields.
- Can copy and paste markup annotations.
- Uses PdfMarkupAnnotationView.MarkupTransformer as an interaction controller for markup annotation and PdfAnnotationView.ViewTransformer as an interaction controller for non-markup annotation and interactive field.
In 'Edit' mode the
PdfAnnotationTool class:
- Allows to view and edit all PDF annotations and PDF interactive fields.
- Does NOT raise and process tigger events (Activate, MouseDown, Keystoke, etc) of annotations and interactive fields.
- Can copy and paste any annotation and interactive field.
- Can select multiple annotations and interactive fields, selected annotations can be moved as a group.
- Shows all (not hidden and hidden) annotations and interactive fields.
- Can use special highlighting of fields (PdfAnnotationTool.EditorModeHighlight).
- Uses PdfAnnotationView.EditTransformer as an interaction controller.
The process of visual constructing of annotation is run using
PdfAnnotationTool.AddAndBuildAnnotation method.
Here is C#/VB.NET code that demonstrates how to create a visual tool for displaying and interaction with annotations and fields of PDF interactive form:
/// <summary>
/// Creates the PDF annotation tool with JavaScript support.
/// </summary>
/// <param name="viewer">The image viewer.</param>
public static Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool CreatePdfAnnotationToolWithJavaScriptSupport(Vintasoft.Imaging.UI.ImageViewer viewer)
{
// create PDF JavaScript application
Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp jsApp =
new Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp();
// add PDF documents, which are associated with images in viewer,
// to the document set of PDF JavaScript application
jsApp.RegisterImageViewer(viewer);
// create PdfJavaScriptActionExecutor for PDF JavaScript application
jsApp.ActionExecutor = new Vintasoft.Imaging.Pdf.JavaScript.PdfJavaScriptActionExecutor(
jsApp, new Vintasoft.Imaging.Pdf.JavaScriptApi.PdfJsConsole());
// create Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool annotationTool =
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool(jsApp, true);
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup;
// create an application action executor
Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor applicationActionExecutor =
new Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor();
// add executor for PdfJavaScriptAction to the application action executor
applicationActionExecutor.Items.Add(jsApp.ActionExecutor);
// add executor for PdfGotoAction to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.UI.PdfGotoActionExecutor(viewer));
// add executor for PdfNamedAction to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.UI.PdfNamedActionExecutor(viewer));
// add executor for PdfResetFormAction to the application action executor
applicationActionExecutor.Items.Add(
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolResetFormActionExecutor(annotationTool));
// add executor for PdfAnnotationHideAction to the application action executor
applicationActionExecutor.Items.Add(
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolAnnotationHideActionExecutor(annotationTool));
// add executor for PdfUriAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfUriActionExecutor());
// add executor for PdfLaunchAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfLaunchActionExecutor());
// add executor for PdfSubmitFormAction to the application action executor,
// executor is located in WpfPdfDemosCommonCode
//applicationActionExecutor.Items.Add(new PdfSubmitActionExecutor(viewer));
// add the default executor (for PdfResetFormAction and PdfAnnotationHideAction)
// to the application action executor
applicationActionExecutor.Items.Add(new Vintasoft.Imaging.Pdf.PdfActionExecutor());
// set the application action executor as action executor of PDF annotation tool
annotationTool.ActionExecutor = applicationActionExecutor;
// create a document-level actions executor
Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor documentLevelActionsExecutor =
new Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor(jsApp);
// set the application action executor as action executor of the document-level actions executor
documentLevelActionsExecutor.ActionExecutor = applicationActionExecutor;
return annotationTool;
}
''' <summary>
''' Creates the PDF annotation tool with JavaScript support.
''' </summary>
''' <param name="viewer">The image viewer.</param>
Public Shared Function CreatePdfAnnotationToolWithJavaScriptSupport(viewer As Vintasoft.Imaging.UI.ImageViewer) As Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
' create PDF JavaScript application
Dim jsApp As New Vintasoft.Imaging.Pdf.UI.JavaScript.WinFormsPdfJsApp()
' add PDF documents, which are associated with images in viewer,
' to the document set of PDF JavaScript application
jsApp.RegisterImageViewer(viewer)
' create PdfJavaScriptActionExecutor for PDF JavaScript application
jsApp.ActionExecutor = New Vintasoft.Imaging.Pdf.JavaScript.PdfJavaScriptActionExecutor(jsApp, New Vintasoft.Imaging.Pdf.JavaScriptApi.PdfJsConsole())
' create Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool
Dim annotationTool As New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool(jsApp, True)
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup
' create an application action executor
Dim applicationActionExecutor As New Vintasoft.Imaging.Pdf.PdfActionCompositeExecutor()
' add executor for PdfJavaScriptAction to the application action executor
applicationActionExecutor.Items.Add(jsApp.ActionExecutor)
' add executor for PdfGotoAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.PdfGotoActionExecutor(viewer))
' add executor for PdfNamedAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.PdfNamedActionExecutor(viewer))
' add executor for PdfResetFormAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolResetFormActionExecutor(annotationTool))
' add executor for PdfAnnotationHideAction to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationToolAnnotationHideActionExecutor(annotationTool))
' add executor for PdfUriAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfUriActionExecutor());
' add executor for PdfLaunchAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfLaunchActionExecutor());
' add executor for PdfSubmitFormAction to the application action executor,
' executor is located in WpfPdfDemosCommonCode
'applicationActionExecutor.Items.Add(new PdfSubmitActionExecutor(viewer));
' add the default executor (for PdfResetFormAction and PdfAnnotationHideAction)
' to the application action executor
applicationActionExecutor.Items.Add(New Vintasoft.Imaging.Pdf.PdfActionExecutor())
' set the application action executor as action executor of PDF annotation tool
annotationTool.ActionExecutor = applicationActionExecutor
' create a document-level actions executor
Dim documentLevelActionsExecutor As New Vintasoft.Imaging.Pdf.PdfDocumentLevelActionsExecutor(jsApp)
' set the application action executor as action executor of the document-level actions executor
documentLevelActionsExecutor.ActionExecutor = applicationActionExecutor
Return annotationTool
End Function
Here is C#/VB.NET code that demonstrates how to start the buildig of PDF annotation:
/// <summary>
/// Adds and builds a polyline annotation.
/// </summary>
/// <param name="annotationTool">The PDF annotation tool, which will build an annotation.</param>
public static void AddAndBuildPolylineeAnnotation(Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool annotationTool)
{
Vintasoft.Imaging.Pdf.Tree.PdfPage focusedPage = annotationTool.FocusedPage;
if (focusedPage == null)
throw new System.InvalidOperationException();
// create a PDF annotation
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation annotation =
new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation(focusedPage);
annotation.BorderWidth = 5;
annotation.Color = System.Drawing.Color.Red;
annotation.InteriorColor = System.Drawing.Color.Green;
annotation.StartPointLineEndingStyle = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationLineEndingStyle.ClosedArrow;
// if tool mode does not allow to build annotation
if (annotationTool.InteractionMode == Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.None ||
annotationTool.InteractionMode == Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.View)
// change the tool mode
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup;
// add and build the annotation
annotationTool.AddAndBuildAnnotation(annotation);
}
''' <summary>
''' Adds and builds a polyline annotation.
''' </summary>
''' <param name="annotationTool">The PDF annotation tool, which will build an annotation.</param>
Public Shared Sub AddAndBuildPolylineeAnnotation(annotationTool As Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool)
Dim focusedPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = annotationTool.FocusedPage
If focusedPage Is Nothing Then
Throw New System.InvalidOperationException()
End If
' create a PDF annotation
Dim annotation As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation(focusedPage)
annotation.BorderWidth = 5
annotation.Color = System.Drawing.Color.Red
annotation.InteriorColor = System.Drawing.Color.Green
annotation.StartPointLineEndingStyle = Vintasoft.Imaging.Pdf.Tree.Annotations.PdfAnnotationLineEndingStyle.ClosedArrow
' if tool mode does not allow to build annotation
If annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.None OrElse annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.View Then
' change the tool mode
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup
End If
' add and build the annotation
annotationTool.AddAndBuildAnnotation(annotation)
End Sub
Here is C#/VB.NET code that demonstrates how to override the building process of PDF annotation and start the building of PDF annotation:
/// <summary>
/// Adds and builds a freehand polyline annotation.
/// </summary>
/// <param name="annotationTool">The PDF annotation tool, which will build an annotation.</param>
public static void AddAndBuildFreehandPolylineAnnotation(
Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool annotationTool)
{
Vintasoft.Imaging.Pdf.Tree.PdfPage focusedPage = annotationTool.FocusedPage;
if (focusedPage == null)
throw new System.InvalidOperationException();
// create the PDF annotation
Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation annotation =
new Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation(focusedPage);
annotation.BorderWidth = 5;
annotation.Color = System.Drawing.Color.Red;
// if tool mode does not allow to build annotation
if (annotationTool.InteractionMode == Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.None ||
annotationTool.InteractionMode == Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.View)
// change the tool mode
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup;
// create the annotation view because view parameters must be changed
Vintasoft.Imaging.Pdf.UI.Annotations.PdfPolygonalAnnotationView view =
new Vintasoft.Imaging.Pdf.UI.Annotations.PdfPolylineAnnotationView(annotation, annotationTool.FocusedJsDoc);
// set the builder for annotation view
view.Builder = new Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectFreehandBuilder(view, 2, 0.1f);
// add and build annotation
annotationTool.AddAndBuildAnnotation(view);
}
''' <summary>
''' Adds and builds a freehand polyline annotation.
''' </summary>
''' <param name="annotationTool">The PDF annotation tool, which will build an annotation.</param>
Public Shared Sub AddAndBuildFreehandPolylineAnnotation(annotationTool As Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationTool)
Dim focusedPage As Vintasoft.Imaging.Pdf.Tree.PdfPage = annotationTool.FocusedPage
If focusedPage Is Nothing Then
Throw New System.InvalidOperationException()
End If
' create the PDF annotation
Dim annotation As New Vintasoft.Imaging.Pdf.Tree.Annotations.PdfPolylineAnnotation(focusedPage)
annotation.BorderWidth = 5
annotation.Color = System.Drawing.Color.Red
' if tool mode does not allow to build annotation
If annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.None OrElse annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.View Then
' change the tool mode
annotationTool.InteractionMode = Vintasoft.Imaging.Pdf.UI.Annotations.PdfAnnotationInteractionMode.Markup
End If
' create the annotation view because view parameters must be changed
Dim view As Vintasoft.Imaging.Pdf.UI.Annotations.PdfPolygonalAnnotationView = New Vintasoft.Imaging.Pdf.UI.Annotations.PdfPolylineAnnotationView(annotation, annotationTool.FocusedJsDoc)
' set the builder for annotation view
view.Builder = New Vintasoft.Imaging.UI.VisualTools.UserInteraction.PointBasedObjectFreehandBuilder(view, 2, 0.1F)
' add and build annotation
annotationTool.AddAndBuildAnnotation(view)
End Sub