跳過到頁腳內容
使用 IRONPRINT

如何在 C# 中靜默打印 PDF 文件

PDF文件是一種可攜式文件格式,可以要求的格式儲存和傳輸資料。 它能保持所儲存資料的格式,使用戶能夠專注於處理數位文件的其他方面。 列印電子文檔是一項繁瑣的任務,因為資料在不同的平台上顯示效果不同。 但是,以 PDF 文件形式傳送資料有助於保持列印格式。 然而,對於 C# 開發人員來說,以程式方式列印 PDF 文件可能是一個挑戰。 透過IronPDF - C# PDF 庫,列印 PDF 文件的過程變得極為簡單便捷。

本文將介紹如何使用 IronPDF 庫在 C# 中靜默列印 PDF 文件。

IronPDF - C# PDF 庫

IronPDF 是一個 C# .NET 程式庫,它允許開發人員建立、讀取和編輯 PDF 文件。 這是一個頂級的 C# 庫,優先考慮準確性、易用性和速度。 它專為C#、F# 和 VB.NET設計,並與.NET 7、6、5、Core、Standard 或 Framework 版本高度相容。它利用 IronPDF 強大的 Chromium 引擎,幫助使用者從 HTML 產生適用於 Web、桌面和控制台的 PDF 檔案。

此外,IronPDF 還允許使用者輕鬆操作和編輯 PDF,添加頁首和頁腳,從 PDF 中提取文字和圖像。

一些重要功能包括:

  • 從不同文件格式載入和產生 PDF 文件
  • 使用預設印表機儲存和列印 PDF 文件 合併和拆分 PDF 文件 無需 Adobe Reader 的 PDF 編輯器

使用 IronPDF 庫在 C# 中靜默列印 PDF 文件的步驟

要靜默列印 PDF 文檔,首先需要在本機電腦上安裝以下組件。

  1. Visual Studio - 它是 C# 開發的官方 IDE,必須安裝在電腦上。 您可以從Visual Studio 網站下載並安裝。 2.建立專案- 若要使用 Visual Studio 2022 建立一個用於 PDF 列印的 C# 控制台應用程序,請按照以下步驟操作: 開啟 Visual Studio,然後按一下"建立新專案"

    <div class="content-img-align-center">
        <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-1.webp" alt="Visual Studio" class="img-responsive add-shadow" />
        <p class="content__image-caption">Visual Studio</p>
    </div>

    選擇 C# 控制台應用程序,然後按一下"下一步"。

    <div class="content-img-align-center">
        <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-2.webp" alt="新專案對話框" class="img-responsive add-shadow" />
        <p class="content__image-caption">新專案對話框</p>
    </div>

    現在,輸入您的項目名稱,選擇位置,然後按一下"下一步"。

    <div class="content-img-align-center">
        <img src="/static-assets/pdf/blog/csharp-print-pdf-silently/csharp-print-pdf-silently-3.webp" alt="Web Forms" class="img-responsive add-shadow" />
        <p class="content__image-caption">Web Forms</p>
    </div>
    • 為您的應用程式選擇最新的 .NET Framework。 我們將使用穩定版本 6.0。

      附加資訊

      附加資訊

    • 點擊創建,控制台專案就創建好了,我們可以開始以程式方式列印 PDF 文件了。 3.安裝 IronPDF - 有 3 種方法可以下載和安裝 IronPDF 庫。 這些工具如下: -使用 Visual Studio - Visual Studio 具有 NuGet 套件管理器,可以協助在 C# 專案中安裝 NuGet 套件。

      • 點選選單列中的"工具",或
      • 在解決方案資源管理器中以滑鼠右鍵按一下專案文件

        解決方案資源管理器

        解決方案資源管理器

        專案選單 > 管理 NuGet 套件

        專案選單 > 管理 NuGet 套件

      • 開啟後,在 NuGet 套件管理器中瀏覽 IronPDF 並進行安裝,如下所示:

        從 NuGet 套件安裝 IronPDF

        從 NuGet 套件安裝 IronPDF

    -直接下載 NuGet 套件- 下載 IronPDF 的另一種方法是訪問 NuGet 網站並直接下載該包。 這是連結https://www.nuget.org/packages/IronPdf/ 。 -下載 IronPDF .DLL 庫- 您也可以直接從 IronPDF 網站下載 IronPDF。請造訪: IronPDF DLL 下載頁面進行安裝。 在專案中引用 .DLL 檔案即可使用它。

