Create a
new WPF project
- In the design mode of the Mainwindow.xaml. Add button control and WindowsFormHost Control in the MainWindow.xaml
<Window x:Class="DemoPDFView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Height="619" Width="700">
<Grid>
<Button Name="Btn1"
Content="Button"
HorizontalAlignment="Left"
VerticalAlignment="Top" Width="75"
Margin="126,27,0,0"/>
<WindowsFormsHost Name="WFH1"
HorizontalAlignment="Left" Height="430"
VerticalAlignment="Top" Width="492"
Margin="10,69,0,0"/>
</Grid>
</Window>
- Now Add a UserControl by clicking on Add New Item in the solution Explorer
Go to the
design Mode: In the design view of UserControl
- Right Click on the tool box and Click on “Choose Items”
- You will get the following “Choose Toolbox Items” window. Go to “COM Components” Tab and check “Adobe PDF Reader” and click on “OK”
- You will now find the adobe control in the tool boxes.
Add the Adobe
PDF Reader(axAcroPDF1)control in the form.
Set the
width and height properties of the axAcroPDF1
control same as WindowsFormsHost control
That is
In the code
behind file Copy and paste the code below InitializeComponent();
Add the
line this.axAcroPDF1.LoadFile(filename);
Your
userControl class should look like this
public partial class UserControl1 : UserControl
{
public
UserControl1(string filename)
{
InitializeComponent();
this.axAcroPDF1.LoadFile(filename);
}
}
Now in the
code behind file of MainWindow.xaml add
the code for the Btn1 button event
private void
Btn1_Click(object sender, RoutedEventArgs e)
{
//
Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new
Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default
file name
dlg.DefaultExt = ".pdf"; // Default file extension
dlg.Filter = "pdf files (*.pdf) |*.pdf;"; //
Filter files by extension
// Show
open file dialog box
Nullable<bool>
result = dlg.ShowDialog();
// Process
open file dialog box results
if
(result == true)
{
// Open
document
string
filename = dlg.FileName;
var uc
= new UserControl1(filename);
this.WFH1.Child
= uc;
}
}
OUTPUT:
No comments:
Post a Comment