Back to Devexpress

Annotations in the PDF Viewer

windowsforms-119479-controls-and-libraries-pdf-viewer-additional-content-annotations.md

latest14.1 KB
Original Source

Annotations in the PDF Viewer

  • Nov 27, 2024
  • 7 minutes to read

The PDF Viewer allows you to add interactive elements ( annotations ) such as notes, highlights, or text boxes directly to a PDF document. You can create and delete annotations, edit their content or flatten them, add related comments and reviews. All these capabilities allow you to add context or feedback directly to PDF files and thus simplify collaboration during document reviews.

Note

The PDF Viewer ignores the No Rotate and No Zoom annotation flags.

Annotation Types

Text Markup Annotations

Highlight

Highlight text and leave comments about it.

Strikethrough

Strike out text and leave comments about it.

Underline

Underline text and leave comments about it.

Text Annotations

Sticky Notes

Add a comment annotation to a specific location in a PDF document.

Free Text Annotations

Insert a text box at a specific location in a PDF document.

Callouts

Insert a combination of a text box and a pointer line to a specific location in a PDF document.

Create an Annotation

User Interface

Use commands on the Comment Ribbon page to create text markup annotations, sticky notes, free text, and callouts.

Text Markup

Enable one of the selection tools in the Text group and select the text that should have an annotation:

You can also add text markup annotations for the selected text from the context menu:

Text Annotation (Sticky Note, Free Text, Callout)

Click Sticky Note , Free Text , or Callout in the Ribbon and select a location in a PDF document to place the element:

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:

MethodDescription
PdfViewer.HighlightSelectedTextHighlights the selected text.
PdfViewer.StrikethroughSelectedTextStrikes through the selected text.
PdfViewer.UnderlineSelectedTextUnderlines the selected text.
PdfViewer.AddStickyNoteAdds a sticky note at the specified position.

The code below highlights the selected text and adds a sticky note.

csharp
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);
vb
Imports System.Drawing
Imports System.Windows.Forms

' Load a document
pdfViewer.LoadDocument("..\..\Demo.pdf")

' Select a text to highlight
pdfViewer.FindText("PDF Viewer")

' Highlight selected text
pdfViewer.HighlightSelectedText()

pdfViewer.AddStickyNote(New PdfDocumentPosition(1, New PdfPoint(29, 568)), "Comment", Color.Crimson)

You can also use PDF Facade API to access and manage annotations. This API implements additional functionality, but is only available as part of DevExpress Office File API * or Universal subscription. Navigate to the following section for additional information and a code sample: Use PDF Facade API to Manage Annotations.

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 following code sample specifies the default sticky note and text highlight settings:

csharp
PdfMarkupToolsSettings markupToolsSettings = pdfViewer.MarkupToolsSettings;
markupToolsSettings.StickyNoteIconName = PdfTextAnnotationIconName.Help;
markupToolsSettings.StickyNoteColor = Color.SkyBlue;

markupToolsSettings.TextHighlightColor = Color.IndianRed;
markupToolsSettings.TextHighlightDefaultSubject = "Spelling";
vb
Dim markupToolsSettings As PdfMarkupToolsSettings = pdfViewer.MarkupToolsSettings
markupToolsSettings.StickyNoteIconName = PdfTextAnnotationIconName.Help
markupToolsSettings.StickyNoteColor = Color.SkyBlue

markupToolsSettings.TextHighlightColor = Color.IndianRed
markupToolsSettings.TextHighlightDefaultSubject = "Spelling"

Edit Annotations

Right-click the annotation in the document (or in the Comments navigation page) and select Properties :

Use the invoked Annotation Properties dialog to edit the annotation (text and border colors, opacity, text, and so on). Available properties depend on the annotation type:

Click Set as Default to change default settings for all new annotations of this type.

Users can move Sticky Notes, Free Text, and Callouts:

Users can move pointer lines associated with Callout annotations:

Users can resize Free Text and Callout annotations:

Tip

When the Comments panel is opened, double-click a sticky note to edit its associated comment.

Add Replies and Reviews

The Comments navigation pane (Alt + C) shows all document annotations. You can use it to add replies and set review statuses for 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 choose an option. Select None to retract your response:

Remove Annotations

Right-click an annotation in the Comments navigation pane and select Delete in the context menu to remove the annotation:

Users can also click an annotation in a document and press Delete or right-click and choose Delete from the context menu:

Annotation Events

The table below lists the PdfViewer events that occur in response to various annotation actions:

AnnotationCreatingFires before the annotation is created.AnnotationCreatedOccurs after an annotation is created.AnnotationGotFocusOccurs when an annotation receives input focus.AnnotationLostFocusOccurs when an annotation loses input focus.AnnotationChangingFires before the annotation is changed.AnnotationChangedFires after an annotation has been changed.AnnotationDeletingFires before an annotation is deleted from a page.AnnotationDeletedOccurs after the annotation is deleted.ReplyCreatedOccurs after the annotation reply is created in the Comments Panel.ReplyChangedOccurs after the annotation reply is changed in the Comments Panel.ReplyDeletedOccurs after the annotation reply is deleted in the Comments Panel.

Use PDF Facade API to Manage Annotations

Important

Functionality in this section is available as part of the DevExpress Office File API or Universal Subscription. Refer to the DevExpress Subscription page for pricing information.

The PDF Facade API contains additional annotation types you can manage in code: Link, Rubber Stamp, Caret, File Attachment, Ink, and others. You can use the PdfDocumentFacade class to organize these annotations: create, delete, edit their content, flatten, add related comments and reviews.

The following code sample creates a file attachment and a rubber stamp annotation:

csharp
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";
vb
pdfViewer.LoadDocument("Document.pdf")

' Access the first page properties
Dim documentFacade As PdfDocumentFacade = pdfViewer.GetDocumentFacade()
Dim pageFacade As PdfPageFacade = documentFacade.Pages(0)

' Define a rubber stamp rectangle
Dim rubberStampRectangle As New PdfRectangle(491, 727, 591, 773)

' Create a "Draft" rubber stamp annotation
Dim rubberStamp As PdfRubberStampAnnotationFacade =
     pageFacade.AddRubberStampAnnotation(rubberStampRectangle, PdfRubberStampAnnotationIconName.Draft)
rubberStamp.Author = "Jesse Faden"
rubberStamp.Contents = "Made in PDF Document API"

' Define a file attachment area 
Dim attachmentPoint As New PdfPoint(29, 568)

' Specify attachment data
Dim attachment As New PdfFileAttachment() With {.CreationDate = Date.Now, .Description = "This is my attached file", .FileName = "MyAttach.txt", .Data = File.ReadAllBytes("..\..\Attachment.txt")}

' Create a file attachment annotation
Dim pdfFileAttachment As PdfFileAttachmentAnnotationFacade =
     pageFacade.AddFileAttachmentAnnotation(attachmentPoint, attachment, PdfFileAttachmentAnnotationIconName.PaperClip)
pdfFileAttachment.Author = "Nancy Skywalker"
pdfFileAttachment.Contents = "Additional Content"

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.

See Also

Organize Annotations in PDF Document API