如何在C語言中列印Word文件
要在C#中列印Word文件,請使用IronWord來建立文件,使用IronPDF將其轉換為PDF格式,並使用IronPrint處理列印過程,提供可自訂的設定以支援多個平台。
當構建C#應用程式時,您經常需要以程式方式生成和列印Word文件。 無論您是在建立報告、處理文件還是生成專業輸出,擁有可靠的工具都會帶來很大不同。 這就是Iron Software的IronWord、IronPDF和IronPrint的用武之地——這些程式庫相互協作,簡化了您C#應用程式中的文件建立、轉換和列印過程。
本文引導您使用IronPrint進行列印,使用IronWord建立Word文件,並使用IronPDF將其轉換為PDF。 無論您是在構建企業報表系統還是自動化文件工作流程,這些工具為文件處理提供了您所需的一切。
How to Print a Word Document in C#?
- 建立Visual Studio專案
- 安裝IronWord、IronPDF和IronPrint程式庫
- 使用IronWord的
WordDocument類建立Word文件 - 使用
SaveAs方法儲存Word文件 - 使用IronPDF的
DocxToPdfRenderer方法建立PDF文件 - 使用IronPrint調整
PrinterSettings - 使用IronPrint的
Printer.Print方法列印
什麼是IronPrint?
IronPrint是一種適用於.NET的高效列印程式庫,可讓您完全控制C#中的列印。 由Iron Software構建,其提供專為列印任務設計的專用類和方法,讓您微調列印過程的各個方面。 該程式庫可與.NET Framework和.NET Core完美配合,因此可以在任何型別的應用程式中使用。
IronPrint的主要特點有哪些?
IronPrint如何處理列印設定?
IronPrint允許您自訂列印作業的各個方面:
- 紙張尺寸(信紙、法律、A4、A3、自定義)
- 方向(縱向或橫向)
- 用於品質控制的DPI
- 帶有排序的副本數量
- 列印機選擇和驗證
- 精確測量的邊距
- 節省成本的灰階列印
// Example: Advanced print settings configuration
using IronPrint;
// Create complete print settings
PrintSettings advancedSettings = new PrintSettings()
{
PrinterName = "HP LaserJet Pro",
PaperSize = PaperSize.A4,
PrintOrientation = PrintOrientation.Portrait,
Dpi = 600, // High quality print
NumberOfCopies = 3,
Grayscale = true,
PaperMargins = new Margins(50, 50, 40, 40) // Left, Right, Top, Bottom
};
// Apply settings to print job
Printer.Print("document.pdf", advancedSettings);
// Example: Advanced print settings configuration
using IronPrint;
// Create complete print settings
PrintSettings advancedSettings = new PrintSettings()
{
PrinterName = "HP LaserJet Pro",
PaperSize = PaperSize.A4,
PrintOrientation = PrintOrientation.Portrait,
Dpi = 600, // High quality print
NumberOfCopies = 3,
Grayscale = true,
PaperMargins = new Margins(50, 50, 40, 40) // Left, Right, Top, Bottom
};
// Apply settings to print job
Printer.Print("document.pdf", advancedSettings);
Imports IronPrint
' Example: Advanced print settings configuration
' Create complete print settings
Dim advancedSettings As New PrintSettings() With {
.PrinterName = "HP LaserJet Pro",
.PaperSize = PaperSize.A4,
.PrintOrientation = PrintOrientation.Portrait,
.Dpi = 600, ' High quality print
.NumberOfCopies = 3,
.Grayscale = True,
.PaperMargins = New Margins(50, 50, 40, 40) ' Left, Right, Top, Bottom
}
' Apply settings to print job
Printer.Print("document.pdf", advancedSettings)
Printer類如何工作?
Printer類是IronPrint的核心。 它提供用於列印各種文件型別的方法,包括圖像和PDF。 您可以將其整合到任何列印場景中,它甚至支持即時應用的列印對話框。 ShowPrintDialog方法為使用者提供熟悉的列印配置選項,當他們需要時。
IronPrint支持哪些平台?
IronPrint適用於Windows、macOS、Android和iOS–確保無論您部署在何處,都具備一致的列印功能。 這種跨平台支持延伸到WPF、Windows Forms和ASP.NET應用程式。
需要哪些先決條件?
在開始之前,請確保您擁有:
如何建立、轉換和列印Word文件?
讓我們構建一個C#控制台應用程式,來建立Word文件,將其轉換為PDF,並使用所有三個程式庫列印。
步驟1:在Visual Studio中建立C#控制台應用程式
- 打開Visual Studio並建立一個新的C#控制台應用程式。
- 配置專案並點擊"下一步"。
- 從附加資訊中選擇您的.NET Framework並點擊"建立"。
步驟2:通過NuGet包管理器安裝必要的程式庫
- 從工具選單中打開NuGet包管理器控制台。
- 在瀏覽標籤中搜尋每個程式庫並點選安裝。
-
使用此命令安裝IronPrint:
Install-Package IronPrint
-
同樣方式安裝IronWord和IronPDF。 對於控制台,使用:
Install-Package IronWord Install-Package IronPdfInstall-Package IronWord Install-Package IronPdfSHELL
步驟3:使用IronWord建立Word文件
讓我們開始建立一個簡單的Word文件,使用IronWord:
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 - 建立
WordDocument並儲存
對於更複雜的文件,新增格式設定、多個段落和表格:
using IronWord;
using IronWord.Models;
// Create a more complex Word document
WordDocument complexDoc = new WordDocument();
// Add a title paragraph with formatting
TextRun titleRun = new TextRun("Quarterly Sales Report")
{
FontSize = 24,
Bold = true,
FontFamily = "Arial"
};
Paragraph titleParagraph = new Paragraph();
titleParagraph.AddTextRun(titleRun);
// Add body content
TextRun bodyRun = new TextRun("This report contains sales data for Q4 2023.");
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddTextRun(bodyRun);
// Add paragraphs to document
complexDoc.AddParagraph(titleParagraph);
complexDoc.AddParagraph(bodyParagraph);
// Save the document
complexDoc.SaveAs("assets/sales_report.docx");
using IronWord;
using IronWord.Models;
// Create a more complex Word document
WordDocument complexDoc = new WordDocument();
// Add a title paragraph with formatting
TextRun titleRun = new TextRun("Quarterly Sales Report")
{
FontSize = 24,
Bold = true,
FontFamily = "Arial"
};
Paragraph titleParagraph = new Paragraph();
titleParagraph.AddTextRun(titleRun);
// Add body content
TextRun bodyRun = new TextRun("This report contains sales data for Q4 2023.");
Paragraph bodyParagraph = new Paragraph();
bodyParagraph.AddTextRun(bodyRun);
// Add paragraphs to document
complexDoc.AddParagraph(titleParagraph);
complexDoc.AddParagraph(bodyParagraph);
// Save the document
complexDoc.SaveAs("assets/sales_report.docx");
Imports IronWord
Imports IronWord.Models
' Create a more complex Word document
Dim complexDoc As New WordDocument()
' Add a title paragraph with formatting
Dim titleRun As New TextRun("Quarterly Sales Report") With {
.FontSize = 24,
.Bold = True,
.FontFamily = "Arial"
}
Dim titleParagraph As New Paragraph()
titleParagraph.AddTextRun(titleRun)
' Add body content
Dim bodyRun As New TextRun("This report contains sales data for Q4 2023.")
Dim bodyParagraph As New Paragraph()
bodyParagraph.AddTextRun(bodyRun)
' Add paragraphs to document
complexDoc.AddParagraph(titleParagraph)
complexDoc.AddParagraph(bodyParagraph)
' Save the document
complexDoc.SaveAs("assets/sales_report.docx")
輸出Word文件

