How to: Customize Popup Menus in the PDF Viewer
- 3 minutes to read
The PDF Viewer UI includes the following pre-defined popup menus:
- Context menus for page content, bookmarks, thumbnails, and other elements.
- Static menus available in various toolbars and panels.
Handle the PdfViewer.PopupMenuShowing event to customize popup menus. Check the PopupMenuKind argument to determine the menu type. Use the ItemLinks argument to access the command collection.
Add a PopUp Menu Item
This example demonstrates how to add a custom item to the popup menu.
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraPdfViewer;
using System.Windows.Forms;
//...
void pdfViewer1_PopupMenuShowing(object sender, PdfPopupMenuShowingEventArgs e) {
// Create a bar button item.
BarButtonItem browseBarButton = new BarButtonItem();
browseBarButton.Caption = "Custom Item";
// Insert the bar button item
// and start a new group.
e.ItemLinks.Add(browseBarButton, true);
// Handle the bar button click event.
browseBarButton.ItemClick += browseBarButton_ItemClick;
}
void browseBarButton_ItemClick(object sender, ItemClickEventArgs e) {
MessageBox.Show("ItemClick event fires");
}
Modify a PopUp Menu Item
Access the menu item collection and call its GetPdfViewerBarItemLink method to obtain an item by its command ID. Use the returned BarItemLink object to change the menu item parameters.
The code sample below shows how to change a caption of a built-in menu item:
using DevExpress.XtraPdfViewer;
using DevExpress.XtraPdfViewer.Commands;
using DevExpress.XtraPdfViewer.Extensions;
//...
void pdfViewer1_PopupMenuShowing(object sender, PdfPopupMenuShowingEventArgs e)
{
// Retrieve a Hand Tool item
// in the Page Content menu.
if (e.PopupMenuKind == PdfPopupMenuKind.PageContent)
{
var handToolItem =
e.ItemLinks.GetPdfViewerBarItemLink(PdfViewerCommandId.HandTool);
// Change the item caption.
handToolItem.Caption = "Custom Caption";
}
}
Remove a PopUp Menu Item
Access the menu item collection and call its GetPdfViewerBarItemLink method to obtain an item by its command ID. Call the Remove(T) method and pass the obtain menu item as a parameter to remove this item.
This example shows how to remove specific items from the page content popup menu.
using DevExpress.XtraPdfViewer;
using DevExpress.XtraPdfViewer.Commands;
using DevExpress.XtraPdfViewer.Extensions;
//...
void pdfViewer1_PopupMenuShowing(object sender, PdfPopupMenuShowingEventArgs e)
{
// Remove Rotate Clockwise and Rotate Counterclockwise items
// from the Page Content popup menu.
if (e.PopupMenuKind == PdfPopupMenuKind.PageContent)
{
var rotateClockwiseItem =
e.ItemLinks.GetPdfViewerBarItemLink(PdfViewerCommandId.RotatePageClockwise);
e.ItemLinks.Remove(rotateClockwiseItem);
var rotateCounterclockwiseItem =
e.ItemLinks.GetPdfViewerBarItemLink(PdfViewerCommandId.RotatePageCounterclockwise);
e.ItemLinks.Remove(rotateCounterclockwiseItem);
}
}
Hide the Pop-Up Menu
Clear all items from the item collection to hide a popup menu.
The code sample below hides the pop-up menu for a bookmark tree:
using DevExpress.XtraPdfViewer;
using DevExpress.XtraPdfViewer.Commands;
using DevExpress.XtraPdfViewer.Extensions;
//...
void pdfViewer1_PopupMenuShowing(object sender, PdfPopupMenuShowingEventArgs e)
{
// Hide the popup menu for the bookmark tree.
if (e.PopupMenuKind == PdfPopupMenuKind.BookmarkTree)
{
e.ItemLinks.Clear();
}
}