跳過到頁腳內容
使用 IRONPRINT

如何在C語言中列印Word文檔

若要在 C# 中列印 Word 文檔,可以使用 IronWord 建立文檔,使用 IronPDF 將其轉換為 PDF 格式,然後使用 IronPrint 處理列印過程,並可在多個平台上進行自訂設定。

在建立 C# 應用程式時,您經常需要以程式設計方式產生和列印 Word 文件。 無論您是建立報告、處理文件還是製作專業成果,擁有可靠的工具都至關重要。 這時,Iron Software 的IronWordIronPDFIronPrint就派上了用場——這些庫協同工作,簡化了 C# 應用程式中的文件創建、轉換和列印。

本文將指導您如何使用 IronPrint 進行列印,如何使用 IronWord 建立 Word 文檔,以及如何使用 IronPDF 將其轉換為 PDF。 無論您是建立企業報告系統還是實現文件工作流程自動化,這些工具都能提供文件處理所需的一切。

How to Print a Word Document in C#?

  1. 建立一個 Visual Studio 項目
  2. 安裝 IronWord、IronPDF 和 IronPrint 庫
  3. 使用 IronWord 類別建立 Word 文件 WordDocument
  4. 使用 SaveAs 方法儲存 Word 文檔
  5. 使用 IronPDF 的 DocxToPdfRenderer 方法建立 PDF 文檔
  6. 使用 IronPrint 調整 PrinterSettings
  7. 使用 IronPrint 方法列印 Printer.Print

什麼是IronPrint?

IronPrint是一個高效的 .NET 列印庫,它使你能夠完全控制 C# 中的列印。 該軟體由 Iron Software 開發,提供專為列印任務設計的專用類別和方法,讓您可以微調列印過程的每個方面。 該程式庫與.NET Framework 和 .NET Core都能流暢地運行,因此您可以在任何類型的應用程式中使用它。

IronPrint的主要特點是什麼?

IronPrint 如何處理列印設定?

IronPrint 讓您可以自訂列印作業的各個方面:

  • 紙張尺寸(Letter、Legal、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)
$vbLabelText   $csharpLabel

印表機類別是如何運作的?

Printer 類別是 IronPrint 的核心。 它提供了列印各種文件類型(包括圖像和 PDF)的方法。 您可以將其整合到任何列印場景中,它甚至支援即時應用程式的列印對話方塊。 ShowPrintDialog 方法可在使用者需要時提供熟悉的列印配置選項。

IronPrint 支援哪些平台?

IronPrint 可在 Windows、macOS、Android 和 iOS 系統上運行,確保無論部署在哪裡,都能提供一致的列印功能。 這種跨平台支援擴展到了WPF、Windows Forms 和 ASP.NET 應用程式

需要哪些先決條件?

在開始之前,請確保您已準備好:

  1. Visual Studio:從官方網站下載並安裝。
  2. IronWord 庫:用於建立和操作 Word 文件。 透過 NuGet 或IronWord安裝。
  3. IronPDF 庫:用於 Word 轉 PDF。 從IronPDF獲取。
  4. IronPrint 庫:用於列印功能。 IronPrint有售。

如何建立、轉換和列印 Word 文件?

讓我們建立一個 C# 控制台應用程序,該應用程式會建立一個 Word 文檔,將其轉換為 PDF,並使用這三個庫將其列印出來。

步驟 1:在 Visual Studio 中建立一個 C# 控制台應用程式

1.開啟 Visual Studio 並建立新的 C# Console Application。

  1. 配置項目,然後按一下"下一步"。
  2. 從"附加資訊"中選擇您的 .NET Framework,然後按一下"建立"。

步驟 2:透過 NuGet 套件管理器安裝必要的庫

  1. 從"工具"功能表開啟 NuGet 套件管理器控制台。
  2. 在瀏覽標籤中,搜尋每個庫並點擊安裝。
  3. 使用以下指令安裝 IronPrint

    Install-Package IronPrint
  4. 以相同的方式安裝 IronWord 和 IronPDF。 對於控制台,請使用:

    Install-Package IronWord
    Install-Package IronPdf
    Install-Package IronWord
    Install-Package IronPdf
    SHELL

步驟 3:使用 IronWord 建立 Word 文件

我們先用IronWord建立一個簡單的 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")
$vbLabelText   $csharpLabel