步驟4:使用IronPDF將Word文件轉換為PDF
現在讓我們使用IronPDF將我們的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");
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
- 儲存結果
步驟5:使用IronPrint列印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()獲取可用的列印機 - 選擇特定的列印機
- 配置紙張尺寸和邊距
- 列印PDF

想要更多的關於副本控制、多頁、灰階和DPI的控制,請查看這些程式碼範例。 您也可以啟用列印機對話框供使用者交互。
使用IronPrint列印的優勢是什麼?
這就是為什麼IronPrint在C#列印任務中脫穎而出的原因:
為什麼異步列印很重要?
IronPrint提供異步功能,防止列印操作阻塞您的應用程式。 您的介面在長時間列印作業期間保持回應迅速:
// Asynchronous printing example
using IronPrint;
using System.Threading.Tasks;
public async Task PrintDocumentAsync(string filePath)
{
PrintSettings settings = new PrintSettings
{
PrinterName = "Default Printer",
NumberOfCopies = 2
};
// Non-blocking print operation
await Printer.PrintAsync(filePath, settings);
Console.WriteLine("Print job completed!");
}
// Asynchronous printing example
using IronPrint;
using System.Threading.Tasks;
public async Task PrintDocumentAsync(string filePath)
{
PrintSettings settings = new PrintSettings
{
PrinterName = "Default Printer",
NumberOfCopies = 2
};
// Non-blocking print operation
await Printer.PrintAsync(filePath, settings);
Console.WriteLine("Print job completed!");
}
Imports IronPrint
Imports System.Threading.Tasks
Public Async Function PrintDocumentAsync(filePath As String) As Task
Dim settings As New PrintSettings With {
.PrinterName = "Default Printer",
.NumberOfCopies = 2
}
' Non-blocking print operation
Await Printer.PrintAsync(filePath, settings)
Console.WriteLine("Print job completed!")
End Function
列印選項如何提高功能性?
Printer類可以處理各種文件型別,包括PDF、PNG、JPG、TIFF和BMP。這種多功能性意味著您可以不更改方法的方式列印不同內容型別。
我可以部署到哪些平台?
IronPrint可以運行在 Windows、Android、iOS 和 macOS 上。 您的列印程式碼在所有平台上都能一致工作,使得部署變得簡單。
可以自訂哪些列印設定?
通過PrintSettings類,您可以控制:
- 紙張尺寸和方向
- DPI及列印品質
- 副本和排序
- 邊距和佈局
- 雙面列印
- 自定義頁面範圍
IronPrint如何與其他程式庫整合?
IronPrint可以與其他Iron Software產品如IronBarcode和IronPDF平滑整合。 一致的API設計使得在一個工作流程中建立、轉換和列印文件變得更容易。
為什麼API被認為是使用者友好的?
IronPrint的直觀方法名稱和完整的IntelliSense支持使其易於所有開發人員使用。 您可以快速新增列印功能,無需陡峭的學習曲線。
有哪些支援資源可用?
Iron Software提供完整的文件、範例、API參考和最佳實踐。 他們的支援團隊幫助您有效地實施列印功能。
IronPrint如何改善列印控制?
IronPrint為您提供對列印的各個方面的精確控制。 設置確切的紙張尺寸、邊距和參數,以確保您的輸出滿足特定要求。 監控列印機狀態並處理錯誤以管理可靠的列印作業。
下一步是什麼?
您現在擁有在C#應用程式中建立Word文件,將其轉換為PDF並列印所需的一切。 IronWord、IronPDF和IronPrint協同工作,提供完整的文件處理解決方案。 無論您是在構建web、移動、桌面或控制台應用程式,這些工具都能簡化您的文件工作流程。
如需更多列印技術,請存取文件頁面。 探索批量列印和自定義列印處理器等功能,以提升您的應用程式能力。
常見問題
如何在C#中列印不失去格式的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完成最終的列印過程。這種組合確保高質量的輸出和易用性。


