在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
便攜式文檔格式 (PDF),有時被稱為 ISO 32000,由Adobe於1992年創建,是一種文件格式,可以在不依賴於操作系統、硬體或應用軟體的情況下呈現具有文字格式和圖形的文檔。 PDF 文件是平面文件的說明,其具有定義的布局,包括顯示所需的所有文字、字體、點陣圖像、向量圖形和其他數據。 它建立在 PostScript 之上。
透過從 .NET C# 程式碼將PDF發送到打印機以自動化打印過程,節省人工勞力,確保 PDF 文件創建的一致性,並允許您將打印功能整合到應用程式中。它提供對打印過程的精確控制。
在本文中,我們將在 C# Windows 應用程式中列印 PDF 檔案。
創建一個新的 Windows 專案。
從 NuGet 安裝IronPrint庫。
導入庫。
編寫代碼以匯入 PDF 文件。
實現邏輯並處理異常。
.NET C# 應用程式的開發人員可以使用 IronPrint,這是一個強大的 C# 列印庫,可以幫助他們整合列印功能。 IronPrint 是一個可靠的文件列印解決方案,無論您是在開發桌面、移動或網絡應用程式。
IronPrint 提供非同步列印、更高的平台相容性以及改進的列印功能。
對於需要流暢文件輸出的應用程式,IronPrint 是一個重要的工具,因為它賦予 .NET 開發人員精確的列印控制。 調查 IronPrint 以使用高效的文檔列印增強您的程式。 要了解更多有關 IronPrint 的信息,請參閱此文件頁面。
開啟 Visual Studio 應用程式並點擊「檔案」選單。 然後選擇「新專案」,接下來在 C# 中選擇「Windows Forms 應用程式 (.NET Framework)」。
在選擇專案位置後,將專案名稱指定到分配的文字欄位中。 接下來,選擇必要的 .NET Framework,然後按照下面的範例點擊建立按鈕。
接下來,Visual Studio 專案的組織方式將取決於選擇的應用程式。 只需打開 Form1.cs 文件,即可開始添加代碼並構建 Windows Forms 應用程式。
然後可以測試程式碼並添加庫。
利用 Visual Studio 工具,從工具選單中選擇 NuGet 套件管理器。 要查看套件管理終端機控制台,請導航至套件管理介面。
Install-Package IronPrint
套件下載和安裝後,現在可以在正在進行的專案中使用。
另一個選擇是使用 NuGet 套件管理器解決方案方法。 在 Visual Studio 中,您可以使用 NuGet 套件管理器直接將套件安裝到解決方案中。 下圖說明如何開啟 NuGet 套件管理器。
使用 NuGet 網站上的搜索框來查找套件。 只需在套件管理器中搜索"IronPrint",如下面的截圖所示。
附圖顯示了一系列相關的搜尋結果。 請進行以下變更以便在您的電腦上安裝NuGet IronPrint庫。
使用IronPrint庫,列印檔案變得簡單。 第一步是在預設建立專案時創建的 Windows 表單中設計 Windows 表單,並添加兩個按鈕。 第一個按鈕是選擇我們需要列印的 PDF 文件。 第二個按鈕用於打印 PDF 文件。
在此範例中,我們將透過幾行程式碼列印 PDF 檔案。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(this);
}
private void button2_Click(object sender, EventArgs e)
{
Printer.Print(openFileDialog1.FileName.ToString());
//or
// Configure print setting and then Print the file
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
Printer.Print(openFileDialog1.FileName.ToString(), printSettings);
// or
Printer.ShowPrintDialog(openFileDialog1.FileName.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using IronPrint;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog(this);
}
private void button2_Click(object sender, EventArgs e)
{
Printer.Print(openFileDialog1.FileName.ToString());
//or
// Configure print setting and then Print the file
PrintSettings printSettings = new PrintSettings();
printSettings.Dpi = 150;
printSettings.NumberOfCopies = 2;
printSettings.PaperOrientation = PaperOrientation.Portrait;
Printer.Print(openFileDialog1.FileName.ToString(), printSettings);
// or
Printer.ShowPrintDialog(openFileDialog1.FileName.ToString());
}
}
}
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Threading.Tasks
Imports System.Windows.Forms
Imports IronPrint
Namespace WindowsFormsApp1
Partial Public Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
openFileDialog1.ShowDialog(Me)
End Sub
Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
Printer.Print(openFileDialog1.FileName.ToString())
'or
' Configure print setting and then Print the file
Dim printSettings As New PrintSettings()
printSettings.Dpi = 150
printSettings.NumberOfCopies = 2
printSettings.PaperOrientation = PaperOrientation.Portrait
Printer.Print(openFileDialog1.FileName.ToString(), printSettings)
' or
Printer.ShowPrintDialog(openFileDialog1.FileName.ToString())
End Sub
End Class
End Namespace
在上面的代碼範例中,要使用IronPrint庫,我們首先將其匯入代碼中「using IronPrint」。 然後,我們可以通過點擊「選擇檔案」按鈕,使用openfiledialog控制項來協助用戶選擇本地磁碟上可用的PDF文件。 選擇 PDF 文件後,系統會等待用戶點擊“列印”按鈕。 當按下列印按鈕時,我們將輸入的 PDF 文件傳遞到 Print 方法中,該方法在 IronPrint 庫的 Printer 類中可用。
Print 方法讓我們能夠在不開啟任何列印對話框的情況下,靜默列印 PDF 檔案。 在 Print 方法中傳遞檔案名稱後,它將把列印的 PDF 文件載入到物件中,並將文件發送到預設的印表機。 現在打印機將打印 PDF 文件。 列印物件允許我們傳遞兩種類型的參數,一種是檔案名稱或檔案位元組陣列,用於使用預設列印設定列印 PDF 檔,另一種是PrintSetting參數,透過它我們可以指定列印機設定,例如頁面大小、紙張方向為直向或橫向、列印機名稱、紙張邊距、透過NumberofCopies設定列印多份等等。
如果我們不想靜默列印文件,可以使用另一個方法列印 PDF 檔案,稱為ShowPrintDialog,這將開啟列印對話方塊選單,讓我們選擇印表機選項。 要了解更多關於IronPrint程式碼的信息,請參閱程式碼範例頁面。
總而言之,IronPrint 是數位時代中展現無障礙性和知識共享力量的豐碑。 IronPrint 是學者、業餘愛好者和學生的寶貴資源,其廣泛的印刷作品集合涵蓋了多種主題、流派和語言。 透過採用技術和數位化其藏品,IronPrint 已使這些無價資源對全球觀眾變得可及,打破資訊界限,促進學習和探索,以達到前所未有的規模。 作為啟蒙的燈塔,IronPrint 保留了過去,提升了現在,並鼓勵未來的世代在社會變遷之際探索人類創造力和知識的輝煌。
IronPrint 的經濟型開發版本提供免費試用以了解更多價格信息。如需了解更多 Iron Software 產品,請查看他們的網站。