Disable toolbar items

20 Jan 20257 minutes to read

To remove the default toolbar completely, use the PdfDocumentView control instead of PdfViewerControl as described in the section.

However, an individual toolbar item can also be removed from the default toolbar of the PDF Viewer using the toolbar template.

NOTE

When disabling the toolbar items using the toolbar template, the toolbar template should be accessed either in the PdfViewerControl’s Loaded event or Windows loaded event as the toolbar items will be initialized when the PdfViewer is loaded. Accessing the toolbar items using its template before the PdfViewer is loaded will result in a null reference exception

The following code sample explains disabling the text search tool from the default toolbar.

public MainWindow()
{
    InitializeComponent();
    // Load the specified PDF file.
    pdfViewer.Load("Document.pdf");
    // Attach an event handler to the Loaded event of the PdfViewerControl.
    pdfViewer.Loaded += pdfViewer_Loaded;
}

private void pdfViewer_Loaded(object sender, RoutedEventArgs e)
{
    //Get the instance of the toolbar using its template name.
    DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;
    //Get the instance of the open file button using its template name.
    Button textSearchButton = (Button)toolbar.Template.FindName("PART_ButtonTextSearch", toolbar);
    //Set the visibility of the button to collapsed.
    textSearchButton.Visibility = System.Windows.Visibility.Collapsed;
}

NOTE

Apply the changes of hiding toolbar items, only after when the application window is loaded.
The sample project for disabling toolbar item is available in the GitHub.

Similarly, other toolbar items also can be disabled. The following table lists the template names of the rest of the toolbar items along with their respective types in the order they appear in the toolbar.

Toolbar item Template name Type
File tool PART_FileToggleButton System.Windows.Controls.Primitives.ToggleButton
Navigation tools separator Part_NavigationToolsSeparator System.Windows.Shapes.Rectangle
First page tool PART_ButtonGoToFirstPage System.Windows.Controls.Button
Previous page tool PART_ButtonGoToPreviousPage System.Windows.Controls.Button
Current page number tool PART_TextCurrentPageIndex System.Windows.Controls.TextBox
Page separator tool PART_PageSeparator System.Windows.Controls.TextBlock
Page count tool PART_LabelTotalPageCount System.Windows.Controls.TextBlock
Next page tool PART_ButtonGoToNextPage System.Windows.Controls.Button
Last page tool PART_ButtonGoToLastPage System.Windows.Controls.Button
Current zoom level tool PART_ComboBoxCurrentZoomLevel System.Windows.Controls.ComboBox
Zoom in tool PART_ButtonZoomIn System.Windows.Controls.Button
Zoom out tool PART_ButtonZoomOut System.Windows.Controls.Button
Annotation tools separator PART_AnnotationToolsSeparator System.Windows.Shapes.Rectangle
Sticky note tool PART_StickyNote System.Windows.Controls.Primitives.ToggleButton
Ink tool PART_Ink System.Windows.Controls.Primitives.ToggleButton
Ink eraser tool PART_InkEraser System.Windows.Controls.Primitives.ToggleButton
Text markup tool PART_TextMarkup System.Windows.Controls.Primitives.ToggleButton
Shapes tool PART_Shapes System.Windows.Controls.Primitives.ToggleButton
Fill tool PART_Fill System.Windows.Controls.Primitives.ToggleButton
Add textbox tool PART_FreeText System.Windows.Controls.Primitives.ToggleButton
Text properties tool PART_ButtonTextBoxFont System.Windows.Controls.Button
Stamp tool PART_Stamp System.Windows.Controls.Primitives.ToggleButton
Handwritten signature tool PART_ButtonSignature System.Windows.Controls.Button
Select tool PART_SelectTool System.Windows.Controls.Primitives.ToggleButton
Hand tool PART_HandTool System.Windows.Controls.Primitives.ToggleButton
Separator between the cursor tools and text search button Part_CursorTools System.Windows.Shapes.Rectangle
Text search tool PART_ButtonTextSearch System.Windows.Controls.Button

NOTE

From the v18.4.0.x onwards, the file menu items such as Open, Save, Save As, and Print are not directly present in the toolbar and they are present in the context menu of the File tools toggle button.
From the v21.1.0.x onwards, the PDFViewer toolbar design is changed for a better user interface and visual.

Due to the changes in the toolbar design, there were some changes for a few tool templates. They are listed as follows.

Before v21.1.0.x (Old Toolbar)

Toolbar item Template name Type
Zoom tools separator Part_ZoomToolsSeparator_0 System.Windows.Shapes.Rectangle
Fit width tool PART_ButtonFitWidth System.Windows.Controls.Button
Fit page tool PART_ButtonFitPage System.Windows.Controls.Button
Zoom tools separator PART_ZoomToolsSeparator_1 System.Windows.Shapes.Rectangle
Separator between annotation and cursor tools PART_AnnotationsSeparator System.Windows.Shapes.Rectangle
Marquee zoom tool PART_MarqueeZoom System.Windows.Controls.Primitives.ToggleButton

After v21.1.0.x (Current Toolbar)

Toolbar item Template name Type
Zoom tools separator PART_ZoomToolsSeparator System.Windows.Shapes.Rectangle
Cursor tools separator PART_CursorToolsSeparator System.Windows.Shapes.Rectangle
Fit width tool PART_FitWidth System.Windows.Controls.ComboBoxItem
Fit page tool PART_FitPage System.Windows.Controls.ComboBoxItem
Marquee zoom tool PART_MarqueeZoom System.Windows.Controls.ComboBoxItem
Separates zoom mode and zoom percentage PART_ComboBoxZoomModeSeperator System.Windows.Controls.Separator
Separates zoom mode and marquee zoom PART_ComboBoxMarqueeZoomSeperator System.Windows.Controls.Separator

The following code sample explains disabling the combo box item, and separators present inside the combo box.

private void HideComboBoxTools(object sender, RoutedEventArgs e)
{
	// Get the instance of the toolbar using its template name.
    DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;
    // Get the Instance of the zoom level combo box using its template name.
    ComboBox comboBox = (ComboBox)toolbar.Template.FindName("PART_ComboBoxCurrentZoomLevel", toolbar); 
    // Iterates the combo box items present inside the combo box and disables them.
    for (int i = 0; i < comboBox.Items.Count; i++)
    {
        if (comboBox.Items[i] is ComboBoxItem)
        {
            ComboBoxItem item = comboBox.Items[i] as ComboBoxItem;
			if (item.Name == "PART_FitPage")
            {
                item.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (item.Name == "PART_FitWidth")
            {
                item.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (item.Name == "PART_MarqueeZoom")
            {
                item.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        if (comboBox.Items[i] is Separator)
        {
            Separator separator = comboBox.Items[i] as Separator;
            if (separator.Name == "PART_ComboBoxZoomModeSeperator")
            {
                separator.Visibility = System.Windows.Visibility.Collapsed;
            }
            if (separator.Name == "PART_ComboBoxMarqueeZoomSeperator")
            {
                separator.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
    }
}

Disable the file menu item

The PDF Viewer allows users to disable the individual menu items present under the file menu of the toolbar. The following example code explains how to disable the open menu item from the file menu.

private void HideOpenTool(object sender, RoutedEventArgs e)
{
	//Get the instance of the toolbar using its template name.
	DocumentToolbar toolbar = pdfViewer.Template.FindName("PART_Toolbar", pdfViewer) as DocumentToolbar;
	//Get the instance of the file menu button using its template name.
	ToggleButton FileButton = (ToggleButton)toolbar.Template.FindName("PART_FileToggleButton", toolbar);
	//Get the instance of the file menu button context menu and the item collection.
	ContextMenu FileContextMenu = FileButton.ContextMenu;
	foreach (MenuItem FileMenuItem in FileContextMenu.Items)
	{
		//Get the instance of the open menu item using its template name and disable its visibility.
		if (FileMenuItem.Name == "PART_OpenMenuItem")
			FileMenuItem.Visibility = System.Windows.Visibility.Collapsed;
	}
}

Similarly, other file menu items can be disabled. The following table lists the template names along with their respective types in the order they appear in the context menu.

Toolbar item Template name Type
Open tool PART_OpenMenuItem System.Windows.Controls.MenuItem
Save tool PART_SaveMenuItem System.Windows.Controls.MenuItem
SaveAs tool PART_SaveAsMenuItem System.Windows.Controls.MenuItem
Print tool PART_PrintMenuItem System.Windows.Controls.MenuItem

NOTE

The present UI design is subject to change, based on the inclusion of new features, enhancements, and user convenience.