使用DevExpress的PdfViewer實現PDF打開、預覽、另存為、打印(附源碼下載)


場景

Winform控件-DevExpress18下載安裝注冊以及在VS中使用:

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

參照以上將DevExpress安裝並引進到工具箱。

這里使用的是VS2013所以安裝的DevExpress是14版本。

DevExpress14以及注冊機下載


https://download.csdn.net/download/badao_liumang_qizhi/11608734

效果

 

實現

項目搭建

新建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();
        }

 

效果

 

 

源碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11617199


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM