如何在 C# 中打印 Word 文檔
在不斷發展的軟體開發領域,以程式方式產生和列印 Word 文件的能力是一項基本要求。 C# 開發人員經常會遇到這樣的情況:產生和列印 Word 文件對於產生報表、處理文件或建立專業外觀的輸出等任務至關重要。 為了滿足這項需求,Iron Software 推出了IronWord 、 IronPDF和IronPrint ,這些功能強大的程式庫旨在簡化 C# 應用程式中 Word 和 PDF 文件的建立、操作和列印。
本文將探討 IronPrint 的功能和優勢,介紹如何使用 IronWord 建立 Word 文件物件傳送器,以及如何使用 IronPDF 將其轉換為 PDF 進行列印。
如何在 C# 中列印 Word 文檔
- 建立一個 Visual Studio 項目
- 安裝 IronWord、IronPDF 和 IronPrint 庫
- 使用 IronWord
WordDocument類別建立 Word 文件 - 使用
SaveAs方法儲存 Word 文檔 - 使用 IronPDF 的
DocxToPdfRenderer方法建立 PDF 文檔 - 使用 IronPrint 調整
PrinterSettings - 使用 IronPrint
Printer.Print列印。列印方法
IronPrint
IronPrint由 Iron Software 開發,是一個功能強大且用途廣泛的 .NET 列印庫,為 C# 中的列印任務處理提供了各種工具。 它以專門針對列印相關功能量身定制的類別和方法脫穎而出,為開發人員提供了對列印過程和印表機設定的精細控制。
IronPrint 的主要特點
1. 全面列印設定
IronPrint 使開發人員能夠自訂列印流程的各個方面。 這包括:
紙張尺寸
- 方向(縱向或橫向)
- DPI(每吋點數)
- 份數
- 印表機名稱
- 頁邊距(上、下、左、右)
- 灰階列印
2. 多功能列印,印表機類別
IronPrint 的一個突出特點是引入了Printer類別。 這類提供了一套全面的方法,用於列印各種文件類型,包括圖像和 PDF 文件。 印表機類的多功能性使其能夠無縫整合到各種列印場景中。 它還允許在即時應用程式中列印時顯示列印對話框,從而對 Word 文件的列印進行更精細的控制。
3. 跨平台支持
IronPrint 具有跨平台相容性,使其適合在多個環境中部署。 無論您的應用程式運行在 Windows、macOS、Android 或 iOS 系統上,IronPrint 都能確保列印功能的一致性和可靠性。
先決條件
在開始實施之前,請確保您已具備以下先決條件:
建立、轉換和列印 Word 文件的步驟
請依照以下步驟設定 C# 控制台應用程式,建立 Word 文件對象,將其轉換為 PDF,最後分別使用 IronWord、IronPDF 和 IronPrint 進行列印。
步驟 1:在 Visual Studio 中建立一個 C# 控制台應用程式
1.開啟 Visual Studio 並建立新的 C# Console Application。
- 依下列方式配置項目,然後按一下"下一步"。
- 從"附加資訊"中選擇 .NET Framework,然後按一下"建立"。
步驟 2:透過 NuGet 套件管理器安裝必要的庫
- 使用 Visual Studio 專案中的"工具"功能表或"解決方案資源管理器"開啟 NuGet 套件管理器控制台或 NuGet 套件管理器(適用於解決方案)。
在 NuGet 的瀏覽標籤中,搜尋庫並按一下安裝。 3.安裝 IronPrint列印庫:
使用 NuGet 套件管理器控制台,執行下列命令:
Install-Package IronPrint
使用"管理解決方案的 NuGet 套件"安裝 IronWord 和 IronPDF 庫。 若要使用 NuGet 套件管理員控制台安裝IronWord和IronPDF ,請使用下列指令:
Install-Package IronWord Install-Package IronPdfInstall-Package IronWord Install-Package IronPdfSHELL
步驟 3:使用 IronWord 建立 Word 文件
讓我們先使用IronWord建立一個簡單的 Word 文件。 以下程式碼片段示範如何建立包含範例文字的 Word 文件並將其儲存:
using IronWord;
using IronWord.Models;
// Code to Create Word File
// Create a TextRun object with sample text
TextRun textRun = new TextRun("Sample text");
// Create a paragraph and add the TextRun to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a Word document object with the paragraph and save it as a .docx file
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");using IronWord;
using IronWord.Models;
// Code to Create Word File
// Create a TextRun object with sample text
TextRun textRun = new TextRun("Sample text");
// Create a paragraph and add the TextRun to it
Paragraph paragraph = new Paragraph();
paragraph.AddTextRun(textRun);
// Create a Word document object with the paragraph and save it as a .docx file
WordDocument doc = new WordDocument(paragraph);
doc.SaveAs("assets/document.docx");Imports IronWord
Imports IronWord.Models
' Code to Create Word File
' Create a TextRun object with sample text
Private textRun As New TextRun("Sample text")
' Create a paragraph and add the TextRun to it
Private paragraph As New Paragraph()
paragraph.AddTextRun(textRun)
' Create a Word document object with the paragraph and save it as a .docx file
Dim doc As New WordDocument(paragraph)
doc.SaveAs("assets/document.docx")在此程式碼中
- 我們建立一個包含範例文字的
TextRun。 - 建立一個
Paragraph,並將TextRun加入到該段落中。 最後,建立一個包含該段落的WordDocument,並將新文檔儲存為"document.docx"。
輸出 Word 文件
如何在 C# 中列印 Word 文件:圖 2 - 使用 IronWord 產生的輸出 DOCX 檔案:document.docx
步驟 4:使用 IronPDF 將 Word 文件轉換為 PDF
有了 Word 文件後,我們可能需要將其轉換為 PDF格式。 IronPDF簡化了這個過程,實現了無縫轉換。 以下是程式碼片段:
using IronPdf;
// Code to convert DOCX file to PDF using IronPDF
// Create a DocxToPdfRenderer instance
var renderer = new DocxToPdfRenderer();
// Render the DOCX document as a PDF
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");
// Save the resulting PDF
pdf.SaveAs("assets/word.pdf");using IronPdf;
// Code to convert DOCX file to PDF using IronPDF
// Create a DocxToPdfRenderer instance
var renderer = new DocxToPdfRenderer();
// Render the DOCX document as a PDF
var pdf = renderer.RenderDocxAsPdf("assets/document.docx");
// Save the resulting PDF
pdf.SaveAs("assets/word.pdf");Imports IronPdf
' Code to convert DOCX file to PDF using IronPDF
' Create a DocxToPdfRenderer instance
Private renderer = New DocxToPdfRenderer()
' Render the DOCX document as a PDF
Private pdf = renderer.RenderDocxAsPdf("assets/document.docx")
' Save the resulting PDF
pdf.SaveAs("assets/word.pdf")在此程式碼中
- 我們使用 DocxToPdfRenderer() 方法將 Word 文件渲染為 PDF。
- 產生的 PDF 檔案儲存為"word.pdf"。
步驟 5:使用 IronPrint 列印 PDF 文件
使用IronPrint可以實現 PDF 列印,從而提供對列印設定的靈活性和控制。 如果未設定列印設置,則使用預設設定進行列印。 以下程式碼示範如何列印產生的 PDF 檔案:
using IronPrint;
using System.Collections.Generic;
// Code for Printing using IronPrint
// Fetch printer names available in the system
List<string> printerNames = Printer.GetPrinterNames();
// Configure print settings
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
if(printerName.Equals("Microsoft Print to PDF"))
{
printerSettings.PrinterName = printerName;
}
}
// Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
// Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings);using IronPrint;
using System.Collections.Generic;
// Code for Printing using IronPrint
// Fetch printer names available in the system
List<string> printerNames = Printer.GetPrinterNames();
// Configure print settings
PrintSettings printerSettings = new PrintSettings();
foreach(string printerName in printerNames)
{
if(printerName.Equals("Microsoft Print to PDF"))
{
printerSettings.PrinterName = printerName;
}
}
// Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4;
Margins margins = new Margins(30, 10);
printerSettings.PaperMargins = margins;
// Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings);Imports IronPrint
Imports System.Collections.Generic
' Code for Printing using IronPrint
' Fetch printer names available in the system
Private printerNames As List(Of String) = Printer.GetPrinterNames()
' Configure print settings
Private printerSettings As New PrintSettings()
For Each printerName As String In printerNames
If printerName.Equals("Microsoft Print to PDF") Then
printerSettings.PrinterName = printerName
End If
Next printerName
' Set paper size to A4 and configure margins
printerSettings.PaperSize = PaperSize.A4
Dim margins As New Margins(30, 10)
printerSettings.PaperMargins = margins
' Print the PDF with the specified settings
Printer.Print("assets/word.pdf", printerSettings)在此程式碼中
- 我們使用
Printer.GetPrinterNames()來取得可用的印表機名稱。 - 設定特定印表機名稱(在本例中為"Microsoft Print to PDF")。 如果實體印表機不可用,則 IronPrint 預設會將此印表機作為預設印表機。
- 配置列印設置,指定紙張尺寸為 A4 並設定邊距。 最後,使用
Printer.Print()方法列印 PDF。
如何在 C# 中列印 Word 文件:圖 3 - 使用 IronPrint 輸出可列印的 PDF 文件:word.pdf
如果您想控制NumberOfCopies 、多頁列印、 Grayscale和DPI請造訪此程式碼範例頁面。 您也可以啟用印表機對話方塊並阻止文件靜默列印。
IronPrint 在 C# 列印方面的優勢
以下是使用IronPrint在 C# 控制台或 Windows 窗體應用程式中進行列印作業的一些主要優點:
1. 非同步列印
IronPrint 提供非同步功能,允許非同步執行列印操作。 這樣可以防止列印操作阻塞線程,從而提高應用程式的效能和響應速度。
2. 多種列印選項
IronPrint 中的專用Printer類別提供了一套全面的方法,用於列印各種文件類型,包括圖像和 PDF 文件。 這種多功能性提供了超越標準印刷的靈活性,使開發人員能夠輕鬆處理不同類型的內容。
3. 跨平台支持
IronPrint 支援跨多個平台進行列印,包括 Windows、Android、iOS 和 macOS。 這種跨平台相容性使其適用於各種應用環境,確保您的列印功能可以部署在不同的作業系統上。
4. 可自訂的列印設定
IronPrint 允許開發人員精細控製列印設置,提供高度的自訂功能。 開發人員可以透過PrintSettings類別指定列印過程的各個方面,例如紙張尺寸、方向、DPI、份數、印表機名稱、邊距和灰階列印。
5. 與 IronQR 和 IronPDF 無縫集成
IronPrint 可以與其他 Iron Software 庫(如 IronQR 和 IronPDF)無縫整合。 此整合使開發人員能夠在統一且有效率的工作流程中建立、轉換和列印二維碼、PDF 和其他文件。
6. 使用者友善 API
IronPrint 具有用戶友好的 API,可簡化 C# 應用程式中列印功能的實作。 開發人員可以快速地為專案添加條碼和列印功能,從而減少開發時間和精力。
7. 全面的文件和支持
IronPrint 擁有 Iron Software 提供的全面文件和支援。 這確保開發人員在實現列印功能時能夠獲得資源和幫助,從而更容易解決問題並優化列印過程。
8. 加強對印刷過程的控制
借助 IronPrint,開發人員可以更好地控制列印過程。 諸如設定紙張尺寸、邊距和其他列印參數等功能,可實現精確控制,確保列印輸出符合特定要求和標準。
結論
按照這些步驟,您可以將 Word 文件處理、轉換為 PDF 和列印功能無縫整合到您的 C# 應用程式中。 IronWord 、 IronPDF和IronPrint共同為希望增強文件相關任務的開發人員提供了一套強大的工具包。 無論您是在開發 Web 應用程式、行動應用程式、桌面應用程式還是控制台應用程序,本指南都將為您提供全面的資源,幫助您在 .NET 專案中有效地利用這些程式庫。
有關如何有效列印的更多信息,請訪問此文檔頁面。
常見問題解答
如何在 C# 中列印 Word 文件而不遺失格式?
若要列印 Word 文件,同時保持其 C# 格式,請使用 IronWord 建立文件,使用 IronPDF 將其轉換為 PDF,然後再使用 IronPrint 列印。這可確保在整個過程中保留文件的格式。
在 C# 中使用 IronPrint 進行文件列印有哪些優點?
IronPrint 提供異步列印、自訂設定(如紙張大小和方向)、跨平台相容性,以及與 Iron Software 其他函式庫的無縫整合,為 C# 環境中的列印任務提供強大的解決方案。
如何將 IronWord、IronPDF 和 IronPrint 整合到 C# 專案中?
要將這些函式庫整合到 C# 專案中,請透過 Visual Studio 中的 NuGet Package Manager Console 進行安裝。使用Install-Package IronWord、Install-Package IronPDF和Install-Package IronPrint來新增Word建立、PDF轉換和列印的必要功能。
使用 IronPrint 時可以自訂列印設定嗎?
是的,IronPrint 允許您自訂各種列印設定,包括紙張大小、方向、DPI、份數、印表機名稱、邊界和灰階列印,讓您完全掌控列印流程。
IronPrint 適合跨平台列印工作嗎?
IronPrint 的設計支援跨平台,可在 Windows、macOS、Android 和 iOS 上部署,因此適用於各種開發環境。
在 C# 中建立和列印 Word 文件涉及哪些步驟?
首先,使用 IronWord 建立 Word 文件。接下來,使用 IronPDF 的 DocxToPdfRenderer 將其轉換為 PDF。最後,使用 IronPrint 列印 PDF,確保文件的格式自始至終維持不變。
IronPrint 如何增強 C# 應用程式中的文件處理能力?
IronPrint 透過提供全面的列印設定、異步列印以及與其他 Iron Software 函式庫的無縫整合,增強了文件處理功能,從而促進 C# 應用程式中文件處理與列印的效率。
建議使用哪些工具來產生和列印 C# 語言的文件?
Iron Software 建議使用 IronWord 來建立文件,使用 IronPDF 來轉換成 PDF,並使用 IronPrint 來完成最後的列印程序。這種組合可確保高品質的輸出和易用性。








