PDFViewer Quick Start for WPF

  • Demo
  • App.xaml
  • App.xaml.cs
  • App.xaml.vb
  • MainWindow.xaml
  • MainWindow.xaml.cs
  • MainWindow.xaml.vb

The sample demonstrates how to get started with Spire.PDFViewer for WPF.

 

<Application x:Class="OpenPDF_WPF.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
             
    <Application.Resources>
        
    </Application.Resources>
</Application>

using System.Windows;

namespace OpenPDF_WPF
{
    /// 
    /// Interaction logic for App.xaml
    /// 
    public partial class App : Application
    {
    }
}

Namespace OpenPDF_WPF
	''' 
	''' Interaction logic for App.xaml
	''' 
	Partial Public Class App
		Inherits Application
	End Class
End Namespace

<Window x:Class="OpenPDF_WPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Spire.PDFViewer for .NET - Demo – Open PDF via PdfViewerDocument Component" Height="608" Width="651" Loaded="Window_Loaded" xmlns:my="clr-namespace:Spire.PdfViewer.Wpf;assembly=Spire.PdfViewer.Wpf">
    <Grid>
      <ToolBar Height="43" HorizontalAlignment="Left" Name="toolBar1" VerticalAlignment="Top" Width="74">
        <Button Name="btnOpen" Click="btnOpen_Click">
         <Image Source="Resources/Open.png" Width="42"></Image>
        </Button>
      </ToolBar>
      <my:PdfDocumentViewer Height="515" HorizontalAlignment="Left" Margin="0,42,0,0" Name="pdfDocumentViewer1" VerticalAlignment="Top" Width="625" /d>
    </Grid>
</Window>    

using System;
using System.Windows;

using Microsoft.Win32;
namespace OpenPDF_WPF
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.pdfDocumentViewer1.LoadFromFile(@"..\..\Suns.pdf");
            this.pdfDocumentViewer1.ZoomTo(60);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            //open a pdf document
            OpenFileDialog dialog = new OpenFileDialog()
            {
                Filter = "Pdf document(*.Pdf)|*.pdf",
                Title = "Open Pdf Document",
                Multiselect = false
            };
            bool? result = dialog.ShowDialog();
            if (result.Value)
            {
                try
                {
                    //Load pdf document from file.
                    this.pdfDocumentViewer1.LoadFromFile(dialog.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
        }
    }
}

Imports Microsoft.Win32
Namespace OpenPDF_WPF
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Partial Public Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
			Me.pdfDocumentViewer1.LoadFromFile("..\..\Suns.pdf")
			Me.pdfDocumentViewer1.ZoomTo(60)
		End Sub

		Private Sub btnOpen_Click(ByVal sender As Object, ByVal e As EventArgs)
			'open a pdf document
			Dim dialog As New OpenFileDialog() With {.Filter = "Pdf document(*.Pdf)|*.pdf", .Title = "Open Pdf Document", .Multiselect = False}
			Dim result? As Boolean = dialog.ShowDialog()
			If result.Value Then
				Try
					'Load pdf document from file.
					Me.pdfDocumentViewer1.LoadFromFile(dialog.FileName)
				Catch ex As Exception
					MessageBox.Show(ex.Message, "Error!", MessageBoxButton.OK, MessageBoxImage.Error)
				End Try
			End If
		End Sub
	End Class
End Namespace