事情經過是這樣的:

  • 我們用我們的文本創建了一個 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")
$vbLabelText   $csharpLabel

輸出 Word 文件

使用 IronWord 建立的輸出 Word 文檔,顯示格式化的文字內容 - document.docx 文件在 Microsoft 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")
$vbLabelText   $csharpLabel

過程很簡單:

  • 建立一個 DocxToPdfRenderer 將 Word 文件渲染為 PDF
  • 保存結果

步驟 5:使用 IronPrint 列印 PDF 文件

最後,讓我們用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)
$vbLabelText   $csharpLabel

此代碼:

  • 取得可用印表機,代碼為 Printer.GetPrinterNames()
  • 選擇特定印表機
  • 設定紙張尺寸和頁邊距。 列印PDF文件

列印預覽顯示轉換後的 PDF 文件已準備好列印 - IronPrint 輸出顯示格式和邊距正確的 word.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
$vbLabelText   $csharpLabel

列印選項如何提升功能性?

Printer 類別可處理多種文件類型,包括 PDF、PNG、JPG、TIFF 和 BMP。這種多功能性意味著您可以列印不同的內容類型而無需改變您的方法。

我可以部署到哪些平台?

IronPrint 可在 Windows、Android、iOS 和 macOS 上運作。 您的列印程式碼在所有平台上都能穩定運行,部署起來非常簡單。

哪些列印設定可以自訂?

透過 PrintSettings 類,您可以控制:

紙張尺寸和方向

  • DPI 和列印品質
  • 影印與整理
  • 頁邊距和佈局 雙面列印
  • 自訂頁面範圍

IronPrint 如何與其他函式庫整合?

IronPrint 可以與IronBarcode和 IronPDF 等其他 Iron Software 產品無縫協作。 一致的 API 設計使得在一個工作流程中輕鬆建立、轉換和列印文件。

為什麼說該 API 易於使用?

IronPrint 直覺的方法名稱和完整的 IntelliSense 支援使其易於所有開發人員使用。 您可以快速添加列印功能,無需經歷陡峭的學習曲線。

有哪些支援資源可用?

Iron Software 提供完整的文件、範例、API 參考和最佳實踐。 他們的支援團隊可以幫助您有效地實現列印功能。

IronPrint 如何提高對列印的控制?

IronPrint 讓您能夠精確控制列印的各個方面。 設定精確的紙張尺寸、邊距和參數,以確保輸出符合特定要求。 監控印表機狀態並處理錯誤,以實現可靠的列印作業管理。

接下來的步驟是什麼?

現在,您已擁有在 C# 應用程式中建立 Word 文件、將其轉換為 PDF 並列印所需的一切。 IronWordIronPDFIronPrint協同工作,提供完整的文件處理解決方案。 無論您是建立 Web 應用程式、行動應用程式、桌面應用程式還是控制台應用程序,這些工具都能簡化您的文件工作流程。

有關更多列印技巧,請造訪文件頁面。 探索大量列印和自訂列印處理器等功能,以提升應用程式的效能。

IronPrint許可證從 $799 開始。 立即下載該庫,為您的 C# 應用程式添加專業列印功能。

常見問題解答

如何在 C# 中打印 Word 文檔而不丟失格式?

要在 C# 中打印 Word 文檔,同時保持其格式,使用 IronWord 創建文檔,使用 IronPDF 將其轉換為 PDF,然後用 IronPrint 打印。這確保了文檔的格式在整個過程中得以保留。

使用 IronPrint 在 C# 中打印文檔有什麼好處?

IronPrint 提供異步打印、可定制的設置(如紙張大小和方向)、跨平台兼容性及與 Iron Software 的其他庫的無縫集成,為 C# 環境中的打印任務提供了一個強大的解決方案。

如何將 IronWord、IronPDF 和 IronPrint 集成到 C# 項目中?

要將這些庫集成到 C# 項目中,在 Visual Studio 的 NuGet 包管理器控制台中安裝它們。使用 Install-Package IronWordInstall-Package IronPDFInstall-Package IronPrint 添加文檔創建、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 進行最終的打印過程。這種組合確保了高質量的輸出和易用性。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。

鋼鐵支援團隊

我們每週 5 天,每天 24 小時在線上。
聊天
電子郵件
打電話給我