IRONPRINTの使用

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

チャクニット・ビン
チャクニット・ビン
2024年6月6日
共有:

イントロダクション

ネットワークプリンターでのプログラム印刷は、様々なソフトウェアアプリケーションで共通のタスクである。 デスクトップ、モバイルアプリケーション、ウェブサービスのいずれを構築する場合でも、ドキュメントをネットワークプリンターに直接送信する機能は、ソフトウェアの使いやすさと効率を大幅に向上させます。 この記事では、C#とIronPrintを使用してネットワークプリンターで印刷する方法を、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;
// 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;
// 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
' Configure printer setting
Private printSettings As New PrintSettings()
printSettings.Dpi = 220
printSettings.NumberOfCopies = 10
printSettings.PaperOrientation = PaperOrientation.Portrait
' Print the document
Printer.Print("awesomeIronPrint.pdf", printSettings)
$vbLabelText   $csharpLabel

System.Printing

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

  1. PrintQueue:システムに接続されたプリンターまたは印刷デバイスを表します。

  2. PrintServer: ネットワーク上のプリントサーバーを表します。

  3. PrintSystemJobInfo: 印刷ジョブのステータスやプロパティなど、印刷ジョブに関する情報を提供します。

  4. PrintTicket: 印刷ジョブの設定を表し、用紙サイズ、向き、印刷品質を含みます。

    System.Printingの使い始め: コードに入る前に、System.Printingを使用した印刷の仕組みについて基本的な理解を得ておきましょう。

  5. PrintQueueの選択: 印刷に使用したいプリンターを表すPrintQueueオブジェクトを特定する必要があります。

  6. PrintTicketの設定: 必要に応じて、PrintTicketオブジェクトを設定して、用紙サイズ、印刷方向、コピー数などの印刷設定を指定することができます。

  7. ドキュメント印刷: 最後に、選択された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, paper size, orientation
            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, paper size, orientation
            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, paper size, orientation
			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 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 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 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. グレースケール: グレースケールで印刷するかどうかを決定するブール値。 デフォルトはfalseです。

    では、コード例を見てみよう:

using IronPrint;
namespace IronPrintDemo;
public class Program
{
    public static void Main()
    {
        // Configure custom print setting
        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 setting
        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 setting
			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オブジェクトは、カスタム印刷設定を構成するためにインスタンス化されます。

    • printSettings の Dpi プロパティが 150 に設定されており、これは 1 インチあたり 150 ドットの印刷解像度を示しています。

    • printSettingsNumberOfCopiesプロパティは2に設定されており、ドキュメントの同一のコピーが2部印刷されることを指定しています。

    • printSettingsPaperOrientation プロパティは PaperOrientation.Portrait に設定されています。これは、ドキュメントが縦向きで印刷されることを示しています。
  5. 最後にプリンター。 印刷メソッドは、パラメーター "sample.pdf"(印刷するドキュメントの名前)およびprintSettings(カスタム印刷設定)と共に呼び出されます。 このメソッドは、おそらく指定された設定を使用して印刷プロセスを処理する。

出力

ネットワークプリンターで印刷する方法(C#の場合):図9 - 印刷キューに追加されたPDFドキュメントの例出力

ライセンス

IronPrintは、Iron Softwareから提供されるエンタープライズライブラリであり、実行にはライセンスが必要です。 IronPrintのライセンスキーを追加することで、プロジェクトは制限や透かしなしで実行できるようになります。 こちらでライセンスを購入するか、こちらで無料の30日間試用キーを登録してください。

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

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

これは、制限や透かしなしで文書を印刷します。

結論

要約すると、IronPrintとSystem.Printingはそれぞれに強みがあり、異なるシナリオに適しています。 IronPrintは、シンプルさと多様性に焦点を当てた効率的でクロスプラットフォームのソリューションを提供します。一方、System.Printingは、特にWindowsベースのアプリケーションにおいて、ネイティブ統合と詳細な制御を提供します。 開発者は、C#(シーシャープ)プロジェクトでこれらの印刷ライブラリのいずれかを選択する際に、特定の要件とプラットフォームターゲットを考慮する必要があります。

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

チャクニット・ビン
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeで作業しています。彼はC#と.NETに深い専門知識を持ち、ソフトウェアの改善と顧客サポートを支援しています。ユーザーとの対話から得た彼の洞察は、より良い製品、文書、および全体的な体験に貢献しています。
次へ >
C#でQRコードを印刷する方法