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

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

ネットワーク プリンターでのプログラムによる印刷は、さまざまなソフトウェア アプリケーションで共通のタスクです。 デスクトップ、モバイル アプリケーション、Web サービスなどを構築する場合でも、ドキュメントをネットワーク プリンターに直接送信できれば、ソフトウェアの使いやすさと効率性が大幅に向上します。 この記事では、C# とIron SoftwareIronPrintを使用してネットワーク プリンターに印刷する方法について説明します。

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

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

環境の設定

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

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

IronPrint

実装の詳細に入る前に、IronPrint の使用を開始する方法を確認しましょう。

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

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

  • .NET バージョンのサポート: IronPrint は、.NET 8、7、6、5、および Core 3.1+ を含む C#、VB.NET、および F# をサポートします。 *オペレーティング システムと環境*: 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

C# のSystem.Printing名前空間には、開発者がプリンター、印刷キュー、プリント サーバー、印刷ジョブをプログラムで操作できるようにするクラスとインターフェイスが含まれています。 この名前空間内の主要なクラスとインターフェースには次のものがあります。

  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比較

C#でネットワークプリンターを使って印刷する方法: 図1 - IronPrintと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 with Dialogを使用して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を使用すると、Dialog で印刷できます。
  2. ダイアログが開いたら、ドキュメントを印刷するために必要なプリンターを選択します。

出力

! C#でネットワークプリンターを使って印刷する方法: 図8 - 正しいプリンターを選択する

ステップ6: 印刷設定による高度な印刷

IronPrint では、詳細設定によるドキュメントの印刷がサポートされています。 次のプロパティをサポートします。

  1. PaperSize : プリンタが使用する用紙の寸法を指定します。
  2. PaperOrientation : 自動、縦、横など、用紙の向きを定義します。
  3. DPI : 希望する印刷解像度を 1 インチあたりのドット数で設定します。 デフォルト値は 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名前空間内に、public として宣言された Program という名前のクラスがあります。
  3. Main メソッドはプログラムのエントリ ポイントとして機能します。
  4. Mainメソッド内:
    • カスタム印刷設定を構成するために、 printSettingsという名前のPrintSettingsオブジェクトがインスタンス化されます。
    • printSettingsDpiプロパティは 150 に設定されており、印刷解像度が 150 ドット/インチであることを示します。
    • printSettingsNumberOfCopiesプロパティは 2 に設定され、ドキュメントの同一コピーが 2 部印刷されることを指定します。
    • printSettingsPaperOrientationプロパティがPaperOrientation.Portraitに設定され、ドキュメントが縦向きで印刷されることを示します。
  5. 最後に、パラメーター"sample.pdf"(印刷するドキュメントの名前) とprintSettings (カスタム印刷設定) を使用してPrinter.Printメソッドが呼び出されます。 このメソッドは、指定された設定を使用して印刷プロセスを処理します。

出力

! C#でネットワークプリンターを使って印刷する方法: 図9 - 印刷キューに追加されたPDF文書の出力例

ライセンス

Iron SoftwareIronPrintはエンタープライズ ライブラリであり、実行するにはライセンスが必要です。 IronPrint ライセンス キーを追加すると、プロジェクトは制限や透かしなしで公開されます。 ここでライセンスを購入するか、ここで30 日間の無料試用キーにサインアップしてください。

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

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

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

結論

要約すると、IronPrint とSystem.Printingはそれぞれ長所があり、異なるシナリオに適しています。 IronPrint は、シンプルさと汎用性を重視した合理化されたクロスプラットフォーム ソリューションを提供します。一方、 System.Printing 、特に Windows ベースのアプリケーション向けにネイティブ統合ときめ細かな制御を提供します。 開発者は、C# プロジェクト用にこれらの印刷ライブラリを選択する際に、特定の要件とプラットフォーム ターゲットを考慮する必要があります。

IronPrintは、ユーザーフレンドリーなAPIと、様々なプラットフォームやプロジェクトタイプにわたる幅広い互換性を提供することで、.NETアプリケーション内でのドキュメント印刷を簡素化します。この記事で概説する手順に従うことで、印刷機能を.NETプロジェクトにシームレスに統合し、使いやすさと生産性を向上させることができます。 IronPrint の可能性を探り、アプリケーション内で効率的なドキュメント印刷の世界を実現しましょう。

よくある質問

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

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

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

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

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

You can install IronPrint using the NuGet Package Manager in Visual Studio or by running the command Install-Package IronPrint in the Package Manager Console.

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

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