最近又一个需求,需要使用C#代码打开PDF文件,展示在UI上,很多第三方组件都是收费版本的,我只需要展示在UI上供查看而已,不需要特殊的功能,花钱买一个组件是不值得的,经济允许的情况,当然应该支持这些开发者的。
下面介绍使用AxPDFViewerLib.dll 组件实现PDF文件浏览,打印,虽然是评估版本,也不影响正常的浏览,打印,另存等功能。
实现打开一个PDF文件浏览如下:
1. 添加组件 AxPDFViewerLib,PDFViewerLib.dll
2. 添加UserControl.cs
在自定义用户控件中添加axPDFViewer1 控件
讲控件铺满整个控件,Dock 属性选择Fill。
添加代码,完整代码如下:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public Boolean bReadonly = false;
private void axPDFViewer1_OnDocumentOpened(object sender, AxPDFViewerLib._DPDFViewerEvents_OnDocumentOpenedEvent e)
{
if (bReadonly)
{
axPDFViewer1.SetReadOnly();
}
else
{
}
}
public void OpenEditable()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "PDF File Formats (*.pdf)|*.pdf|All Files (*.*) | *.* ||";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
axPDFViewer1.Toolbars = true;
axPDFViewer1.LoadFile(openFileDialog.FileName);
bReadonly = false;
}
}
public void OpenReadOnly()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "PDF File Formats (*.pdf)|*.pdf|All Files (*.*) | *.* ||";
openFileDialog.RestoreDirectory = true;
openFileDialog.FilterIndex = 1;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
axPDFViewer1.Toolbars = false;
axPDFViewer1.LoadFile(openFileDialog.FileName);
bReadonly = true;
}
}
public void SaveAs()
{
axPDFViewer1.SaveCopyAs();
}
public void Print()
{
axPDFViewer1.PrintWithDialog();
}
public void ClosePDF()
{
axPDFViewer1.Clear();
}
}
3. 主UI设计,后台代码设计
完整界面设计:
<Window x:Class="pdfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:pdfApp"
Title="MainWindow" Height="350" Width="725" Closed="Window_Closed">
<Window.Resources>
<Style TargetType="Button">
<Setter Property="Margin" Value="3"/>
</Style>
</Window.Resources>
<DockPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom">
<Button x:Name="Open" Click="Open_Click" Content="Open in Editable Mode..." Width="172" />
<Button x:Name="ReadOnly" Click="ReadOnly_Click" Content="Open in ReadOnly Mode..." Width="180" />
<Button x:Name="SaveAs" Click="SaveAs_Click" Content="Save As..." />
<Button x:Name="print" Click="print_Click" Content="Print" Width="66" />
<Button x:Name="Close" Click="Close_Click" Content="Close" Width="48" />
</StackPanel>
<WindowsFormsHost DockPanel.Dock="Top" Margin="2">
<local:UserControl1 x:Name="_host"/>
</WindowsFormsHost>
</DockPanel>
</Window>
完整后台代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace pdfApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Open_Click(object sender, RoutedEventArgs e)
{
_host.OpenEditable();
}
private void ReadOnly_Click(object sender, RoutedEventArgs e)
{
_host.OpenReadOnly();
}
private void SaveAs_Click(object sender, RoutedEventArgs e)
{
_host.SaveAs();
}
private void print_Click(object sender, RoutedEventArgs e)
{
_host.Print();
}
private void Close_Click(object sender, RoutedEventArgs e)
{
_host.ClosePDF();
}
private void Window_Closed(object sender, EventArgs e)
{
_host.ClosePDF();
}
}
}
网友评论