產生 PDF 文件並列印作業

我們將從 URL 產生 PDF 文件。 建立 PDF 文件很簡單,通常只需兩個步驟。 以下程式碼範例產生 PDF 檔案:

using IronPdf;

// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();

// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
using IronPdf;

// Initialize a new instance of ChromePdfRenderer.
ChromePdfRenderer Renderer = new ChromePdfRenderer();

// Render the specified URL as a PDF document.
PdfDocument Pdf = Renderer.RenderUrlAsPdf("https://ironpdf.com/");
Imports IronPdf

' Initialize a new instance of ChromePdfRenderer.
Private Renderer As New ChromePdfRenderer()

' Render the specified URL as a PDF document.
Private Pdf As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")
$vbLabelText   $csharpLabel

使用上述程式碼建立了一個 PDF 文檔對象,該對像已準備好進行列印。 接下來,我們將使用預設印表機將PDF 文件列印到紙張上。 這段程式碼只有一行,內容如下:

// Print the PDF document using the default printer settings.
Pdf.Print();
// Print the PDF document using the default printer settings.
Pdf.Print();
' Print the PDF document using the default printer settings.
Pdf.Print()
$vbLabelText   $csharpLabel

Print方式會將 PDF 檔案傳送至預設印表機進行列印。

進階列印選項

IronPDF 為靜默列印提供了多種進階列印選項。

使用PdfDocument.GetPrintDocument方法,並將結果儲存在System.Drawing.Printing.PrintDocument物件中。 程式碼很簡單,如下所示:

// Remember to add assembly reference to System.Drawing.dll in project

// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
// Remember to add assembly reference to System.Drawing.dll in project

// Get the print document for the PDF.
System.Drawing.Printing.PrintDocument PrintPDF = Pdf.GetPrintDocument();
' Remember to add assembly reference to System.Drawing.dll in project

' Get the print document for the PDF.
Dim PrintPDF As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
$vbLabelText   $csharpLabel

指定印表機名稱

IronPDF 也提供了指定列印到特定印表機的功能。 若要指定名稱,可以使用PrinterSettings.PrinterName屬性。 首先,我們需要取得目前的 PDF 文檔物件。 程式碼範例如下:

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Send the print job to the specified printer.
    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Send the print job to the specified printer.
    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name.
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Send the print job to the specified printer.
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

設定印表機解析度

另一個很棒的功能是設定印表機解析度。 我們可以根據輸出結果控制要列印、顯示的像素數。 PDF 文件的DefaultPageSettings.PrinterResolution屬性可用於設定解析度。 以下是一個非常簡單的程式碼範例:

// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Send the print job with the custom resolution settings.
printDocument.Print();
// Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
    Kind = PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};

// Send the print job with the custom resolution settings.
printDocument.Print();
' Set the custom resolution for the printer.
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
	.Kind = PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}

' Send the print job with the custom resolution settings.
printDocument.Print()
$vbLabelText   $csharpLabel

PrintToFile 方法

PdfDocument類別提供了PrintToFile方法,讓我們在 C#中將 PDF 列印到檔案。 它接受pathToFile作為參數,以便將檔案直接列印到該位置,而無需打開印表機對話方塊。 程式碼很簡單,如下所示:

// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
// Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", false);
' Print the document to a specified file location.
printDocument.PrintToFile("PathToFile", False)
$vbLabelText   $csharpLabel

完整的程式碼範例如下:

using IronPdf;

// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set a custom print resolution.
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Execute the print job.
    printDocument.Print();
}
using IronPdf;

// Initialize the PDF renderer and create the PDF document.
ChromePdfRenderer Renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/");

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name.
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set a custom print resolution.
    printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
    {
        Kind = PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Execute the print job.
    printDocument.Print();
}
Imports IronPdf

