フッターコンテンツにスキップ
IRONPRINTの使用

C#でネットワークプリンターを使って印刷する方法

ネットワークプリンターでのプログラムによる印刷は、多くのソフトウェアアプリケーションで一般的な作業です。 デスクトップ、モバイルアプリケーション、またはウェブサービスを構築する場合でも、ネットワークプリンターに直接ドキュメントを送信する機能によって、ソフトウェアの使いやすさと効率が大幅に向上することがあります。 In this article, we'll explore how to print on a network printer using C# and IronPrint from Iron Software.

ネットワークプリンターでC#で印刷する方法

  1. IronPrintを使用してドキュメントの印刷のための新しいプロジェクトを作成します。
  2. 新しいプロジェクトにIronPrintをインストールします。
  3. ドキュメントを印刷するために使用可能なネットワークプリンターの一覧を表示します。
  4. IronPrintを使用してPDFドキュメントを印刷します。
  5. IronPrintを使用してダイアログを使用してPDFドキュメントを印刷します。
  6. 印刷設定を使用した高度な印刷。

環境の設定

コーディングの部分に入る前に、環境が正しく設定されていることを確認しましょう。 C#でネットワークプリンターに印刷するには、次のことが必要です:

  1. C#アプリケーションを実行するマシンからネットワークプリンターにアクセス可能であることを確認します。
  2. プリンターのネットワークアドレス (IPアドレスまたはネットワークプリンター名)。
  3. マシンにインストールされた必要なプリンタードライバー。
  4. プリンターやドライバーをインストールするための管理者権限が必要になる場合があります。

IronPrint

実装の詳細に進む前に、IronPrintの使用方法について知識を深めましょう。

  1. インストール: IronPrintはNuGetパッケージマネージャーを通じて簡単にインストールできます。 単にNuGetページを訪問し、インストール手順に従ってください。
  2. ドキュメント: クイックスタートガイドと包括的なAPIリファレンスについては公式ドキュメントを参照してください。

互換性とサポート: IronPrintは、さまざまな環境とプロジェクトタイプで広範な互換性とサポートを提供します。

  • .NETバージョンサポート: IronPrintはC#、VB.NET、F#をサポートしており、.NET 8,7,6,5、およびCore 3.1+を含みます。
  • オペレーティングシステムと環境: IronPrintはWindows (7+)、macOS (10+)、iOS (11+)、およびAndroid API 21+(v5 "Lollipop")と互換性があります。
  • プロジェクトタイプ: モバイル (Xamarin, MAUI, Avalonia)、デスクトップ (WPF, MAUI, Windows Avalonia)、またはコンソールアプリケーションを開発している場合でも、IronPrintがサポートしています。

それでは、次の例でIronPrintでの印刷方法を見てみましょう。

using IronPrint;

namespace IronPrintDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure printer setting
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 220;
            printSettings.NumberOfCopies = 10;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the document
            Printer.Print("awesomeIronPrint.pdf", printSettings);
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // Configure printer setting
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 220;
            printSettings.NumberOfCopies = 10;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the document
            Printer.Print("awesomeIronPrint.pdf", printSettings);
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			' Configure printer setting
			Dim printSettings As New PrintSettings()
			printSettings.Dpi = 220
			printSettings.NumberOfCopies = 10
			printSettings.PaperOrientation = PaperOrientation.Portrait

			' Print the document
			Printer.Print("awesomeIronPrint.pdf", printSettings)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

System.Printing

System.Printing名前空間は、C#でプリンター、印刷キュー、印刷サーバー、および印刷ジョブとプログラム的に対話するためのクラスとインターフェースを提供します。 この名前空間内の主なクラスとインターフェースには以下が含まれます:

  1. PrintQueue: システムに接続されたプリンターまたは印刷装置を表します。
  2. PrintServer: ネットワーク上の印刷サーバーを表します。
  3. PrintSystemJobInfo: 印刷ジョブのステータスやプロパティなどの情報を提供します。
  4. PrintTicket: 印刷ジョブの設定を表し、用紙サイズ、方向、および印刷品質を含みます。

始めるにあたってSystem.Printing: コードに入る前に、印刷がSystem.Printingでどのように機能するかの基本的な理解を持ちましょう。

  1. PrintQueueの選択: 使用したいプリンターを表すPrintQueueオブジェクトを識別する必要があります。
  2. PrintTicketの構成: オプションで、PrintTicketオブジェクトを構成して、印刷設定(用紙サイズ、方向、コピー数など)を指定することができます。
  3. ドキュメントの印刷: 最後に、選択したPrintQueueに印刷するドキュメントを送信します。

