Working with PdfViewerControl in Windows Forms PDF Viewer (PdfViewerControl)

Essential PdfViewerControl can display and print PDF files and export the pages as raster images and meta files.

Properties, Methods, and Events Tables

Properties

Property Description Type Data Type
EnableNotificationBar Enables the display of the Notification bar on setting it to true. N/A bool
PageCount Returns the number of pages as viewed in the PdfViewerControl. N/A Integer
ShowToolbar Displays the document toolbar when set to true. N/A bool

Methods

Method Description Parameters Type Return Type
Load loads the PDF to the viewer. Overloads: (string filePath) (string filePath, string password)(PdfLoadedDocument doc)(Stream file) N/A Void
Unload Unloads the loaded PDF. - N/A Void
Dispose Unloads the document and releases the resources used by the component. - N/A Void
ExportAsImage Converts the page to a raster image. Overloads:(int pageIndex)(int startIndex, int endIndex) N/A Bitmap
FindText Searches for the occurrence of given input text in the PDF document and returns all the occurrences and its location in all pages of the PDF document. Overloads:(string text, out Dictionary<int, List <System.Drawing.RectangleF>> matchTextPosition) N/A bool
GoToPageAtIndex Navigates to the specified page (int index) N/A Void
ZoomTo Magnifies the document displayed to the specified percentage. (int percentage) N/A Void

Events

Event Description Event Type
DocumentLoaded triggered after the PDF is successfully loaded. N/A N/A
HyperLinkMouseHover triggered when the mouse pointer is placed over the URL. N/A N/A
HyperLinkMouseClicked triggered when the URL in the PDF document is clicked. N/A N/A

Viewing PDF Files

A PDF can be loaded into the PdfViewerControl either through the open file button available in the toolbar or through the Load method. It also requests passwords to open encrypted documents.

//Initialize PdfViewerControl.

PdfViewerControl pdfViewerControl1 = new PdfViewerControl();

//Load the PDF.

pdfViewerControl1.Load("Sample.pdf");
'Initialize PdfViewerControl.

Private pdfViewerControl1 As New PdfViewerControl()

'Load the PDF.

pdfViewerControl1.Load("Sample.pdf")

You can load an encrypted document by using the overload in the Load method.

//Initialize PdfViewerControl.

PdfViewerControl pdfViewerControl1 = new PdfViewerControl();

//Load the PDF.

pdfViewerControl1.Load("Sample.pdf", "password");
'Initialize PdfViewerControl.

Private pdfViewerControl1 As New PdfViewerControl()

'Load the PDF.

pdfViewerControl1.Load("Sample.pdf", "password")

Exporting PDF

Exporting pages of PDF document as raster images

Essential PdfViewerControl allows selected pages to be exported as raster images. Exporting can be done using the ExportAsImage method. This option helps to convert a PDF into an image.

Bitmap image = pdfViewerControl1.ExportAsImage(0);

// Save the image.

image.Save("Sample.png", ImageFormat.Png);
Dim image As Bitmap = pdfViewerControl1.ExportAsImage(0)

'Save the image.

image.Save("Sample.png", ImageFormat.Png)

You can also specify the page range instead of converting each page.

Bitmap[] image = pdfViewerControl1.ExportAsImage(0, 3);
Dim image() As Bitmap = pdfViewerControl1.ExportAsImage(0, 3)

Exporting pages of PDF document as Vector Images

Exporting pages of PDF document as vector images can be done using the ExportAsMetafile method. The following code sample demonstrates how a PDF document can be exported as a Metafile.

Metafile image = pdfViewerControl1.ExportAsMetafile(0);

// Save the image

image.Save("Sample.emf", ImageFormat.Emf);
Dim image As Metafile = pdfViewerControl1.ExportAsMetafile(0)

' Save the image

image.Save("Sample.emf", ImageFormat.Emf)

You can also specify the page range instead of converting each page individually.

Metafile[] image = pdfViewerControl1.ExportAsMetafile(0, 3);
Dim image() As Metafile = pdfViewerControl1.ExportAsMetafile(0, 3)

Text selection

In PDF, text can be selected by clicking the mouse left button and dragging the mouse pointer over the text.

Detecting the completion of text selection

When the text selection is completed, the TextSelectionCompleted event will be raised. The selected text can be retrieved as string from the args parameter of the event handler.

private void PdfViewer_TextSelectionCompleted(object sender, TextSelectionCompletedEventArgs args)
{
    //Get the whole selected text
    string selectedText = args.SelectedText;

    //Get the selected text and its rectangular bounds for each page separately if the selection is made on multiple pages
    Dictionary<int, Dictionary<string, Rectangle>> selectedTextInformation = args.SelectedTextInformation;
}

Copying the selected text

The selected text can be copied by clicking Copy from the context menu, which appears when clicking right mouse button after the text is selected.

Copy Text

The selected text can also be copied using the keyboard shortcut Ctrl + C.

Annotations

Essential PdfViewerControl provides support for URI annotations in the PDF document, which enables the URI available in the PDF document to be opened in the browser just by clicking it. This also supports a few events which are listed in the events table.