IRONWORDの使用

C# で Word を印刷する(開発者向けチュートリアル)

更新済み 10月 30, 2023
共有:

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

前提条件

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

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

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

ワード文書:テスト用に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)
VB   C#

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

印刷機能の実装

環境が整い、ドキュメント・オブジェクトを理解したところで、いよいよ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
VB   C#

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

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
VB   C#

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

印刷設定のカスタマイズ

印刷設定を変更できることが、プログラム制御を際立たせている。 プリンター設定の調整、特定のプリンターの定義、あるいはドキュメントのサイレント印刷など、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)
VB   C#

プリンタの指定

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

wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name";
wordDoc.PrintOut();
wordApp.ActivePrinter = "Printer Name"
wordDoc.PrintOut()
VB   C#

プリンタの詳細設定

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

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
VB   C#

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

アイアンワードの紹介

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

IronXL for .NET (.NET用アイアンXL):C#(シーシャープ)エクセルライブラリ

結論

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

< 以前
C#でWordドキュメントを開く

準備はできましたか? バージョン: 2024.9 新発売

無料のNuGetダウンロード 総ダウンロード数: 4,489 View Licenses >