実装System.Printingでのドキュメント印刷: 次に、System.Printingを使用してドキュメントを印刷するプロセスを見てみましょう:

ここにソースコードがあります。

using System;
using System.IO;
using System.Printing;

class Program
{
    static void Main(string[] args)
    {
        // Specify the path to the document to be printed on the local printer
        string documentPath = "path/to/your/document.pdf";

        // Instantiate a Printer Server object representing the local print server
        using (PrintServer printServer = new PrintServer())
        {
            // Get the default PrintQueue from the PrintServer default 
            PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;

            // Create a PrintTicket object to specify print settings (optional)
            PrintTicket printTicket = new PrintTicket();

            // Configure print settings, e.g., number of copies
            printTicket.CopyCount = 1;

            // Print the document to the default PrintQueue with the specified PrintTicket
            defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
        }

        Console.WriteLine("Document sent to print queue.");
    }
}
using System;
using System.IO;
using System.Printing;

class Program
{
    static void Main(string[] args)
    {
        // Specify the path to the document to be printed on the local printer
        string documentPath = "path/to/your/document.pdf";

        // Instantiate a Printer Server object representing the local print server
        using (PrintServer printServer = new PrintServer())
        {
            // Get the default PrintQueue from the PrintServer default 
            PrintQueue defaultPrintQueue = printServer.DefaultPrintQueue;

            // Create a PrintTicket object to specify print settings (optional)
            PrintTicket printTicket = new PrintTicket();

            // Configure print settings, e.g., number of copies
            printTicket.CopyCount = 1;

            // Print the document to the default PrintQueue with the specified PrintTicket
            defaultPrintQueue.AddJob("MyPrintJob", documentPath, false, printTicket);
        }

        Console.WriteLine("Document sent to print queue.");
    }
}
Imports System
Imports System.IO
Imports System.Printing

Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Specify the path to the document to be printed on the local printer
		Dim documentPath As String = "path/to/your/document.pdf"

		' Instantiate a Printer Server object representing the local print server
		Using printServer As New PrintServer()
			' Get the default PrintQueue from the PrintServer default 
			Dim defaultPrintQueue As PrintQueue = printServer.DefaultPrintQueue

			' Create a PrintTicket object to specify print settings (optional)
			Dim printTicket As New PrintTicket()

			' Configure print settings, e.g., number of copies
			printTicket.CopyCount = 1

			' Print the document to the default PrintQueue with the specified PrintTicket
			defaultPrintQueue.AddJob("MyPrintJob", documentPath, False, printTicket)
		End Using

		Console.WriteLine("Document sent to print queue.")
	End Sub
End Class
$vbLabelText   $csharpLabel

IronPrint と System.Printingの比較

IronPrint vs System.Printingの簡単な比較表

この表では、IronPrintとSystem.Printingの機能と特徴の概要を示し、開発者が特定の要件とプラットフォームターゲットに基づいて情報に基づいた決定を下せるようにします。

ステップ1: IronPrintを使用したドキュメント印刷用の新しいプロジェクトを作成

以下のようにVisual Studioでコンソールアプリケーションを作成します。

ネットワークプリンターでC#での印刷: 図2 - コンソールアプリケーションの作成

プロジェクト名と場所を指定します。

ネットワークプリンターでC#での印刷: 図3 - プロジェクト名の指定

.NETバージョンを選択します。

ネットワークプリンターでC#での印刷: 図4 - 適切な.NETバージョンを選択

今、プロジェクトは作成され、さらにコーディングの準備が整っています。

ステップ2: 新しいプロジェクトにIronPrintをインストール

下記のようにVisual StudioパッケージマネージャーからIronPrintをインストールします。

ネットワークプリンターでC#での印刷: 図5 - Visual Studioパッケージマネージャーを使ってIronPrintを検索

IronPrintは以下のコマンドを使ってもインストールできます。

Install-Package IronPrint

ステップ3: ドキュメントを印刷するために使用可能なネットワークプリンターを一覧表示

IronPrintを使用してプリンター名を一覧表示するには、以下のコードを使用します。

using System;
using System.Collections.Generic;
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Get printer names using printer drivers
            List<string> printersName = Printer.GetPrinterNames();
            foreach (var printer in printersName)
            {
                Console.WriteLine(printer);
            }
        }
    }
}
using System;
using System.Collections.Generic;
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Get printer names using printer drivers
            List<string> printersName = Printer.GetPrinterNames();
            foreach (var printer in printersName)
            {
                Console.WriteLine(printer);
            }
        }
    }
}
Imports System
Imports System.Collections.Generic
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Get printer names using printer drivers
			Dim printersName As List(Of String) = Printer.GetPrinterNames()
			For Each printer In printersName
				Console.WriteLine(printer)
			Next printer
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

