欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

程序员文章站 2022-07-20 10:07:06
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100061243 参照以上将DevExpress安装并引进到工具箱。 这里使用的是VS2013所以安 ......

场景

winform控件-devexpress18下载安装注册以及在vs中使用:

https://blog.csdn.net/badao_liumang_qizhi/article/details/100061243

参照以上将devexpress安装并引进到工具箱。

这里使用的是vs2013所以安装的devexpress是14版本。

devexpress14以及注册机下载


效果

使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

 

实现

项目搭建

新建winfom程序,然后拖拽一个pdfvieerr控件。然后添加一个button按钮。

 使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

 

pdf打开与预览实现

双击进入button按钮的点击事件中

 private void simplebutton2_click(object sender, eventargs e)
        {
            //打开pdf文件,并获取文件路径
            string filepath = filedialoghelper.openpdf();
            //如果不为空
            if (!string.isnullorempty(filepath))
            {
                //加载预览  其中pdfviewer1 与控件的name相对应
                this.pdfviewer1.loaddocument(filepath);
            }
        }

 

然后新建filedialoghelper工具类,实现选择打开文件并返回路径的功能。

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.windows.forms;

namespace pdfexport
{
    class filedialoghelper
    {
        public static string openpdf() { 
        
            openfiledialog filedialog = new openfiledialog();
            filedialog.multiselect = true;
            filedialog.title = "请选择文件";
            filedialog.filter = "所有文件(*pdf*)|*.pdf*"; //设置要选择的文件的类型
            if (filedialog.showdialog() == dialogresult.ok)
            {
                return filedialog.filename;//返回文件的完整路径               
            }
            else {
                return null;
            }

        }
    }
}

 

pdf另存为实现

在窗体上再拖拽一个button,双击进入其点击事件中。

 private void simplebutton1_click_1(object sender, eventargs e)
        {

            this.pdfviewer1.savedocument(@"d:\pdf\a.pdf");
        }

 

注:

调用自带的savedocument()方法,这里传递的是保存的路径。

其还有个重载方法:

public void savedocument(stream stream);

 

效果

 使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

 

 

打印pdf实现

再拖拽一个按钮,双击进入其点击事件中。

 private void simplebutton3_click(object sender, eventargs e)
        {
            this.pdfviewer1.print();
        }

 

效果

 使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

 

源码下载