在生產環境中測試,無水印。
在任何需要的地方都能運行。
獲得 30 天的全功能產品。
在幾分鐘內上手運行。
試用產品期間完全訪問我們的支援工程團隊
PDF 的意思是「可攜式文件格式」。 在許多情況下,開發者需要在應用程式中以程式方式列印 PDF 檔案。 在 C# 中,這可能是一項非常繁瑣的工作,但由於 IronPDF,這可以通過幾行代碼輕鬆完成。 此工具允許我們使用預設的打印機設置以及自定義打印選項來打印 PDF 文件。 在本教程中,您將學習如何使用C#語言打印PDF文件。
下面將涵蓋以下主題:
安裝 IronPDF
NuGet 套件管理器
NuGet 套件管理器主控台
列印 PDF 文件
創建 PDF 文件並列印 PDF
從網址創建 PDF 文件並列印
Print
方法以預設列印機設定列印 PDFPrinterName
屬性發送到指定的打印機PrinterResolution
屬性以自訂印表機的解析度IronPDF 是一個適用於 .NET Framework 的 PDF 函式庫,允許開發人員輕鬆建立 PDF 文件。 IronPDF 的渲染在桌面版 Google Chrome 中是「像素完美」的。 IronPDF 可以輕而易舉地使用單行代碼創建 PDF 文件。 它能在不用 Acrobat Reader 或其他 PDF 查看器的情況下處理 PDF 文件。
IronPDF 可以用來從 HTML 字串、HTML 檔案或 URL 建立 PDF 文件。 之後,它可以將這些文件發送到預設打印機進行打印。
可以免費試用IronPDF。
本教程將使用 Visual Studio 2022,但您也可以使用早期版本。
控制台應用程式
.NET框架
要安裝 IronPDF Library,我們可以使用以下列出的任何方法:
我們可以從 NuGet 套件管理器安裝 IronPDF C# .NET Core 庫。
通過點擊工具 > NuGet 套件管理器 > 管理解決方案的 NuGet 套件打開套件管理器。
套件管理器
或者,在方案總管中右鍵點擊專案,然後選擇管理 NuGet 套件。
NuGet 套件管理員 - 解決方案總管
搜尋 IronPDF。 選擇 IronPDF 並點擊安裝。 程式庫將開始安裝。
安裝 IronPDF
通過點擊工具 > NuGet 套件管理器 > 套件管理器控制台來打開NuGet套件管理器控制台。
在命令行中輸入以下命令:
Install-Package IronPrint
套件管理控制台
在您的專案中使用IronPDF的另一種方法是從IronPDF庫中添加DLL檔案。 您可以從此連結下載 DLL 檔案。
安裝完成後,將 IronPDF 和 System.Drawing.DLL
命名空間添加到您的程式檔案中。
using IronPdf;
using System.Drawing.dll;
using IronPdf;
using System.Drawing.dll;
Imports IronPdf
Imports System.Drawing.dll
注意:您必須在每個希望使用 IronPDF 功能的文件中添加這些引用。
IronPDF 已安裝完畢並準備就緒! 我們現在可以為 .NET Core 應用程式建立我們的第一個 PDF 文件,並將其發送到預設印表機進行列印。 讓我們看看下面的一些程式碼範例。
處理 HTML 字串並將其轉換為 PDF 格式非常容易。 這個新創建的檔案可以使用 IronPDF 進行列印。 以下是輕鬆創建 PDF 的程式碼。
// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");
// Send the PDF to the default printer to print
Pdf.Print();
System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
// Render any HTML fragment or document to HTML
var html= new ChromePdfRenderer();
using var PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file");
// Send the PDF to the default printer to print
Pdf.Print();
System.Drawing.Printing.PrintDocument PrintDocYouCanWorkWith = Pdf.GetPrintDocument();
' Render any HTML fragment or document to HTML
Dim html= New ChromePdfRenderer()
Dim PDF = html.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to PDF file")
' Send the PDF to the default printer to print
Pdf.Print()
Dim PrintDocYouCanWorkWith As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
這段程式碼將使用RenderHtmlAsPdf
函數傳入的HTML內容創建一個PDF檔案。 此功能將 HTML 片段轉換為 PDF 文件。
您必須熟悉 HTML 標籤,才能使用 IronPDF 庫生成 PDF 文件或 PDF 頁面。 我們使用Print
函數將 PDF 文件的輸出發送到打印機。 印表機對話框將會出現,讓您確認列印任務。
您也可以使用網址製作 PDF 文件:
var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");
// Send the PDF to the default printer to print
Pdf.Print();
System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
var Render = new ChromePdfRenderer();
var PDF = Render.RenderUrlAsPdf("https://ironpdf.com/");
// Send the PDF to the default printer to print
Pdf.Print();
System.Drawing.Printing.PrintDocument PrintDoc = Pdf.GetPrintDocument();
Dim Render = New ChromePdfRenderer()
Dim PDF = Render.RenderUrlAsPdf("https://ironpdf.com/")
' Send the PDF to the default printer to print
Pdf.Print()
Dim PrintDoc As System.Drawing.Printing.PrintDocument = Pdf.GetPrintDocument()
PDF 將會如下所示列印:
套件管理控制台
IronPDF 功能多樣,能夠處理打印功能,如查找打印機和設置打印分辨率。
要指定打印機,您需要做的只是獲取當前打印的文檔對象(使用GetPrintDocument
方法),然後使用PrinterSettings.PrinterName
屬性。 您可以選擇任何可用的打印機。
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.Print()
End Using
在上面的代碼範例中,我選擇「Microsoft Print to PDF」。 有關設定特定列印設定的更多資訊可以在文件頁面中找到。
您也可以設定列印 PDF 的解析度。 解析度是指根據您的輸出正在列印或顯示的像素數量。 您也可以通過 IronPDF 設定列印文件的解析度,使用 PDF 文件的 DefaultPageSettings.PrinterResolution
屬性。
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
在以下的程式碼範例中,您將看到如何更改打印機名稱、解析度,以及如何獲取已打印頁面的計數。
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
var printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;
printDocument.Print();
}
using (var printDocument = PDF.GetPrintDocument())
{
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";
printDocument.DefaultPageSettings.PrinterResolution = new PrinterResolution
{
Kind = PrinterResolutionKind.Custom,
X = 1200,
Y = 1200
};
var printedPages = 0;
printDocument.PrintPage += (sender, args) => printedPages++;
printDocument.Print();
}
Using printDocument = PDF.GetPrintDocument()
printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"
printDocument.DefaultPageSettings.PrinterResolution = New PrinterResolution With {
.Kind = PrinterResolutionKind.Custom,
.X = 1200,
.Y = 1200
}
Dim printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: printDocument.PrintPage += (sender, args) => printedPages++;
AddHandler printDocument.PrintPage, Sub(sender, args) printedPages
printedPages += 1
printDocument.Print()
End Using
IronPDF 是處理 PDF 文件的完整解決方案。 它提供了從不同格式轉換為 PDF 的功能。 使用 IronPDF 函式庫,PDF 檔案的操作和格式化變得非常簡單。 只需要幾行程式碼即可創建並格式化 PDF 文件。它還可以以程式的方式列印 PDF。 它通過將 PDF 發送到電腦的默認打印機來打印。 我們可以向用戶顯示打印對話框,或者可以使用Print
方法的重載來安靜地打印。
IronPDF 的免費試用也可用於測試其在您的應用程式中生成和列印 PDF 文件的全部潛力。 有關授權的更多信息可以在此連結中找到。
此外,目前的特別優惠讓您可以用兩個產品的價格獲得五個Iron Software產品。