Navigation and View |
The users navigate and change the view with the viewer toolbar and panels. Your application can do the same from code - for example from its own toolbar, menu or keyboard shortcuts - and can hide the parts of the viewer it does not need. The first page of a document is page 1.
Member | Description |
|---|---|
Move to the first, previous, next or last page. | |
The page shown, or go to a page. Set before a document is loaded, the document opens at that page. | |
Go to a named destination - a location the document defines, like "chapter4". | |
Back and forward through the views already visited: after following a link, an outline entry, a named destination or a page number, go back to where the user was. | |
Raised when the page shown changes, whatever the reason. |
// a page box in the application toolbar private void pageTextBox_KeyDown(object sender, KeyEventArgs e) { int page; if (e.KeyCode == Keys.Enter && int.TryParse(pageTextBox.Text, out page) && page >= 1 && page <= pdfViewer.PageCount) pdfViewer.SetCurrentPage(page); } private void pdfViewer_CurrentPageChanged(object sender, EventArgs e) { pageTextBox.Text = pdfViewer.CurrentPage.ToString(); }
Find(String, Boolean, Boolean) searches the document, highlights every match and scrolls the first match from the current page on into view; the arguments choose a case sensitive search and whole words only. FindNext and FindPrevious move to the next and the previous match (after the last match, the first one). An empty text removes the highlights.
The SearchCompleted event is raised when a search is complete - after each of these calls, and after a search the user makes in the find bar of the viewer. The event data has the text, the number of matches in the document (MatchCount), the selected match (CurrentMatch, from 1) and its page (PageNumber). A search made before the document is displayed runs when it is.
pdfViewer.SearchCompleted += delegate (object sender, PdfViewerSearchEventArgs e) { findResultLabel.Text = e.MatchCount == 0 ? "Not found" : string.Format("{0} of {1} (page {2})", e.CurrentMatch, e.MatchCount, e.PageNumber); }; pdfViewer.Find("invoice", false, true); // not case sensitive, whole words pdfViewer.FindNext();
PageFitMode fits the page in the viewer: Fit (the whole page), FitH (the page width) or FitV (the page height). ZoomPercentage sets an exact zoom (100 is 100%); reading it gives the zoom of the viewer, which changes when the user zooms or when a fit mode is applied.
pdfViewer.PageFitMode = PageFitMode.FitH; // fit the page width pdfViewer.ZoomPercentage = 150; // 150%
SetViewRect(Single, Single, Single, Single) zooms on a rectangle of the current page, and SetZoomScroll(Single, Single, Single) sets a zoom and scrolls a point of the page to the top left corner (positions in points, 1/72 inch, from the top left corner of the page).
Pages shown | |
|---|---|
SinglePage | One page at a time. |
OneColumn | All pages in one continuous column (the default). |
TwoColumnLeft | Two pages side by side, odd pages on the left. |
TwoColumnRight | Two pages side by side, the first page alone on the right - like a book with its cover. |
NotSet | The document decides (its viewer preferences). |
pdfViewer.PageLayoutMode = PageLayoutMode.TwoColumnLeft;
DocumentDisplayMode opens the panel next to the document with the page thumbnails (Thumbs), the outline (Bookmarks) or the attached files (Attachments), or closes it (None). With NotSet the document decides. ShowNavigationPanel shows or hides the button that opens the panel.
pdfViewer.DocumentDisplayMode = DocumentDisplayMode.Bookmarks;
ShowToolbarMode shows or hides the toolbar of the viewer - hide it when your application has its own toolbar.
ShowScrollbars shows or hides the scrollbars; the document can still be scrolled with the mouse wheel and the keyboard.
SetCurrentHighlight(Int32, Int32, Int32, Int32) highlights a rectangle of the current page (left, top, width and height in points) - for example a search result or a field to check. A rectangle with no size removes the highlight.
RemoveProductLogo removes the logo shown while no document is loaded.
ViewerTheme sets the colors of the viewer - its toolbar, panels and background: Light, Dark, or System (as set in Windows, the default). The pages keep their own colors.
Single toolbar buttons can be hidden (their keyboard shortcuts do nothing then):
Property | Toolbar button | Default |
|---|---|---|
Open a PDF file of the computer in the viewer (tools menu, Ctrl+O) | false | |
Open and close the navigation panel | true | |
The find bar (Ctrl+F) | true | |
Previous and next page, the page number box, first and last page | true | |
Zoom out, zoom in and the zoom list | true | |
Print (Ctrl+P) | true | |
Save a copy of the document (Ctrl+S) | true | |
Presentation mode (full screen) | true | |
Document properties | true |
Hiding a button changes only the toolbar: the methods of the control (printing, finding, navigating) work either way. Use CanPrint to deny printing (see Security and viewer preferences).
A layout or a panel set by your application (PageLayoutMode, DocumentDisplayMode) takes precedence over the viewer preferences of the document. Set them back to NotSet to let the document decide. View settings stay in effect for the next documents you load; a new document opens at its first page unless you set CurrentPage before loading it.