跳至頁尾內容
USING IRONPRINT

如何在 C# Windows 應用中列印 PDF 文件

可攜式文件格式(PDF),有時也稱為ISO 32000,由Adobe於1992年建立,是一種文件格式,使文件的呈現不依賴於作業系統、硬體或應用程式軟體,而能包含文字格式化和圖形。 PDF文件是一個具有定義佈局的平面文件解釋,包含所有顯示所需的文字、字體、光柵圖像、向量圖形和其他資料。 它建立在PostScript之上。

通過從.NET C#程式碼將PDF發送到列印機來自動化列印過程,節省人力,確保PDF文件建立的一致性,並讓您可以將列印功能整合到您的應用中。這提供了對列印過程的精細控制。

在本文中,我們將在C# Windows應用程式中列印PDF文件。

如何在C# Windows應用程式中列印PDF文件

  1. 建立一個新的Windows專案。
  2. 從NuGet安裝IronPrint程式庫。
  3. 匯入程式庫。
  4. 撰寫程式碼以匯入PDF文件。
  5. 實現邏輯並處理異常。
  6. 列印文件。

IronPrint

.NET C#應用程式的開發者可以使用IronPrint,一個強大的C#列印程式庫,來幫助他們整合列印功能。 IronPrint是一個可靠的文件列印解決方案,不論您開發的是桌面、移動或Web應用程式。

IronPrint的功能

  • IronPrint與Windows、macOS、Android和iOS相容,在其中任何一個上都順利運行。 IronPrint保證無論是針對Web應用、移動應用還是桌面軟體的可靠列印結果。
  • 可以使用IronPrint列印以下格式的文件:PDF、PNG、HTML、TIFF、GIF、JPEG和BITMAP。
  • 您可以直接從應用程式程式碼中列印文件與IronPrint。 可以輕鬆整合的發票、報告和標籤的列印功能。
  • 自動列印而不出現對話框。 非常適合使用者參與不必要的後台作業或批處理。
  • 調整參數如份數、紙張尺寸、方向和DPI。 IronPrint使開發者能夠自定義列印過程以滿足特定需求。
  • IronPrint通過類和方法提供某些與列印相關的功能。 對於開發者,精確和全面的列印設置確保了一個簡化的API。
  • IronPrint提供異步列印、更廣的平臺相容性和改進的列印功能。

對於需要順利文件輸出的應用程式,IronPrint是一個重要的工具,因為它給.NET開發者提供了對列印的精確控制。 探究IronPrint以提升您的程式中的有效文件列印。 欲了解更多IronPrint的資訊,請參閱此文件頁面

在Visual Studio中建立新專案

打開Visual Studio應用,並點擊文件選單。 然後選擇"新專案",接著在C#中選擇"視窗表單應用程式(.NET Framework)"。

如何在C# Windows應用程式中列印PDF文件:圖1 - 打開Visual Studio並在.NET Framework中建立新的C# Windows表單應用程式專案。

選擇專案位置後,在指定的文字框中指定專案名稱。 接下來,選擇必要的.NET Framework,然後如以下樣例所示點擊建立按鈕。

如何在C# Windows應用程式中列印PDF文件:圖2 - 選擇專案名稱和位置,接著選擇合適的.NET Framework版本並點擊建立按鈕。

接下來,Visual Studio專案的組織將取決於所選的應用程式。 只需打開Form1.cs文件以開始新增程式碼及構建Windows表單應用程式。

然後可以測試程式碼並新增程式庫。

安裝IronPrint程式庫

利用Visual Studio工具,從工具選單中選擇NuGet程式包管理器。 導覽到程式包管理員介面以查看程式包管理終端控制台。

Install-Package IronPrint

程式包現在可以在下載和安裝後用於正在進行的專案中。

如何在C# Windows應用程式中列印PDF文件:圖3 - 要使用NuGet範例指令安裝IronPrint,請使用以下指令:`Install-Package IronPrint`

另一個選項是使用NuGet包管理器的解決方案方法。 使用Visual Studio,您可以使用NuGet程式包管理器將程式包直接安裝到解決方案中。 下圖說明了如何打開NuGet程式包管理器。

