0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

C#でWord文書を印刷する

Posted at

デジタル時代となり、電子文書やクラウドドライブを使ってファイルを保存・転送する人が増えているが、それでも印刷は日々の仕事や生活に欠かせないものだ。この記事では、サードパーティ製の無料の.NET Wordライブラリを使用して、C#でWord文書をプログラムで印刷する方法を紹介します。

Word文書を印刷するための無料C#ライブラリ

始める前に、以下のリンクから無料のライブラリ – Free Spire.Doc for .NETをダウンロードするか、Nuget経由で直接インストールする必要があります。

C#でWord文書を印刷し、印刷設定を指定する

Free Spire.Doc for .NETライブラリが提供する PrintDocument クラスを通して、Word文書を特定のプリンタで印刷したり、ページ範囲、部数、両面印刷、用紙サイズなどの印刷設定を指定することができます。

サンプルC#コード:

using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordDocument
{
    class Program
    {
        static void Main(string[] args)
        {
            // Word文書を読み込む
            Document doc = new Document();
            doc.LoadFromFile("input.docx");

            // PrintDocumentオブジェクトの取得
            PrintDocument printDoc = doc.PrintDocument;

            // プリンタ名を指定する
            printDoc.PrinterSettings.PrinterName = "your printer name";

            // 印刷するページの範囲を指定する
            printDoc.PrinterSettings.FromPage = 1;
            printDoc.PrinterSettings.ToPage = 5;

            // 印刷部数の設定
            printDoc.PrinterSettings.Copies = 1;

            // 文書を印刷する
            printDoc.Print();
        }
    }
}

C#でWordをPDFに印刷する

この無料ライブラリーは、Microsoft Print to PDFやMicrosoft XPS Document Writerなどの仮想プリンターを使ったWord文書の印刷もサポートしている。

サンプルC#コード:

using Spire.Doc;
using System.Drawing.Printing;

namespace PrintWordToPdf
{
    class Program
    {
        static void Main(string[] args)
        {
            // Word文書を読み込む
            Document doc = new Document();
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.docx");

            // PrintDocumentオブジェクトの取得
            PrintDocument printDoc = doc.PrintDocument;

            // ファイルに印刷
            printDoc.PrinterSettings.PrintToFile = true;

            // プリンタ名を 「Microsoft Print to PDF 」と指定する。
            printDoc.PrinterSettings.PrinterName = "Microsoft Print to PDF";

            // 出力ファイルのパスと名前を指定する
            printDoc.PrinterSettings.PrintFileName = "PrintToPDF.pdf";

            // 文書を印刷する
            printDoc.Print();
        }
    }
}

C#で1枚のシートに複数ページを印刷する

特定の要件を満たすために複数のページを1枚の用紙に印刷したい場合は、 Doucment.PrintMultipageToOneSheet() メソッドを呼び出すことができます。

サンプルC#コード:

using Spire.Doc;
using Spire.Doc.Printing;
using System.Drawing.Printing;

namespace PrintMultiplePagesOnOneSheet
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Word文書を読み込む
            Document doc = new Document();
            doc.LoadFromFile(@"C:\\Users\\Administrator\\Desktop\\input.docx");

            // PrintDocumentオブジェクトの取得
            PrintDocument printDoc = doc.PrintDocument;

            // 片面印刷を有効にする
            printDoc.PrinterSettings.Duplex = Duplex.Simplex;

            // 1枚の用紙に2ページを印刷する
            doc.PrintMultipageToOneSheet(PagesPreSheet.TwoPages, false);
            // 1枚の用紙に4ページを印刷する
            //doc.PrintMultipageToOneSheet(PagesPreSheet.FourPages, false);
        }
    }
}

注:この機能は.NET Framework 5.0以上には適用されません。


上記3つの印刷方法に加え、無料ライブラリーは以下の印刷方法にも対応している:

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?