C#でpdf 印刷時のプリンター名を指定する方法
IronPrintのPrintSettingsクラスは、印刷ジョブを特定のプリンターに送信するPrinterNameプロパティを公開しています。 ターゲットプリンターの正確な名前を文字列として割り当て、その構成済みPrintSettingsオブジェクトをIronPrintの印刷メソッドのいずれかに渡すと、ドキュメントはシステムのデフォルトプリンターではなく、そのプリンターに送信されます。
このガイドは、プリンター名の設定、実行時に使用可能なプリンターの発見、および他の印刷設定とプリンター選択の組み合わせについて説明します。
クイックスタート: プリンター名の指定
- NuGetを通じてIronPrintをインストールします:
Install-Package IronPrint - ファイルに
using IronPrint;を追加します PrintSettingsオブジェクトを作成しますPrinterNameをターゲットプリンターの正確な名前に設定します- 設定を
Printer.PrintAsync()に渡します
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
PrintSettingsオブジェクトを作成PrinterNameをターゲットプリンターの名前に設定します- 設定を
Printer.Print()に渡す - 指定したプリンターで印刷するためにプロジェクトを実行します
C#でプリンター名を指定するには?
ターゲットプリンターを指定するには、その名前をPrintSettingsオブジェクト上のPrinterNameプロパティに割り当てます。 そのオブジェクトをPrinter.Printに渡します。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-set-printer-name.cs
using IronPrint;
// Configure print settings with a target printer
PrintSettings settings = new PrintSettings();
settings.PrinterName = "Microsoft Print to PDF";
// Send the document to the specified printer
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' Configure print settings with a target printer
Dim settings As New PrintSettings()
settings.PrinterName = "Microsoft Print to PDF"
' Send the document to the specified printer
Printer.Print("invoice.pdf", settings)
まずPrinterName = nullで初期化します—これはOSのデフォルトプリンターを意味します。 次にPrinterNameをターゲットプリンターの正確な文字列名でオーバーライドします。 Printer.Printを呼び出すと、IronPrintはジョブを直接そのプリンターのキューに送信します。
注意すべき重要な2つの詳細。 まず、プリンター名はオペレーティングシステムが報告するものと正確に一致する必要があります。この比較は大文字小文字を区別します。"hp laserjet"のような不一致は、エラーを引き起こさず静かに失敗します。 2つ目は、ShowPrintDialogを介してユーザーが印刷ダイアログを開いた場合、そのダイアログの選択がコードで設定されたPrinterNameをオーバーライドすることです。 これは設計によるもので、ダイアログが最終的な制御をユーザーに提供します。
使用可能なプリンターをどうやって発見しますか?
プリンター名をハードコーディングする代わりに、Printer.GetPrinterNames()を使用して実行時にシステムをクエリできます。 このメソッドはマシンにインストールされたすべてのプリンターを含むList<string>を返します。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-discover-printers.cs
using IronPrint;
// Discover all installed printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Use the first available printer
if (printers.Count > 0)
{
Printer.Print("report.pdf", new PrintSettings
{
PrinterName = printers[0]
});
}
Imports IronPrint
' Discover all installed printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Use the first available printer
If printers.Count > 0 Then
Printer.Print("report.pdf", New PrintSettings With {
.PrinterName = printers(0)
})
End If
オペレーティングシステムが知っているすべてのプリンター(ローカル、ネットワーク、仮想プリンター"Microsoft Print to PDF"などを含む)を取得するためにGetPrinterNames()を呼び出します。リストを反復して、インデックス名の一致やアプリケーションの要件に応じたカスタムロジックでプリンターを選択します。
この発見してから印刷するパターンは、異なるマシンに配備されたアプリケーションにとって必要不可欠です。 シングルマシンシナリオではプリンター名のハードコーディングが機能しますが、プロダクションアプリケーションは利用可能なプリンターをクエリし、ユーザーに選択させるか、命名規則に基づいてプログラムでプリンターを選択する必要があります。 専用のコード例については、プリンター名の取得例を参照してください。
プリンター名を他の設定とどうやって組み合わせますか?
PrintSettingsクラスは、用紙サイズ、向き、DPI、余白、コピー数、および平坦化のプロパティと共にPrinterNameを公開します。 単一のオブジェクトで全てを設定します。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-combined-settings.cs
using IronPrint;
// Build a fully configured print job targeting a specific printer
PrintSettings settings = new PrintSettings
{
PrinterName = "Office Color Printer",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
PaperMargins = new Margins(15, 15, 15, 15),
Grayscale = false
};
// Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings);
Imports IronPrint
' Build a fully configured print job targeting a specific printer
Dim settings As New PrintSettings With {
.PrinterName = "Office Color Printer",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.PaperMargins = New Margins(15, 15, 15, 15),
.Grayscale = False
}
' Print a branded report to the color printer
Printer.Print("quarterly-report.pdf", settings)
読みやすいようにオブジェクト初期化子構文を使用します。 PrinterNameはジョブを"Office Color Printer"にルーティングし、その他のプロパティは出力形式を制御します。 300でDpiはシャープなテキストとグラフィックを生成します。 PaperMarginsはMarginsコンストラクタを通じて4つのミリメートル数値を受け入れます - 上、右、下、左。
IronPrintは構成を1つのユニットとして検証し、統合設定を単一のジョブとしてプリンタードライバーに送信します。 トレイ選択やグレースケールモードのような追加オプションについては、完全な印刷設定ガイドを参照してください。
プリンターを選んで非同期で印刷するには?
WPFやWinFormsアプリのようにメインスレッドをブロックできないアプリケーションには、Printer.GetPrinterNamesAsync()やPrinter.PrintAsync()を使用します。 どちらもTaskを返し、UIを応答性のある状態に保ちます。
:path=/static-assets/print/content-code-examples/how-to/specify-printer-name/specify-printer-name-async-printer-select.cs
using IronPrint;
using System.Threading.Tasks;
public class PrintService
{
public async Task PrintToFirstAvailableAsync(string filePath)
{
// Discover printers without blocking the UI
List<string> printers = await Printer.GetPrinterNamesAsync();
if (printers.Count == 0)
{
Console.WriteLine("No printers found.");
return;
}
// Configure and print to the first available printer
PrintSettings settings = new PrintSettings
{
PrinterName = printers[0],
Dpi = 300
};
await Printer.PrintAsync(filePath, settings);
}
}
Imports IronPrint
Imports System.Threading.Tasks
Public Class PrintService
Public Async Function PrintToFirstAvailableAsync(filePath As String) As Task
' Discover printers without blocking the UI
Dim printers As List(Of String) = Await Printer.GetPrinterNamesAsync()
If printers.Count = 0 Then
Console.WriteLine("No printers found.")
Return
End If
' Configure and print to the first available printer
Dim settings As New PrintSettings With {
.PrinterName = printers(0),
.Dpi = 300
}
Await Printer.PrintAsync(filePath, settings)
End Function
End Class
このクラスベースの例は、発見と印刷のロジックを再利用可能なサービスにラップしています。UIをフリーズさせずにプリンターリストを取得するためにPrinterNameに割り当てます。 await Printer.PrintAsync呼び出しはジョブを非同期に送信します。
本番環境では、printers[0]を命名規則に一致するロジックで置き換えるかもしれません—例えば、"Label"を含むプリンターを探して出荷ラベル用に、"Color"をブランド文書用に探すなど。 IronPrintの非同期メソッドはすべて同じPrintSettingsオブジェクトを受け入れるため、PrinterNameの動作は同期パスと非同期パスの間で同じです。
次のステップは何ですか?
IronPrintのPrinter.GetPrinterNames()を用いた動的実行時発見までカバーしました。 重要な点: プリンター名は正確に一致する必要があります(大文字小文字を区別します)、nullはOSのデフォルトプリンターがデフォルトで設定されており、印刷ダイアログはプログラムの選択を上書きします。
IronPrintの機能をさらに探るために:
・スタンドアロンの発見スニペットについては、プリンター名の取得コード例を参照してください
- 利用可能なすべてのプロパティについて印刷設定構成ガイドを参照
・トレイ管理の
ShowPrintDialogのようなメソッドのPrinterクラスAPIリファレンスを確認してください ・Marginsの完全なAPIリファレンスをブラウズしてください
無料の30日間トライアルを開始して、独自のプロジェクトでプリンター選択をテストするか、生産デプロイメントのライセンスオプションを確認してください。

