透かしなしで本番環境でテストしてください。
必要な場所で動作します。
30日間、完全に機能する製品をご利用いただけます。
数分で稼働させることができます。
製品トライアル期間中にサポートエンジニアリングチームへの完全アクセス
ネットワークプリンターでのプログラム印刷は、様々なソフトウェアアプリケーションで共通のタスクである。 デスクトップ、モバイルアプリケーション、ウェブサービスのいずれを構築する場合でも、ドキュメントをネットワークプリンターに直接送信する機能は、ソフトウェアの使いやすさと効率を大幅に向上させます。 この記事では、C#とIronPrintを使用してネットワークプリンターで印刷する方法を、Iron Softwareから探ります。
IronPrint を使用してドキュメントを印刷するための新しいプロジェクトを作成します。
新しいプロジェクトにIronPrintをインストールします。
ドキュメントの印刷に使用できるネットワークプリンターのリスト。
IronPrint を使用してPDFドキュメントを印刷します。
IronPrintとダイアログを使用してPDFドキュメントを印刷します。
コーディングに入る前に、環境が正しくセットアップされていることを確認しよう。 C#(シーシャープ)でネットワークプリンターに印刷するには、以下のものが必要です:
C#(シーシャープ)アプリケーションを実行するマシンからネットワークプリンターにアクセスできることを確認してください。
プリンターのネットワークアドレス(IPアドレスまたはネットワークプリンター名)。
必要なプリンタドライバがインストールされていること。
実装の詳細に飛び込む前に、IronPrint の始め方を知っておこう:
インストール: IronPrint は NuGet パッケージ マネージャーを通じて簡単にインストールできます。 NuGetページを訪問し、インストール手順に従ってください。
ドキュメント: 公式ドキュメントを参照して、クイックスタートガイドと包括的なAPIリファレンスをご覧ください。
互換性とサポート: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)
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, 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
System.Printing
この表は、IronPrint と System.Printing
の機能と特性の概要を示しており、開発者が特定の要件とプラットフォーム ターゲットに基づいて情報に基づいた意思決定を行うのに役立ちます。
以下のようにVisual Studioでコンソール・アプリケーションを作成する。
プロジェクト名と場所を指定してください。
.NETバージョンを選択してください。
これでプロジェクトは作成され、さらなるコーディングの準備が整った。
以下のようにVisual StudioパッケージマネージャからIronPrintをインストールしてください。
IronPrintは以下のコマンドでもインストールできます:
Install-Package IronPrint
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
Printer.GetPrinterNames
を使用して、利用可能なすべてのプリンタードライバーを取得します。
Console.WriteLine
を使用して名前を印刷します。以下のコードを使用すると、ドキュメントを無音で印刷できます:
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
実行後、出力に示すように、ドキュメントが印刷キューに追加される。
以下のコードを使って、ダイアログのあるドキュメントを印刷してください:
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
IronPrint の ShowPrintDialog
を使用してダイアログで印刷できます
IronPrintでは、高度な設定で文書を印刷することができます。 以下のプロパティをサポートしている:
PaperSize
: プリンターが使用する用紙寸法を指定します。
PaperOrientation
: 用紙の向きを定義します。オート、縦、または横など。
DPI
: インチあたりのドットで希望の印刷解像度を設定します。 デフォルト値は300で、商業印刷でよく使われる。 実際のDPIはプリンターの能力によって制限される場合があります。
NumberOfCopies
: ドキュメントを印刷するために同一のコピーを何部作成するかを示します。
PrinterName
: 印刷タスクに指定されたプリンターの名前を指定します。
PaperMargins
: プリントのマージンを決定し、ミリメートルで測定されます。
グレースケール
: グレースケールで印刷するかどうかを決定するブール値。 デフォルトは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
必要な名前空間IronPrint をインポートすることからコードは始まります。
IronPrintDemo
名前空間の中には、publicとして宣言されたProgramという名前のクラスがあります。
Mainメソッドはプログラムのエントリー・ポイントとなる。
Mainメソッド内:
printSettings
という名前のPrintSettings
オブジェクトは、カスタム印刷設定を構成するためにインスタンス化されます。
printSettings
の Dpi プロパティが 150 に設定されており、これは 1 インチあたり 150 ドットの印刷解像度を示しています。
printSettings
のNumberOfCopies
プロパティは2に設定されており、ドキュメントの同一のコピーが2部印刷されることを指定しています。
printSettings
の PaperOrientation
プロパティは PaperOrientation.Portrait
に設定されています。これは、ドキュメントが縦向きで印刷されることを示しています。printSettings
(カスタム印刷設定)と共に呼び出されます。 このメソッドは、おそらく指定された設定を使用して印刷プロセスを処理する。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の可能性を追求し、アプリケーション内で効率的なドキュメント印刷の世界を解き放ちましょう。