コードの説明

  1. 利用可能なすべてのプリンタードライバを取得するにはPrinter.GetPrinterNamesを使用します。
  2. Console.WriteLineを使用して名前を印刷します。

出力

ネットワークプリンターでC#での印刷: 図6 - 利用可能なプリンターの例の出力

ステップ4: IronPrintを使用してPDFドキュメントを印刷

以下のコードを使用してドキュメントを静かに印刷します:

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print the document silently
            Printer.Print("sample.pdf");
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print the document silently
            Printer.Print("sample.pdf");
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Print the document silently
			Printer.Print("sample.pdf")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

実行後、ドキュメントは出力に示されているように印刷キューに追加されます。

出力

ネットワークプリンターでC#での印刷: 図7 - 印刷キューに追加されるドキュメントの出力例

ステップ5: IronPrintを使用してダイアログでPDFドキュメントを印刷

ダイアログでドキュメントを印刷するには、以下のコードを使用します:

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print with Dialog
            Printer.ShowPrintDialog("sample.pdf");
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Print with Dialog
            Printer.ShowPrintDialog("sample.pdf");
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Print with Dialog
			Printer.ShowPrintDialog("sample.pdf")
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

コードの説明

  1. IronPrintのShowPrintDialogを使うことでダイアログで印刷できます。
  2. ダイアログが開くと、ドキュメントを印刷するための必要なプリンターを選択します。

出力

ネットワークプリンターでC#での印刷: 図8 - 正しいプリンターを選択する

ステップ6: 印刷設定を使用した高度な印刷

高度な設定でのドキュメント印刷はIronPrintでサポートされています。 以下のプロパティがサポートされています。

  1. PaperSize: プリンターによって使用される用紙の寸法を指定します。
  2. PaperOrientation: 用紙の方向を定義し、オート、ポートレート、またはランドスケープなどがあります。
  3. DPI: インチ当たりのドット数での印刷解像度を設定します。 デフォルトは300で、商業印刷で一般に使用されます。 実際のDPIはプリンターの能力によって制限されることがあります。
  4. NumberOfCopies: ドキュメントの同一コピーの数を示します。
  5. PrinterName: 印刷タスクに指定されたプリンターの名前を指定します。
  6. PaperMargins: ミリメートル単位での印刷のマージンを決定します。
  7. Grayscale: グレースケールで印刷するかどうかを決定するブール値。 デフォルトはfalseです。

それでは、コード例を見てみましょう。

using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Configure custom print settings
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the required document
            Printer.Print("sample.pdf", printSettings);
        }
    }
}
using IronPrint;

namespace IronPrintDemo
{
    public class Program
    {
        public static void Main()
        {
            // Configure custom print settings
            PrintSettings printSettings = new PrintSettings();
            printSettings.Dpi = 150;
            printSettings.NumberOfCopies = 2;
            printSettings.PaperOrientation = PaperOrientation.Portrait;

            // Print the required document
            Printer.Print("sample.pdf", printSettings);
        }
    }
}
Imports IronPrint

Namespace IronPrintDemo
	Public Class Program
		Public Shared Sub Main()
			' Configure custom print settings
			Dim printSettings As New PrintSettings()
			printSettings.Dpi = 150
			printSettings.NumberOfCopies = 2
			printSettings.PaperOrientation = PaperOrientation.Portrait

			' Print the required document
			Printer.Print("sample.pdf", printSettings)
		End Sub
	End Class
End Namespace
$vbLabelText   $csharpLabel

コードの説明

  1. コードは、印刷に必要なクラスとメソッドを含む名前空間IronPrintをインポートすることから始まります。
  2. IronPrintDemo名前空間の内部で、Programというクラスがpublicとして宣言されています。
  3. メインメソッドは、プログラムのエントリポイントとして機能します。
  4. メインメソッドの中で:
    • PrintSettingsオブジェクトのprintSettingsは、カスタム印刷設定を構成するためにインスタンス化されます。
    • printSettingsPrintResolutionプロパティは150に設定されており、150 DPIの印刷解像度を意味します。
    • ドキュメントの2つの同一コピーを印刷することを指定するために、NumberOfCopiesプロパティが2に設定されます。
    • PrintPageOrientationプロパティがPrintPageOrientation.Portraitに設定されており、ドキュメントが縦向きで印刷されることを示します。
  5. 最後に、Printer.Printメソッドがパラメータ"sample.pdf"(印刷するドキュメントの名前)とprintSettings(カスタム印刷設定)で呼び出されます。 このメソッドは指定された設定を使用して印刷プロセスを処理します。

