当前位置: 移动技术网 > IT编程>开发语言>c# > 使用DevExpress的PdfViewer实现PDF打开、预览、另存为、打印(附源码下载)

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

2019年08月27日  | 移动技术网IT编程  | 我要评论

场景

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

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

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

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

devexpress14以及注册机下载


效果

 

实现

项目搭建

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

 

 

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);

 

效果

 

 

 

打印pdf实现

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

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

 

效果

 

 

源码下载


 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网