
How to Print with Network Printer in C#
ネットワーク プリンターでのプログラムによる印刷は、さまざまなソフトウェア アプリケーションで共通のタスクです。 デスクトップ、モバイル アプリケーション、Web サービスなどを構築する場合でも、ドキュメントをネットワーク プリンターに直接送信できれば、ソフトウェアの使いやすさと効率性が大幅に向上します。 この記事では、C# とIron SoftwareのIronPrintを使用してネットワーク プリンターに印刷する方法について説明します。
How to Print with Network Printer in C#
- IronPrintを使用してドキュメントを印刷するための新しいプロジェクトを作成します。
- 新しいプロジェクトにIronPrintをインストールします。
- ドキュメントの印刷に使用できるネットワーク プリンターを一覧表示します。
- IronPrintを使用して PDF ドキュメントを印刷します。
- IronPrint with Dialog を使用して PDF ドキュメントを印刷します。
- 印刷設定による高度な印刷。
環境の設定
コーディング部分に進む前に、環境が正しく設定されていることを確認しましょう。 C# でネットワーク プリンターに印刷するには、次のものが必要です。
- C# アプリケーションが実行されるマシンからネットワーク プリンターにアクセスできることを確認します。
- プリンターのネットワーク アドレス (IP アドレスまたはネットワーク プリンター名)。
- 必要なプリンタードライバーがマシンにインストールされている。
- 必要に応じてプリンターまたはドライバーをインストールするための管理者権限。
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);
}
}
}Imports IronPrint
Module IronPrintDemo
Sub Main()
' 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 ModuleImports IronPrint
Module IronPrintDemo
Sub Main()
' 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 ModuleSystem.Printing
C#のSystem.Printing名前空間には、開発者がプリンタ、印刷キュー、印刷サーバ、印刷ジョブとプログラム的にやりとりするためのクラスとインターフェースが含まれています。 この名前空間内の主要なクラスとインターフェースには次のものがあります。
PrintQueue: システムに接続されたプリンタまたは印刷デバイスを表します。PrintServer: ネットワーク上の印刷サーバを表します。PrintSystemJobInfo: 印刷ジョブの状態やプロパティなどの情報を提供します。PrintTicket: 用紙サイズ、向き、印刷品質を含む印刷ジョブの設定を表します。
System.Printingの使い始め: コードに進む前に、System.Printingを用いた印刷の基本的な理解があるか確認しましょう。
PrintQueue選択: 使用したいプリンタを表すPrintQueueオブジェクトを特定する必要があります。PrintTicket設定: 必要に応じて、印刷設定(用紙サイズ、向き、部数など)を指定するPrintTicketオブジェクトを設定できます。- ドキュメント印刷: 最後に、選択した
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 ClassImports 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 ClassIronPrint対System.Printing

この表はIronPrintとSystem.Printingの機能と特性の概要を提供し、開発者が特定の要件やプラットフォームの目標に基づいて情報に基づいた決定を下すのに役立ちます。
ステップ1: IronPrintを使用してドキュメントを印刷するための新しいプロジェクトを作成する
以下に示すように、Visual Studio でコンソール アプリケーションを作成します。

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

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

これでプロジェクトが作成され、さらにコーディングする準備が整いました。
ステップ2: 新しいプロジェクトにIronPrintをインストールする
以下に示すように、Visual Studio パッケージ マネージャーから IronPrint をインストールします。

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 NamespaceImports 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コードの説明
Printer.GetPrinterNamesを使用して、利用可能なすべてのプリンタドライバを取得します。Console.WriteLineを使用して名前を印刷します。
出力

ステップ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 NamespaceImports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Print the document silently
Printer.Print("sample.pdf")
End Sub
End Class
End Namespace実行後、出力に示すように、ドキュメントは印刷キューに追加されます。
出力

ステップ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 NamespaceImports IronPrint
Namespace IronPrintDemo
Public Class Program
Public Shared Sub Main()
' Print with Dialog
Printer.ShowPrintDialog("sample.pdf")
End Sub
End Class
End Namespaceコードの説明
- IronPrintからの
ShowPrintDialogを使用して、ダイアログで印刷できます。 - ダイアログが開いたら、ドキュメントを印刷するために必要なプリンターを選択します。
出力

ステップ6: 印刷設定による高度な印刷
IronPrint では、詳細設定によるドキュメントの印刷がサポートされています。 次のプロパティをサポートします。
PaperSize: プリンタが利用する用紙の寸法を指定します。PaperOrientation: 用紙の向きを自動、縦置き、横置きなどで定義します。DPI: 希望の印刷解像度をインチ毎のドット数で設定します。 デフォルト値は 300 で、商業印刷でよく使用されます。 実際の DPI はプリンターの機能によって制限される場合があります。NumberOfCopies: ドキュメントの同一コピーを印刷する部数を示します。PrinterName: 印刷作業のために指定されたプリンタの名前を指定します。PaperMargins: 印刷のための余白をミリメートルで決定します。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 NamespaceImports 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コードの説明
- コードは、印刷に必要なクラスとメソッドが含まれる必要な名前空間 IronPrint をインポートすることから始まります。
IronPrintDemo名前空間の中には、Programという名前のクラスがpublicとして宣言されています。- Main メソッドはプログラムのエントリ ポイントとして機能します。
- Mainメソッド内:
- カスタム印刷設定を行うためにインスタンス化された
printSettingsという名前であります。 printSettingsは150として設定されており、150dpiの印刷解像度を示します。printSettingsは2として設定されており、2部の同一文書を印刷することを示しています。PaperOrientation.Portraitに設定されており、文書は縦置きの向きで印刷されることを示します。
- カスタム印刷設定を行うためにインスタンス化された
- 最後に、
printSettings(カスタム印刷設定)で呼び出されます。 このメソッドは、指定された設定を使用して印刷プロセスを処理します。
出力

ライセンス
Iron SoftwareのIronPrintはエンタープライズ ライブラリであり、実行するにはライセンスが必要です。 IronPrint ライセンス キーを追加すると、プロジェクトは制限や透かしなしで公開されます。 ここでライセンスを購入するか、ここで30 日間の無料試用キーにサインアップしてください。
ライセンスキーを取得したら、そのキーをアプリケーションのappSettings.jsonファイルに配置する必要があります。
{
"IronPrint.License.LicenseKey": "IRONPRINT.KEY"
}
これにより、制限や透かしなしでドキュメントが印刷されます。
結論
要約すると、IronPrintとSystem.Printingはそれぞれ異なるシナリオに適した強みを持っています。 IronPrintは、シンプルさと汎用性に焦点を当てた、ストリームライン化されたクロスプラットフォームソリューションを提供し、System.Printingは特にWindowsベースのアプリケーションに対してネイティブな統合と詳細な制御を提供します。 開発者は、C# プロジェクト用にこれらの印刷ライブラリを選択する際に、特定の要件とプラットフォーム ターゲットを考慮する必要があります。
IronPrintは、ユーザーフレンドリーなAPIと、様々なプラットフォームやプロジェクトタイプにわたる幅広い互換性を提供することで、.NETアプリケーション内でのドキュメント印刷を簡素化します。この記事で概説する手順に従うことで、印刷機能を.NETプロジェクトにシームレスに統合し、使いやすさと生産性を向上させることができます。 IronPrint の可能性を探り、アプリケーション内で効率的なドキュメント印刷の世界を実現しましょう。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。
関連する記事


