使用 IRONPRINT 如何在 C# 中列印 Word 文件 Curtis Chau 更新:2025年10月16日 下載 IronPrint NuGet 下載 開始免費試用 法學碩士副本 法學碩士副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 若要在 C# 中列印 Word 文檔,可以使用 IronWord 建立文檔,使用 IronPDF 將其轉換為 PDF 格式,然後使用 IronPrint 處理列印過程,並可在多個平台上進行自訂設定。 在建立 C# 應用程式時,您經常需要以程式設計方式產生和列印 Word 文件。 無論您是建立報告、處理文件還是製作專業成果,擁有可靠的工具都至關重要。 這時,Iron Software 的IronWord 、 IronPDF和IronPrint就派上了用場——這些庫協同工作,簡化了 C# 應用程式中的文件創建、轉換和列印。 本文將指導您如何使用 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是一個高效的 .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 應用程式。 需要哪些先決條件? 在開始之前,請確保您已準備好: Visual Studio:從官方網站下載並安裝。 IronWord 庫:用於建立和操作 Word 文件。 透過 NuGet 或IronWord安裝。 IronPDF 庫:用於 Word 轉 PDF。 從IronPDF獲取。 IronPrint 庫:用於列印功能。 可在IronPrint購買。 如何建立、轉換和列印 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 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 文件 步驟 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 並列印所需的一切。 IronWord 、 IronPDF和IronPrint協同工作,提供完整的文件處理解決方案。 無論您是建立 Web 應用程式、行動應用程式、桌面應用程式還是控制台應用程序,這些工具都能簡化您的文件工作流程。 有關更多列印技巧,請造訪文件頁面。 探索大量列印和自訂列印處理器等功能,以提升應用程式的效能。 IronPrint許可證起價為$799 。 立即下載該庫,為您的 C# 應用程式添加專業列印功能。 常見問題解答 如何在 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 來完成最後的列印程序。這種組合可確保高品質的輸出和易用性。 Curtis Chau 立即與工程團隊聊天 技術撰稿人 Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。 相關文章 更新2025年12月19日 用 IronPrint for .NET 打印 PDF 而無需打開 Adobe VB.NET 列印 PDF 教學:使用 IronPrint for .NET 在您的 .NET 應用程式中實作 PDF 列印。靜音列印、對話方選項、自訂設定。 閱讀更多 更新2025年10月19日 如何使用 IronPrint for .NET 在 VB.NET 中打印 PDF VB.NET 列印 PDF 教學:使用 IronPrint for .NET 在您的 .NET 應用程式中實作 PDF 列印。靜音列印、對話方選項、自訂設定。 閱讀更多 更新2025年10月29日 C# 程式化列印 PDF (程式碼範例教學) 在應用程式中需要列印至 PDF 檔案功能的使用個案有很多。 閱讀更多 如何在 C# 中列印 QR 碼.NET PDF 印表機 (開發人員教程)
更新2025年12月19日 用 IronPrint for .NET 打印 PDF 而無需打開 Adobe VB.NET 列印 PDF 教學:使用 IronPrint for .NET 在您的 .NET 應用程式中實作 PDF 列印。靜音列印、對話方選項、自訂設定。 閱讀更多
更新2025年10月19日 如何使用 IronPrint for .NET 在 VB.NET 中打印 PDF VB.NET 列印 PDF 教學:使用 IronPrint for .NET 在您的 .NET 應用程式中實作 PDF 列印。靜音列印、對話方選項、自訂設定。 閱讀更多