如何在C# Windows應用程式中列印PDF文件:圖4 - 在Visual Studio中,前往工具 - NuGet程式包管理器 - 選擇管理解決方案的NuGet程式包。

使用NuGet網站上的搜索框查找程式包。 只需在程式包管理器中搜索"IronPrint",如下面的截圖所示。

How to Print PDF files in a C# Windows application: Figure 5 - Install IronPrint using the Manage NuGet Package for Solutions by searching IronPrint in the search bar of NuGet Package Manager, then select the project and click on the Install button.

隨附的圖片顯示了相關搜索結果的列表。 請進行這些更改以便在您的計算機上安裝NuGet IronPrint程式庫。

使用IronPrint列印PDF

使用IronPrint程式庫列印文件很容易。 第一步是通過在建立專案時在預設Windows表單中新增兩個按鈕來設計Windows表單。 第一個按鈕是選擇我們需要列印的PDF文件。 第二個按鈕是觸發列印PDF文件。

如何在C# Windows應用程式中列印PDF文件:圖6 - Windows表單設計用於選擇PDF文件並使用IronPrint程式庫列印所選PDF。

在此範例中,我們將用幾行程式碼列印PDF文件。

using System;
using System.Windows.Forms;
using IronPrint;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // Constructor to initialize the form
        public Form1()
        {
            InitializeComponent();
        }

        // Event handler for the first button click to open file dialog
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
        }

        // Event handler for the second button click to print the selected PDF
        private void button2_Click(object sender, EventArgs e)
        {
            // Print the file silently with default settings
            Printer.Print(openFileDialog1.FileName);

            // Alternative: Configure print settings before printing
            PrintSettings printSettings = new PrintSettings
            {
                Dpi = 150,
                NumberOfCopies = 2,
                PaperOrientation = PaperOrientation.Portrait
            };

            // Print with custom settings
            Printer.Print(openFileDialog1.FileName, printSettings);

            // Alternative: Show print dialog
            Printer.ShowPrintDialog(openFileDialog1.FileName);
        }
    }
}
using System;
using System.Windows.Forms;
using IronPrint;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        // Constructor to initialize the form
        public Form1()
        {
            InitializeComponent();
        }

        // Event handler for the first button click to open file dialog
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog(this);
        }

        // Event handler for the second button click to print the selected PDF
        private void button2_Click(object sender, EventArgs e)
        {
            // Print the file silently with default settings
            Printer.Print(openFileDialog1.FileName);

            // Alternative: Configure print settings before printing
            PrintSettings printSettings = new PrintSettings
            {
                Dpi = 150,
                NumberOfCopies = 2,
                PaperOrientation = PaperOrientation.Portrait
            };

            // Print with custom settings
            Printer.Print(openFileDialog1.FileName, printSettings);

            // Alternative: Show print dialog
            Printer.ShowPrintDialog(openFileDialog1.FileName);
        }
    }
}
Imports System
Imports System.Windows.Forms
Imports IronPrint

Namespace WindowsFormsApp1
	Partial Public Class Form1
		Inherits Form

		' Constructor to initialize the form
		Public Sub New()
			InitializeComponent()
		End Sub

		' Event handler for the first button click to open file dialog
		Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
			openFileDialog1.ShowDialog(Me)
		End Sub

		' Event handler for the second button click to print the selected PDF
		Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs)
			' Print the file silently with default settings
			Printer.Print(openFileDialog1.FileName)

			' Alternative: Configure print settings before printing
			Dim printSettings As New PrintSettings With {
				.Dpi = 150,
				.NumberOfCopies = 2,
				.PaperOrientation = PaperOrientation.Portrait
			}

			' Print with custom settings
			Printer.Print(openFileDialog1.FileName, printSettings)

			' Alternative: Show print dialog
			Printer.ShowPrintDialog(openFileDialog1.FileName)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

在上面的程式碼範例中使用IronPrint程式庫,首先我們使用using IronPrint;將其匯入到程式碼中。 然後我們幫助使用者通過點擊"選擇文件"按鈕使用OpenFileDialog控件選擇本地驅動器上可用的PDF文件。 選擇PDF文件後,應用等待使用者點擊列印按鈕。 當點擊列印按鈕時,我們將輸入的PDF文件傳遞到Print方法中,這是IronPrint程式庫的Printer類中提供的方法。

