IRONWORDの使用

C# Print Wordチュートリアル:ステップバイステップガイド

このチュートリアルでは、C#(シーシャープ)コンソールアプリケーションでMicrosoft Interopを使用してWord文書を印刷する方法について説明します。 この初心者向けガイドは、Microsoft Word文書をプログラムで印刷する手順を説明します。

前提条件 ##

コードに飛び込む前に、いくつかのことをセットアップしておくことが不可欠だ:

Microsoft Wordのインストール:Microsoft Wordがシステムにインストールされていることを確認してください。 そうでない場合は、お使いのコンピューターのマイクロソフト公式ウェブサイトかアプリストアにアクセスしてインストールしてください。

Visual Studioのセットアップ:コンソールアプリケーションを作成できるVisual Studioがインストールされている必要があります。 初心者の方は、無料で私たちのニーズに十分なVisual Studio Communityのダウンロードを検討してください。

ワード文書:テスト用にWord文書のサンプルを用意してください。 これが印刷所に送る書類になる。

環境の設定 ##

新しいコンソールアプリケーションを作成

  1. Visual Studioを開きます。

  2. 新規プロジェクトの作成」をクリックする。

  3. Console App "を検索し、適切なC#テンプレート(シーシャープ)を選択します。

  4. プロジェクトに名前を付け(例:「InteropPrintConsoleTutorial」)、適切な場所を選択します。

相互運用参照の追加 ###

Interopを使用するには、Microsoft Office Interopライブラリを参照する必要があります。 追加する方法はこうだ:

  1. Visual StudioのソリューションエクスプローラーでConsoleプロジェクトを右クリックします。

  2. Add > Referenceに移動する。

  3. リファレンス・マネージャのウィンドウで、COM タブを開きます。

  4. 検索バーに「Microsoft Word」と入力してリストを絞り込む。

  5. 結果から、Microsoft Word xx.x Object Library(xx.xはバージョン番号を表します)を選択します。

  6. OKボタンをクリックして参照を追加する。

    NuGetパッケージ・マネージャーを使ってインストールすることもできる。

    NuGet パッケージマネージャーを使用して `Microsoft.Office.Interop.Word` ライブラリをインストールすることもできます。

アプリケーション設定の確認 ###

アプリケーションのターゲット・フレームワークがInteropライブラリと互換性があることを確認してください。 ソリューションエクスプローラーでプロジェクトを右クリックし、プロパティを選択し、アプリケーションタブでターゲットフレームワークを表示することで確認できます。 Interop ライブラリのバージョンに問題がある場合は、必要なパッケージまたはアセンブリをダウンロードするか、ターゲット・フレームワークのバージョンを調整することを検討してください。

環境がセットアップされたので、コーディング作業を進めることができる。

ドキュメントオブジェクトを理解する

ドキュメント・オブジェクトは、Word文書を扱う際のInteropサービスの中核をなすものです。 このオブジェクトはMicrosoft Word文書を表し、そのすべての機能を提供します。

一般的なタスクは文書を開くことだ:

object oMissing = Type.Missing;
object fileName = @"C:\path_to_document\document.docx";
Word._Document wordDoc = wordApp.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
object oMissing = Type.Missing;
object fileName = @"C:\path_to_document\document.docx";
Word._Document wordDoc = wordApp.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
Dim oMissing As Object = Type.Missing
Dim fileName As Object = "C:\path_to_document\document.docx"
Dim wordDoc As Word._Document = wordApp.Documents.Open(fileName, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing)
$vbLabelText   $csharpLabel

refのoMissingを持つ複数のパラメータは大変に思えるかもしれないが、多数の引数(そのほとんどはオプション)を期待するOpenメソッドには不可欠なものである。

印刷機能の実装

環境が整い、ドキュメント・オブジェクトを理解したところで、いよいよWord文書を印刷するコア機能に飛び込みましょう。

Word文書の基本印刷 ###

文書を印刷するには、以下の方法を使用できます:

private void ButtonPrint_Click(object sender, EventArgs e)
{
    wordDoc.PrintOut();
}
private void ButtonPrint_Click(object sender, EventArgs e)
{
    wordDoc.PrintOut();
}
Private Sub ButtonPrint_Click(ByVal sender As Object, ByVal e As EventArgs)
	wordDoc.PrintOut()