出力

ネットワークプリンターでC#での印刷: 図9 - 印刷用に印刷キューに追加されたPDFドキュメントの出力例

ライセンス

IronPrint from Iron Softwareの企業ライブラリであり、実行するにはライセンスが必要です。 IronPrint ライセンスキーを追加すると、プロジェクトが制限や透かしなしでライブになります。 Buy a license here or sign up for a free 30-day trial key ここで30日間の無料トライアルキーにサインアップしてください。

ライセンスキーを取得後、アプリケーション内のappSettings.jsonファイルにキーを配置する必要があります

{
  "IronPrint.License.LicenseKey": "IRONPRINT.KEY"
}

これにより、制限や透かしなしでドキュメントを印刷できます。

結論

要約すると、IronPrintとSystem.Printingにはそれぞれの強みがあり、異なるシナリオに適しています。 IronPrintはシンプルさと汎用性に焦点を当て、クロスプラットフォームのソリューションを提供する一方で、System.Printingは特にWindowsベースのアプリケーションにおいて、ネイティブインテグレーションと詳細な制御を提供します。 開発者は、C#プロジェクト向けの印刷ライブラリを選ぶ際、特定の要件とプラットフォームターゲットを考慮するべきです。

IronPrintは、.NETアプリケーション内でのドキュメント印刷を、ユーザーフレンドリーなAPIとさまざまなプラットフォームおよびプロジェクトタイプにおける広範な互換性によって簡単にします。この記事で概説した手順に従うことで、印刷機能を.NETプロジェクトにシームレスに統合し、使いやすさと生産性を向上できます。 IronPrintの可能性を探ることで、アプリケーション内で効率的なドキュメント印刷の世界を開きましょう。

よくある質問

C#でネットワークプリンターで印刷するにはどうすればよいですか?

C#でネットワークプリンターでドキュメントを印刷するためにIronPrintを使用できます。プログラム的に印刷タスクを処理するメソッドを使用して、ドキュメントを直接ネットワークプリンターに送信します。

C#でネットワークプリンターで印刷を設定する手順は何ですか?

まず、ネットワークプリンターへのアクセスとそのアドレスを確認します。必要なドライバーとIronPrintをNuGetパッケージマネージャーを通じてインストールします。その後、IronPrintのメソッドを使用して印刷ジョブを管理して送信します。

C#プロジェクトにIronPrintをインストールするにはどうすればよいですか?

'Install-Package IronPrint'コマンドをパッケージマネージャーコンソールで実行するか、NuGetパッケージマネージャーを使用してインストールできます。

IronPrintはどの.NETバージョンに対応していますか?

IronPrintはC#、VB.NET、F#をサポートし、.NET 8、7、6、5、およびCore 3.1+に対応しています。

IronPrintは異なるオペレーティングシステムで使用できますか?

はい、IronPrintはWindows (7+)、macOS (10+)、iOS (11+)、およびAndroid API 21+ (v5 'Lollipop') に対応しています。

IronPrintはC#のSystem.Printingとどう違いますか?

IronPrintはシンプルさに重点を置いたクロスプラットフォームソリューションを提供し、System.PrintingはWindowsベースのアプリケーションのためのネイティブ統合と制御を提供します。

IronPrintを使用して利用可能なネットワークプリンターをリストするにはどうすればよいですか?

IronPrintのメソッドPrinter.GetPrinterNames()を使用すると、利用可能なプリンタードライバーのリストを取得できます。

IronPrintで利用可能な高度な印刷オプションは何ですか?

IronPrintは、PaperSize、PaperOrientation、DPI、NumberOfCopies、PrinterName、PaperMargins、およびGrayscaleなどの高度な設定をサポートしています。

IronPrintのライセンスオプションは何がありますか?

IronPrintのライセンスは購入するか、Iron Softwareのウェブサイトで無料の30日間トライアルにサインアップできます。ライセンスキーはアプリケーションのappSettings.jsonファイルに配置されます。

コマンドライン経由でIronPrintをインストールする際のコマンドは何ですか?

コマンドライン経由でIronPrintをインストールするには、コマンド Install-Package IronPrint を使用します。

Curtis Chau
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。