在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
本文探討了直接列印 PDF 的重要性,並展示了 IronPDF 這個強大的 C# 程式庫如何促進這一過程。
IronPDF 是一個強大的 C# 程式庫,旨在幫助創建、操作和互動處理 PDF 文件。 使用IronPDF,開發人員可以輕鬆地從HTML內容、圖像和其他來源生成PDF,這使其成為不論簡單還是複雜PDF相關任務的多功能工具。 其功能不僅限於PDF文件生成; IronPDF 還能讓您以各種方式合併、分割和操作 PDF 文件。 此外,其直觀的 API 讓初學者和有經驗的開發人員都能輕鬆使用。 一些重要功能包括:
在處理 PDF 時,列印是一項基本需求。 從 C# 應用程式直接列印 PDF 而不顯示列印對話框具有多個優勢。 它透過消除不必要的互動、自動化列印任務,以及使其無縫整合到更大的工作流程中來提升用戶體驗。 IronPDF 簡化了這個過程,讓開發人員在保持對打印過程的控制的同時,確保打印輸出的完整性。
在進行 PDF 列印過程之前,請確保您已具備以下必要條件:
Visual Studio:這是一個整合開發環境(IDE),您可以在其中創建、編輯和編譯您的 C# 專案。 要下載並安裝到您的電腦上,請造訪官方的Visual Studio 網站。
建立 Visual Studio 主控台專案是一個簡單的過程。 按照以下步驟使用 Visual Studio 創建一個新的控制台應用程序:
開啟Visual Studio:啟動您的Visual Studio整合開發環境(IDE)。
建立新專案:打開 Visual Studio 後,點擊「建立新專案」。
選擇專案範本:在 "建立新專案" 視窗中,您將看到一個專案範本列表。 選擇 Visual C# 控制台應用程式。
在 Visual Studio 中建立新的 C# 主控台應用程式專案
配置專案詳情: 在選擇範本後,您將被要求配置專案詳情。
配置您的新專案
配置其他設定:選擇具有長期支持的 .NET Framework。 IronPDF 支援最新版本的 .NET Framework。
您可以參考以下步驟在您的專案中安裝IronPDF:
打開 Visual Studio 和您的專案。
前往 "工具" 菜單並選擇 "NuGet 套件管理器",然後點擊 "管理解決方案的 NuGet 套件"。
導航至 NuGet 套件管理員
在「瀏覽」標籤中,在搜索框中搜尋「IronPDF」。
在 NuGet 套件管理器 UI 中搜尋 IronPDF
IronPdf
包並將其選擇到您的專案,然後點擊“安裝”按鈕。IronPdf
命名空間代碼首先匯入了
using IronPdf;
using System.Drawing.Printing;
using IronPdf;
using System.Drawing.Printing;
Imports IronPdf
Imports System.Drawing.Printing
ChromePdfRenderer
負責渲染網頁。 PdfDocument
類別代表 PDF 文件並提供了多種可執行操作的方法,包括列印。 以下代碼從 IronPDF 網站網址(https://ironpdf.com/)生成 PDF 文件:
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
ChromePdfRenderer renderer = new ChromePdfRenderer();
PdfDocument pdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/");
Dim renderer As New ChromePdfRenderer()
Dim pdfDocument As PdfDocument = renderer.RenderUrlAsPdf("https://ironpdf.com/")
在上述程式碼範例中,第一行初始化了 IronPDF 函式庫中的 ChromePdfRenderer
實例,其負責將網頁轉換為 PDF 格式。 第二行使用此實例透過從指定的 URL 呈現內容來建立一個 PdfDocument
,以啟用進一步的 PDF 相關操作。
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf");
pdfDocument.Print(300, "Microsoft Print to PDF", "files/printedfile1.pdf")
上述程式碼行使用指定的列印解析度(DPI)啟動PdfDocument
的列印過程,將其發送到「Microsoft Print to PDF」印表機,該印表機通常是在未安裝其他印表機的情況下的預設印表機,並將列印的輸出儲存為檔案目錄中的一個名為「printedfile1.pdf」的 PDF 檔案。 您可以根據需要更改打印機名稱和文件位置。
PDF 檔案列印得像素完美:
輸出名為 "printedfile1.pdf" 的 PDF 檔案圖像
要更好地控制以程式方式列印PDF檔案,請查看具有高級選項的以下代碼片段:
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf";
printDocument.PrinterSettings.PrintToFile = true;
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.PrinterSettings.PrintFileName = "files/printedfile2.pdf"
printDocument.PrinterSettings.PrintToFile = True
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
printDocument.Print()
End Using
在上面的程式碼片段中,IronPDF 促進了高級 PDF 列印自訂功能。 它從PdfDocument
生成一個PrintDocument
,允許自定義打印機設定、打印機名稱、輸出文件名稱和解析度。 設定 PrintFileName
很重要,因為它可以讓你繞過列印對話框進行列印。 然後,代碼使用列印方法觸發列印,將列印的 PDF 文件內容導向指定的印表機,即在此情況下的「Microsoft Print to PDF」。 最後,將輸出保存為 PDF 文件。這可實現無縫、無對話框的 PDF 列印,並具有定制的設置。
您還可以指定頁面範圍來在沒有打印機對話框和Adobe Acrobat Reader的情況下打印PDF文件,這一切都可以通過將IronPDF整合到您的專案中來實現。 如需有關列印的詳細資訊,請造訪代碼範例頁面。
在執行專案後,兩個輸出的 PDF 檔案會在指定的資料夾中生成,無需任何使用者互動。 您還可以使用高級選項來比較大小差異。
顯示兩個打印的 PDF 文件 "printedfile1.pdf" 和 "printedfile2.pdf" 的圖像
直接從 C# 應用程式列印 PDF 文件簡化了文件處理並增強了使用者體驗。 IronPDF,憑藉其多樣化的 PDF 操作工具,使開發人員能夠無縫打印 PDF 文檔。 通過將IronPDF整合到您的C#專案中,您可以充分利用其功能。
IronPDF 是一個以其強大的 PDF 處理功能著稱的商業庫。 它提供免費試用期,使用户能够测试和体验其功能。 試用期結束後,可以取得授權以繼續使用。 要開始使用,您可以從官方IronPDF網站下載該產品。