跳過到頁腳內容
使用 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 文件 無需 Adob​​e 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="網頁表單" class="img-responsive add-shadow" />
        <p class="content__image-caption">網頁表單</p>
    </div>
    • 為您的應用程式選擇最新的.NET Framework 。 我們將使用穩定版本 6.0。

      附加資訊

      附加資訊

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

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

        解決方案探索器

        解決方案探索器

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

        專案選單 > 管理NuGet程式包

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

        從NuGet套件安裝IronPDF

        從NuGet套件安裝IronPDF

    -直接下載NuGet套件- 下載IronPDF 的另一種方法是訪問NuGet網站並直接下載該套件。 這是連結: https: NuGet 。 -下載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/");
$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();
$vbLabelText   $csharpLabel

此方法會將 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();
$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();
}
$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();
$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);
$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();
}
$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 對於個人開發是免費的,並提供試用許可讓您可以在承諾購買商業許可之前測試其全部功能。

如何指定特定打印機來列印 PDF 與 IronPDF?

您可以設置 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 機器人,結合科技與創意的樂趣。

Iron Support Team

We're online 24 hours, 5 days a week.
Chat
Email
Call Me