End Sub
$vbLabelText   $csharpLabel

このメソッドは、デフォルトの設定を使用して、ドキュメントをデフォルトのプリンタに送信します。

3.2 カスタマイズ付きのWordドキュメントを印刷する ###

印刷ダイアログを導入したり、プリンター設定をカスタマイズしたり、あるいは複数ページを印刷したい場合は、より詳細なアプローチが必要になる:

private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
    object copies = "1";
    object pages = "1-3"; // To print multiple pages, e.g., 1 to 3.

    wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
private void ButtonPrintWithSettings_Click(object sender, EventArgs e)
{
    object copies = "1";
    object pages = "1-3"; // To print multiple pages, e.g., 1 to 3.

    wordDoc.PrintOut(Copies: ref copies, Pages: ref pages);
}
Private Sub ButtonPrintWithSettings_Click(ByVal sender As Object, ByVal e As EventArgs)
	Dim copies As Object = "1"
	Dim pages As Object = "1-3" ' To print multiple pages, e.g., 1 to 3.

	wordDoc.PrintOut(Copies:= copies, Pages:= pages)
End Sub
$vbLabelText   $csharpLabel

上記のソースコードでは、ページ範囲と部数を指定しているが、カスタマイズの可能性は膨大である。

印刷設定のカスタマイズ ##

印刷設定を変更できることが、プログラム制御を際立たせている。 プリンター設定の調整、特定のプリンターの定義、あるいはドキュメントのサイレント印刷など、Interopならすべて思いのままです。

サイレント印刷

サイレント印刷とは、ユーザーの操作なしに文書をプリンターに送ることである:

object background = false;
wordDoc.PrintOut(Background: ref background);
object background = false;
wordDoc.PrintOut(Background: ref background);
Dim background As Object = False
wordDoc.PrintOut(Background:= background)
$vbLabelText   $csharpLabel

プリンターの指定 ###

デフォルト以外の特定のプリンターで文書を印刷する:

wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name"
wordDoc.PrintOut()
$vbLabelText   $csharpLabel

高度なプリンター設定

プリンターを指定するだけでなく、プリンターの設定を調整する必要があるかもしれない:

PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
    wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
    wordDoc.PrintOut();
}
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == DialogResult.OK)
{
    wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName;
    wordDoc.PrintOut();
}
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = DialogResult.OK Then
	wordApp.ActivePrinter = printDialog.PrinterSettings.PrinterName
	wordDoc.PrintOut()
End If
$vbLabelText   $csharpLabel

これにより、ユーザーは向きや両面印刷などの設定を手動で調整できる。

IronWordの紹介

Microsoft InteropはWord文書を管理する機能を提供しているが、本格的な商用利用には堅牢性と効率性に欠ける。 優れたWord DOCXファイル処理の代替としてIronWordを使用します。 IronWord はC# (シーシャープ)でExcelファイルのシームレスな読み書きを可能にします。 IronWordの始め方について詳しく学ぶ

IronXL for .NET: C# Excelライブラリ

結論 ##

このチュートリアルでは、Microsoft Interopを活用して、C#(シーシャープ)コンソールアプリケーションでWord文書をプログラムで印刷する手順を説明します。 印刷ダイアログの表示、カスタム印刷設定の設定、指定プリンタの選択やページ範囲の定義など、さまざまな印刷の制御方法について見てきた。 Interopは基本的な機能を提供しますが、IronWordのような強力な代替手段があることに注意する価値があります。

リーガン・パン
ソフトウェアエンジニア
レーガンはリーディング大学で電子工学の学士号を取得しました。Iron Softwareに入社する前の仕事では、一つのタスクに集中して取り組んでいました。Iron Softwareでは、営業、技術サポート、製品開発、マーケティングのいずれにおいても広範な業務に携わることが最も楽しいと感じています。彼は、Iron Softwareライブラリを開発者がどのように使用しているかを理解し、その知識を使ってドキュメントを継続的に改善し、製品を開発することを楽しんでいます。
< 以前
C#でWordドキュメントを開く