Annotations in PDF Viewer
- 5 minutes to read
The PDF Viewer allows you to create, edit, or remove text annotations (sticky notes) and text markup annotations (text highlights). You can also add comments and reviews associated with annotations.
Note
The PDF Viewer ignores the No Rotate and No Zoom annotation flags.
Create an Annotation
User Interface
Use commands on the Comment ribbon page to create text markup annotations and sticky notes.
Enable a selection tool in the Text group and select the text that should have an annotation. Click Sticky Note to place the sticky note in a document.
You can also add text markup annotations from the context menu:
Tip
Use the PdfViewer.CursorMode property to change the viewer’s cursor mode in code.
Code
The following API allows you to add annotations in code:
Method | Description |
---|---|
PdfViewer.HighlightSelectedText | Highlights the selected text. |
PdfViewer.StrikethroughSelectedText | Strikes through the selected text. |
PdfViewer.UnderlineSelectedText | Underlines the selected text. |
PdfViewer.AddStickyNote | Adds a sticky note at the specified position. |
The code below highlights the selected text and adds a sticky note.
using System.Drawing;
using System.Windows.Forms;
// Load a document
pdfViewer.LoadDocument("..\\..\\Demo.pdf");
// Select a text to highlight
pdfViewer.FindText("PDF Viewer");
// Highlight selected text
pdfViewer.HighlightSelectedText();
// Add a sticky note
pdfViewer.AddStickyNote(new PdfDocumentPosition(1, new PdfPoint(29, 568)),
"Comment", Color.Crimson);
Change Markup Tool Settings
User Interface
You can change the default settings for annotation tools. Click the drop-down arrow in the corresponding annotation tool and specify the color and opacity.
Code
To access the markup tool settings in code, use the PdfViewer.MarkupToolsSettings property.
The code sample below shows how to specify the default sticky note and text highlight settings:
PdfMarkupToolsSettings markupToolsSettings = pdfViewer.MarkupToolsSettings;
markupToolsSettings.StickyNoteIconName = PdfTextAnnotationIconName.Help;
markupToolsSettings.StickyNoteColor = Color.SkyBlue;
markupToolsSettings.TextHighlightColor = Color.IndianRed;
markupToolsSettings.TextHighlightDefaultSubject = "Spelling";
Edit Annotations
Use the Annotation Properties dialog to change the annotation properties. Right-click the annotation in the document or in the Comments navigation page and select Properties… to invoke this dialog.
Click Set as Default to use the new settings as the default settings for all annotations.
Add Replies and Reviews
The Comments navigation pane shows all document annotations. You can use it to add replies and set review statuses for the annotations.
Select an annotation on the pane, enter text in the invoked editor and click Reply to add the reply to the annotation.
To specify the review status, right-click an annotation, select Set Status, and set the status. Select None to remove the review status.
Remove Annotations
Right-click an annotation in document or in the Comments navigation pane and select Delete in the context menu to remove the annotation. You can also select an annotation and press the Delete key.
Annotation Events
The table below lists events that occur in response to various annotation actions:
Event | Description |
---|---|
AnnotationCreating | Fires when the user starts to create an annotation or after the corresponding method call. |
AnnotationCreated | Raises after the annotation is created. |
AnnotationGotFocus | Occurs when the annotation receives the input focus. |
AnnotationLostFocus | Fires when the annotation loses the input focus. |
AnnotationChanging | Fires when the user starts to change an annotation. |
AnnotationChanged | Occurs after the user changed the annotation using the Annotation Properties dialog. |
AnnotationDeleting | Occurs before the annotation is deleted. |
AnnotationDeleted | Occurs after the annotation is deleted. |
ReplyCreated | Occurs after the annotation reply is created in the Comments Panel. |
ReplyChanged | Occurs after the annotation reply is changed in the Comments Panel. |
ReplyDeleted | Occurs after the annotation reply is deleted in the Comments Panel. |
Use PDF Facade API to Manage Annotations
You can use the PdfDocumentFacade class to organize annotations: create, delete, edit their content, flatten, add related comments and reviews.
Important
The Universal Subscription or an additional Office File API Subscription is required to use this example in production code. Refer to the DevExpress Subscription page for pricing information.
The PdfViewerExtensions.GetDocumentFacade method retrieves the PdfDocumentFacade class object that allows you to change the PDF document without access to its inner structure.
The PdfPageFacade.Annotations property retrieves all page annotation properties. Use the PdfDocumentFacade.Pages property to access the PdfPageFacade class.
Refer to the following article for more information on how to use PDF Facade API to manage annotations: How to: Use PDF Facade API to Manage Annotations
The code sample below creates a file attachment and a rubber stamp annotation:
pdfViewer.LoadDocument("Document.pdf");
// Access the first page properties
PdfDocumentFacade documentFacade = pdfViewer.GetDocumentFacade();
PdfPageFacade pageFacade = documentFacade.Pages[0];
// Define a rubber stamp rectangle
PdfRectangle rubberStampRectangle = new PdfRectangle(491, 727, 591, 773);
// Create a "Draft" rubber stamp annotation
PdfRubberStampAnnotationFacade rubberStamp =
pageFacade.AddRubberStampAnnotation(rubberStampRectangle,
PdfRubberStampAnnotationIconName.Draft);
rubberStamp.Author = "Jesse Faden";
rubberStamp.Contents = "Made in PDF Document API";
// Define a file attachment area
PdfPoint attachmentPoint = new PdfPoint(29, 568);
// Specify attachment data
PdfFileAttachment attachment = new PdfFileAttachment()
{
CreationDate = DateTime.Now,
Description = "This is my attached file",
FileName = "MyAttach.txt",
Data = File.ReadAllBytes("..\\..\\Attachment.txt")
};
// Create a file attachment annotation
PdfFileAttachmentAnnotationFacade pdfFileAttachment =
pageFacade.AddFileAttachmentAnnotation(attachmentPoint, attachment,
PdfFileAttachmentAnnotationIconName.PaperClip);
pdfFileAttachment.Author = "Nancy Skywalker";
pdfFileAttachment.Contents = "Additional Content";