
C言語でWord文書を印刷する方法
C#でWord文書を印刷するには、 IronWordを使用して文書を作成し、 IronPDFを使用してPDF形式に変換し、 IronPrintを使用して複数のプラットフォームでカスタマイズ可能な設定で印刷処理を行います。
C#アプリケーションを開発する際、プログラムによってWord文書を生成・印刷する必要が生じる場面がよくあります。 レポート作成、文書処理、Professional成果物の作成など、どのような作業を行う場合でも、信頼できるツールがあれば大きな違いが生まれます。 そこで登場するのが、Iron Software社のIronWord 、 IronPDF 、 IronPrintです。これらのライブラリは連携して動作し、C#アプリケーションにおけるドキュメントの作成、変換、印刷を簡素化します。
この記事では、 IronPrintを使った印刷方法、 IronWordを使ったWord文書の作成方法、そしてIronPDFを使ったPDFへの変換方法について説明します。 Enterprise向けレポートシステムを構築する場合でも、文書ワークフローを自動化する場合でも、これらのツールは文書処理に必要なすべてを提供します。
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);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)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)プリンタクラスはどのように動作するのですか?
IronPrintの核となるのはPrinter クラスです。 画像やPDFなど、さまざまなファイル形式を印刷するための機能を提供します。 あらゆる印刷シナリオに統合でき、リアルタイムアプリケーション向けの印刷ダイアログもサポートしています。 ShowPrintDialog メソッドは、ユーザーが必要なときに使い慣れた印刷設定オプションを提供します。
IronPrintはどのプラットフォームをサポートしていますか?
IronPrintはWindows、macOS、Android、iOSに対応しており、どこに展開しても一貫した印刷機能を提供します。 このクロスプラットフォームサポートは、WPF、Windows Forms、およびASP.NETアプリケーションにも適用されます。
必要な前提条件は何ですか?
始める前に、以下のものを用意してください。
- Visual Studio:公式ウェブサイトからダウンロードしてインストールします。
- IronWordライブラリ: Wordファイルの作成と操作に使用します。 IronWordからまたはNuGetを通じてインストールします。
- IronPDFライブラリ: WordからPDFへの変換用。 IronPDFから入手してください。
- IronPrintライブラリ:印刷機能用。 IronPrintで入手可能です。
Word文書の作成、変換、印刷方法を教えてください
それでは、Word文書を作成し、それをPDFに変換し、3つのライブラリすべてを使用して印刷するC#コンソールアプリケーションを作成してみましょう。
ステップ1: Visual StudioでC#コンソールアプリケーションを作成
- Visual Studioを開き、新しいC#コンソールアプリケーションを作成します。
- プロジェクトを設定し、"次へ"をクリックします。
- "追加情報"から.NET Frameworkを選択し、"作成"をクリックします。
ステップ2: NuGetパッケージマネージャーを使用して必要なライブラリをインストールする
-
[ツール] メニューからNuGetパッケージ マネージャー コンソールを開きます。
-
[参照]タブで各ライブラリを検索し、[インストール]をクリックします。
-
以下のコマンドを使用してIronPrintをインストールします。
-
IronWordとIronPDFを同じ方法でインストールします。 コンソールでは、以下を使用してください。
Install-Package IronWord Install-Package IronPdfSHELL
ステップ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")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")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")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を印刷する
最後に、 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)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!");
}' Asynchronous printing example
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' Asynchronous printing example
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は、 IronBarcodeやIronPDFなどの他のIron Software製品とスムーズに連携します。 一貫性のあるAPI設計により、ドキュメントの作成、変換、印刷を単一のワークフローで簡単に行うことができます。
なぜこのAPIはユーザーフレンドリーだと考えられているのか?
IronPrintの直感的なメソッド名と完全なIntelliSense サポートにより、すべての開発者がアクセス可能です。 難しい学習を必要とせずに、印刷機能を迅速に追加できます。
どのような支援リソースが利用できますか?
Iron Softwareは、完全なドキュメント、サンプル、APIリファレンス、およびベストプラクティスを提供します。 彼らのサポートチームは、印刷機能を効果的に導入できるよう支援します。
IronPrintは印刷の制御性をどのように向上させるのか?
IronPrintを使えば、印刷のあらゆる側面を正確に制御できます。 出力が特定の要件を満たすように、用紙サイズ、余白、およびパラメーターを正確に設定してください。 プリンターの状態を監視し、エラーを処理することで、信頼性の高い印刷ジョブ管理を実現します。
次のステップは何ですか?
これで、C#アプリケーションでWord文書を作成し、PDFに変換し、印刷するために必要なものがすべて揃いました。 IronWord 、 IronPDF 、およびIronPrintは連携して、包括的な文書処理ソリューションを提供します。 ウェブ、モバイル、デスクトップ、コンソールなど、どのようなアプリケーションを開発する場合でも、これらのツールはドキュメントワークフローを簡素化します。
その他の印刷技術については、ドキュメントページをご覧ください。 バッチ印刷やカスタム印刷プロセッサなどの機能を活用して、アプリケーションの機能を向上させましょう。
IronPrintライセンスは$999から始まります。 ライブラリをダウンロードして、C#アプリケーションにProfessional印刷機能を今すぐ追加しましょう。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。
関連する記事


