IRONPRINTの使用

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

更新済み 6月 6, 2024
共有:

イントロダクション

ネットワークプリンターでのプログラム印刷は、様々なソフトウェアアプリケーションで共通のタスクである。 デスクトップ、モバイルアプリケーション、ウェブサービスのいずれを構築する場合でも、ドキュメントをネットワークプリンターに直接送信する機能は、ソフトウェアの使いやすさと効率を大幅に向上させます。 この記事では、C#(シーシャープ)を使ってネットワークプリンターで印刷する方法を探ります。 IronPrint から アイアンソフトウェア.

C#(シーシャープ)のネットワークプリンタで印刷する方法;

  1. を使用してドキュメントを印刷するための新規プロジェクトを作成します。 IronPrint.

  2. インストール IronPrint を新しいプロジェクトに加える。

  3. ドキュメントの印刷に使用できるネットワークプリンターのリスト。

  4. を使用してPDF文書を印刷します。 IronPrint.

  5. を使用してPDF文書を印刷します。 IronPrint ダイアログ付き。

  6. 印刷設定で印刷を進める。

環境の設定

コーディングに入る前に、環境が正しくセットアップされていることを確認しよう。 C#(シーシャープ)でネットワークプリンターに印刷するには、以下のものが必要です:

  1. C#(シーシャープ)アプリケーションを実行するマシンからネットワークプリンターにアクセスできることを確認してください。

  2. プリンターのネットワークアドレス (IPアドレスまたはネットワークプリンタ名).

  3. 必要なプリンタドライバがインストールされていること。

  4. 必要であれば、プリンターやドライバーをインストールするための管理者権限。

IronPrint

実装の詳細に飛び込む前に、IronPrint の始め方を知っておこう:

  1. インストール:IronPrint (アイアンプリント)はNuGetパッケージマネージャで簡単にインストールできます。 をご覧ください。 NuGetページ をクリックし、インストール手順に従ってください。

  2. ドキュメント:ドキュメント**:以下を参照のこと。 公式ドキュメント をご覧ください。

    互換性とサポート:IronPrint (アイアンプリント)は、様々な環境とプロジェクトタイプに幅広い互換性とサポートを提供します:

    • .NETバージョンサポート:IronPrintは.NET 8,7,6,5,コア3.1+を含むC#、VB.NET、F#をサポートしています。
    • オペレーティングシステムと環境:アイアンプリントはWindowsに対応しています。 (7+)マックオス (10+)iOS (11+)およびAndroid API 21以上 (v5 "ロリポップ").
  • プロジェクトの種類:モバイル開発 (Xamarin、MAUI、Avalonia)デスクトップ (WPF、MAUI、ウィンドウズ・アヴァロニア)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)
VB   C#

システム印刷

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
VB   C#

アイアンプリントとSystem.Printingの比較

C#(シーシャープ)でネットワークプリンターを使って印刷する方法図1 - IronPrintとSystem.Printingの簡単な比較表

この表はIronPrintとSystem.Printingの機能と特徴を簡単に説明したもので、開発者が特定の要件とプラットフォームターゲットに基づいて十分な情報を得た上で意思決定するのに役立つ。

ステップ1: IronPrint (アイアンプリント)を使ってドキュメントを印刷するための新規プロジェクトを作成する。

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

C#(シーシャープ)でネットワークプリンターを使って印刷する方法:図2 - コンソールアプリケーションの作成

プロジェクト名と場所を指定してください。

C#(シーシャープ)でネットワークプリンターを使って印刷する方法:図3 - プロジェクト名を指定する

.NETバージョンを選択してください。

C#(シーシャープ)でネットワークプリンターを使って印刷する方法:図4 適切な.NETバージョンを選択する

これでプロジェクトは作成され、さらなるコーディングの準備が整った。

ステップ 2: 新しいプロジェクトにアイアンプリントをインストールする。

以下のようにVisual Studioパッケージマネージャからアイアンプリントをインストールしてください。

C#(シーシャープ)でネットワークプリンタを使って印刷する方法:図5 - Visual Studioパッケージマネージャでアイアンプリントを検索する

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
VB   C#

コードの説明

  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
VB   C#

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

出力

C#(シーシャープ)でネットワークプリンターを使って印刷する方法:図7 - 印刷キューに追加されたドキュメントの出力例

ステップ 5: ダイアログを使ってアイアンプリントで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
VB   C#

コードの説明

  1. IronPrint (アイアンプリント)の ShowPrintDialog を使って、ダイアログを使って印刷することができます。

  2. ダイアログが開いたら、必要なプリンタを選択して文書を印刷します。

出力

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

ステップ6:印刷設定で印刷を進める

IronPrint (アイアンプリント)では、高度な設定で文書を印刷することができます。 以下のプロパティをサポートしている:

  1. PaperSize:プリンターが使用する用紙の寸法を指定します。

  2. PaperOrientation:Automatic、Portrait、Landscapeなどの用紙の向きを定義します。

  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 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
VB   C#

コードの説明

  1. 必要な名前空間IronPrint をインポートすることからコードは始まります。

  2. IronPrintDemo`名前空間の中に、Programというクラスがpublicとして宣言されている。

  3. Mainメソッドはプログラムのエントリー・ポイントとなる。

  4. Mainメソッド内:

    • printSettingsという名前のPrintSettings` オブジェクトは、カスタム印刷設定を構成するためにインスタンス化されます。

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

    • printSettingsNumberOfCopies`プロパティは2に設定され、同じドキュメントが2部印刷されることを指定します。

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

出力

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

ライセンス

IronPrint から アイアンソフトウェア はエンタープライズ・ライブラリであり、実行にはライセンスが必要です。 IronPrint (アイアンプリント)のライセンスキーを追加することで、プロジェクトは制限や透かしなしで実行できるようになります。 ライセンス購入はこちら または、30日間の無料トライアル・キーにサインアップしてください。 これ.

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

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

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

結論

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

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

次へ >
C#でQRコードを印刷する方法

準備はできましたか? バージョン: 2024.8 新発売

無料のNuGetダウンロード 総ダウンロード数: 7,144 View Licenses >