' Initialize the PDF renderer and create the PDF document.
Private Renderer As New ChromePdfRenderer()
Private pdfDocument As PdfDocument = Renderer.RenderUrlAsPdf("https://ironpdf.com/")

Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name.
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Set a custom print resolution.
	printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
		.Kind = PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	' Execute the print job.
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

程式碼執行後,會將 URL 轉換為 PDF 文件。 然後,要靜默列印 PDF 文檔,可以使用GetPrintDocument方法。 程式檔案編譯執行成功後,會出現一個印表機對話框,提示將其儲存為 PDF 文件。 然後使用指定的印表機名稱儲存 PDF 檔案。

Csharp Print Pdf Silently 8 related to PrintToFile 方法

Csharp Print Pdf Silently 9 related to PrintToFile 方法

摘要

在本文中,我們仔細研究如何使用 IronPDF 靜默列印 PDF 文件。 IronPDF 在 PDF 列印時提供了許多實用選項。 它還可以追蹤已列印的頁面,並允許您在指定的頁面範圍內進行列印。

IronPDF 的靜默列印功能以及其他列印選項使其成為 C# 中處理 PDF 時的一個傑出庫。

IronPDF 可以幫助使用者將不同格式的資料轉換為 PDF,以及將 PDF 轉換為其他格式。 它使開發人員能夠輕鬆地將 PDF 功能整合到應用程式開發流程中。 另外,檢視和編輯PDF文件不需要Adobe Acrobat Reader。

IronPDF 可免費用於個人開發,也可授權用於商業用途。 It provides a free trial license to access and test out the full functionality of the library. 您可以點擊此連結查看更多詳情。

常見問題解答

如何在 C# 中靜默列印 PDF?

您可以使用 IronPDF 的 Print 方法,在 C# 中默默地打印 PDF。這可讓您在不開啟印表機對話框的情況下進行列印,確保保留您的文件格式。

設定 IronPDF 進行 PDF 無聲列印需要哪些步驟?

若要設定 IronPDF 進行無聲 PDF 列印,請透過 NuGet 套件管理員安裝函式庫,或下載 .DLL 檔案。然後,使用 Print 方法設定靜音列印設定,並指定印表機屬性,例如 PrinterNamePrinterResolution

如何確保列印時維持 PDF 格式?

IronPDF 可讓您設定特定的印表機設定,例如設定 PrinterResolution 屬性,以確保列印輸出與原始文件相符,從而協助維持 PDF 格式。

我可以用 C# 將 PDF 列印到檔案位置嗎?

是的,IronPDF 提供了 PrintToFile 方法,它允許您直接將 PDF 列印到指定的檔案位置,而無需開啟印表機對話框。

不使用 Adobe Acrobat Reader 是否可以列印 PDF?

是的,IronPdf 可讓您列印、檢視和編輯 PDF,而不需要 Adobe Acrobat Reader,使其成為在 C# 應用程式中處理 PDF 文件的靈活工具。

IronPDF 是否支援跨平台 PDF 列印?

是的,IronPdf 支援跨平台 PDF 列印,提供 Windows、macOS、Android 和 iOS 的相容性,使其適用於各種開發環境。

IronPDF 有哪些試用選項?

IronPdf 對於個人開發是免費的,並提供試用授權,可讓您在承諾商業授權之前測試其完整功能。

如何使用 IronPDF 指定特定的印表機進行 PDF 列印?

您可以透過設定 IronPDF 中的 PrinterSettings.PrinterName 屬性來指定特定的印表機,將列印工作導向您所需的印表機。

使用 IronPDF 開發 C# 應用程式建議使用何種 IDE?

Visual Studio 是使用 IronPDF 開發 C# 應用程式的推薦整合開發環境 (IDE),可為編碼和專案管理提供強大的平台。

如何排除 C# 中無聲 PDF 列印的問題?

如果您在 C# 中遇到 PDF 無聲列印的問題,請確保 IronPDF 已正確安裝,且 PrinterNamePrinterResolution 等印表機設定已正確配置。諮詢 IronPDF 文件也可以提供有用的疑難排解提示。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。