C# 代码实现浏览PDF文件

这篇博客介绍了如何在C#中使用AxPDFViewerLib.dll组件免费实现PDF文件的查看、打印功能。通过添加组件,设置UserControl,并在主UI界面设计按钮,实现了打开编辑模式和只读模式,以及保存和打印操作。
摘要由CSDN通过智能技术生成

最近又一个需求,需要使用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();

        }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

flysh05

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值