狠狠色丁香婷婷综合尤物/久久精品综合一区二区三区/中国有色金属学报/国产日韩欧美在线观看 - 国产一区二区三区四区五区tv

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

C#使用開源免費(fèi)庫 PdfiumViewer 實(shí)現(xiàn)PDF打印與查看

admin
2024年7月1日 16:14 本文熱度 3211

前言

PDF是一種常用的文件格式,實(shí)現(xiàn)其打印、查看操作是較為常見的需求。例如打印PDF格式的快遞面單、發(fā)票等。如何通過編寫C#代碼實(shí)現(xiàn)打印、查看需求,可以使用一些三方C#組件庫。如Spire.PDF for .NET、PdfiumViewer等,本文介紹使用PdfiumViewer實(shí)現(xiàn)方式。

PdfiumViewer

PdfiumViewer 是基于 Pdfium 庫的.NET PDF查看器組件。可以使我們輕松地在應(yīng)用程序中嵌入PDF文檔,對(duì)文檔的查看與打印。(或其他基于 Pdfium 庫.NET庫).NET Framework》PdfiumViewer;.NET 6》PdfiumViewer.Core

1、使用和附錄

github地址:
https://github.com/pvginkel/PdfiumViewer
https://github.com/TimChen44/PdfiumViewer.Core
通過包管理器安裝下面幾個(gè)包:
PdfiumViewer
PdfiumViewer.Native.x86_64.v8-xfa:64位的Pdfium。
PdfiumViewer.Native.x86.v8-xfa:32位的Pdfium。

2、使用的類或控件

類名描述
PdfDocument用于呈現(xiàn) PDF 文檔的類。
PdfRenderer用于呈現(xiàn) PdfDocument 的控件類。
PdfViewer托管 PdfRenderer 的控件。

示例代碼

1、打印示例

/// <summary>/// 靜默打印 /// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void buttonPrint_Click(object sender, EventArgs e){    try    {        string printerName = "Microsoft Print to PDF";        string documentName = Guid.NewGuid().ToString("N");        short printCopies = 1;        //         string fileName = string.Format(@"D:\Temp\{0}""20240512193805.pdf");        // 判斷文件是否存在        if (!File.Exists(fileName))        {            MessageBox.Show(string.Format("【{0}】文件不存在!",fileName));            return;        }        // 讀取文件內(nèi)容        byte[] fileBuffer = File.ReadAllBytes(fileName);        // 將字節(jié)內(nèi)容轉(zhuǎn)為流        MemoryStream memoryStream = new MemoryStream(fileBuffer);        //         fileBuffer = null;        // 開始組件裝載文件        using (PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(memoryStream))        {            // 創(chuàng)建文檔輸出發(fā)送到打印機(jī)對(duì)像            PrintDocument printDocument = pdfDocument.CreatePrintDocument();            // 將文檔打印到打印機(jī)            printDocument.PrintController = new StandardPrintController();            if (!string.IsNullOrEmpty(printerName))            {                // 輸出的目標(biāo)打印機(jī)                printDocument.PrinterSettings.PrinterName = printerName;            }            // 打印文檔名稱            printDocument.DocumentName = documentName;            // 打印份數(shù)            printDocument.PrinterSettings.Copies = printCopies;            // 開始發(fā)送打印            printDocument.Print();        }    }    catch(Exception exception)    {        MessageBox.Show(exception.Message);    }}

2、查看示例

/// <summary>/// 打開閱讀/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){    try    {        // PDF 文件        string fileName = string.Format(@"D:\Temp\{0}""20240512193805.pdf");        // 判斷文件是否存在        if (!File.Exists(fileName))        {            MessageBox.Show(string.Format("【{0}】文件不存在!", fileName));            return;        }        // 開始組件裝載文件        PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(fileName);         this.pdfViewer.Document = pdfDocument;        this.pdfViewer.Show();    }    catch (Exception exception)    {        MessageBox.Show(exception.Message);    }}

3、其它示例

/// <summary>/// 其它/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void buttonOther_Click(object sender, EventArgs e){    try    {        // PDF文件        string fileName = string.Format(@"D:\Temp\{0}""20240512193805.pdf");        // 判斷文件是否存在        if (!File.Exists(fileName))        {            MessageBox.Show(string.Format("【{0}】文件不存在!", fileName));            return;        }        // 開始組件裝載文件        using (PdfiumViewer.PdfDocument pdfDocument = PdfiumViewer.PdfDocument.Load(fileName))        {            // 獲取文檔總頁數(shù)            int pageCount = pdfDocument.PageCount;            // 獲取文件信息            PdfInformation pdfInformation = pdfDocument.GetInformation();            // 在文件中搜索指定內(nèi)容            var finder = pdfDocument.Search("文本",false,false);        }    }    catch (Exception exception)    {        MessageBox.Show(exception.Message);    }}

小結(jié)

以上是PdfiumViewer組件簡(jiǎn)單介紹,通過實(shí)現(xiàn)打印與查看示例了解其使用方式。對(duì).NET 6及以上版本使用PdfiumViewer.Core,有興趣的可以試試。


該文章在 2024/7/2 8:35:40 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲(chǔ)管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved