Working with PDF Layers in WPF Pdf Viewer

13 May 20224 minutes to read

The layer support in PDF viewer allows users to toggle the visibility of individual and group of layers in the PDF document to view, print, save, and export as image.

Toggling the visibility of a PDF layer

To toggle the visibility of PDF layers individually, click the eye icon associated with each layer in the layers pane.

WPF PDF Viewer Toggle the visibility of PDF layer

Toggling the visibility of the group of layers

To toggle the visibility of a group of PDF layers, click the eye icon associated with parent layer in the layers pane.

WPF PDF Viewer Toggle the visibility of the group of layer

Programmatically Toggle the Visibility of a PDF Layer

The WPF PDFViewer allows the user to toggle the visibility of a PDF Layer using its IsVisible property. When this property is set to false, the layer becomes invisible, and when this property is set to true, the layer becomes visible. The following code sample explains how to use the PdfDocumentView to retrieve the Layers collection and use the Name of a PDF layer to toggle its visibility.

//Retrieve a PDF document's layers collection using the PdfDocumentView. 
LayerCollection layers = pdfDocumentView.Layers;  

 //Get a layer by its name.              
 for (int i = 0; i < layers.Count; i++) 
 { 
      if (layers[i].Name == "Layer2") 
      { 
         //Toggle the visibility of a Layer.  
         if (layers[i].IsVisible) 
            layers[i].IsVisible = false; 
         else 
            layers[i].IsVisible = true; 
      } 
 }
'Retrieve a PDF document's layers collection using the PdfDocumentView. 
Dim layers As LayerCollection = pdfDocumentView.Layers 
'Get a layer by its name. 
For i As Integer = 0 To layers.Count - 1 
    If layers(i).Name = "Layer2" Then 
      'Toggle the visibility of a Layer. 
       If layers(i).IsVisible Then 
           layers(i).IsVisible = False 
       Else 
           layers(i).IsVisible = True 
       End If 
     End If 
Next

Similarly, it can be achieved in the PdfViewerControl by accessing the Layers collection.

NOTE

The sample project for toggling a PDF Layer is available in the GitHub link.

Disabling the layers

You can disable the display of the layers present in the PDF document by setting the EnableLayers property to false. Refer to the following code example.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    PdfLoadedDocument pdf = new PdfLoadedDocument("PdfLayers.pdf");
    pdfViewer.Load(pdf);
    pdfViewer.EnableLayers = false;

}
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    Dim pdf As New PdfLoadedDocument("PdfLayers.pdf")
    pdfViewer.Load(pdf) 
    pdfViewer.EnableLayers = false;
End Sub

You can also achieve the same in XAML using the DependencyProperty illustrated as follows.

  • XAML
  • <syncfusion:PdfViewerControl x:Name="pdfViewer" EnableLayers="False" />

    NOTE

    By default, the layer feature is enabled in PDF viewer.

    NOTE

    You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.