跳過到頁腳內容
使用 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);
$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# 控制台應用程式。
  2. 配置項目,然後按一下"下一步"。
  3. 從"附加資訊"中選擇您的.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");
$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");
$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");
$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);
$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!");
}
$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 小時在線上。
聊天
電子郵件
打電話給我