Print方法允許我們安靜地列印PDF文件而不打開任何列印對話框。 在Print方法中傳遞文件名稱後,它將列印的PDF文件載入到物件中並將文件發送到預設的列印機。 現在列印機將列印PDF文件。 列印物件允許我們傳遞兩種參數:一是用於使用預設列印設置列印PDF文件的文件名或文件位元組陣列,二是PrintSetting參數,在該參數中,我們可以指定列印設置,如頁面尺寸、紙張方向為縱向或橫向、列印機名稱、紙張邊距,使用NumberOfCopies設置列印多份等。

如果我們不希望靜默列印文件,我們可以使用另一種方法稱為ShowPrintDialog的列印,這將打開列印對話選單並允許我們選擇列印設置。 要了解有關IronPrint程式碼的更多資訊,請參閱程式碼範例頁面。

結論

總之,IronPrint證明了數位時代的可及性和知識共享的力量。 IronPrint對於學者、業餘愛好者和學生來說是一個無價的資源,其廣泛的印刷作品集合涵蓋了廣泛的主題、型別和語言。 通過採用技術及其收藏的數位化,IronPrint使這些無價的資源對全球觀眾可及,拆除資訊邊界,推動學習和探索,規模前所未有。 作為啟蒙的燈塔,IronPrint保留了過去,提升了現在,鼓勵未來的世代在社會變遷中去發現人類創造力和知識的輝煌。

IronPrint的成本效益開發版本有免費試用,以了解更多關於價格的資訊。欲了解其他Iron Software產品的更多資訊,請查看其網站

常見問題

如何在C# Windows應用程式中列印PDF檔案?

要在C# Windows應用程式中列印PDF檔案,您可以使用IronPrint程式庫。首先,在Visual Studio中建立新的Windows專案,通過NuGet安裝IronPrint程式庫,然後使用Printer.Print方法將PDF檔案發送到印表機。

在C#應用程式中支持哪些檔案格式的列印?

IronPrint支持在C#應用程式中列印各種檔案格式,包括PDF、PNG、HTML、TIFF、GIF、JPEG和BITMAP。

我能使用C#程式庫進行靜默列印嗎?

是的,使用IronPrint,您可以進行靜默列印,允許檔案自動列印而不顯示列印對話框,非常適合背景處理。

如何在C#應用程式中自訂列印設置?

您可以使用IronPrint的PrintSettings類來自訂C#應用程式中的列印設置。這使您可以調整設置,例如副本數量、紙張大小、方向和DPI。

C#中支持非同步列印嗎?

是的,IronPrint支持C#中的非同步列印,使您的應用程式在文件列印時可以繼續執行其他任務。

如何安裝用於PDF列印的C#程式庫?

要在C#專案中安裝用於PDF列印的IronPrint程式庫,請在Visual Studio中使用NuGet套件管理器。在套件管理器控制臺中執行命令Install-Package IronPrint或通過NuGet套件管理器介面找到IronPrint。

使用C#列印程式庫的好處是什麼?

使用像IronPrint這樣的C#列印程式庫能夠精確控制文件輸出,跨平台相容,並提供一個簡化的API以增強.NET應用程式的列印功能。

如何開始一個新的Windows Forms專案來在C#中列印?

要在C#中開始一個新的Windows Forms專案來列印,請打開Visual Studio,進入檔案選單,選擇“新專案”,選擇“Windows Forms應用程式(.NET Framework)”,並配置您的專案設置。

我在哪裡可以找到實現C# PDF列印的範例?

關於使用IronPrint在C#中實現PDF列印的範例和文件可以在Iron Software網站上找到,該網站提供各種情境的詳細指南和程式碼範例。

是否有試用版本可用於測試C# PDF列印?

是的,IronPrint的免費試用版本可用於測試C#應用程式中的PDF列印。您可以在Iron Software網站上找到更多詳情。

Curtis Chau
技術作家

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

除了開發,Curtis對物聯網(IoT)有濃厚的興趣,探索創新的方法來整合硬體和軟體。在空閒時間,他喜歡玩遊戲和建立Discord機器人,結合他對技術的熱愛與創造力。

Iron 支援團隊

我們線上24小時,每週5天。
聊天
電子郵件
給我打電話