C# でプリンタ名を取得してpdf 印刷する方法
ドキュメントを印刷に送る.NETアプリでは、システム上で使用可能なプリンタを把握することが一般的な前提条件です。 ユーザーにドロップダウンからプリンタを選ばせることが目的であっても、特定のデバイスに印刷ジョブを自動でルートさせることが目的であっても、プログラムでプリンタ名を取得することが第一歩です。
IronPrint は、現在の Windows マシンにインストールされているすべてのプリンタをPrinter.GetPrinterNames()を公開しています。 以下では、インストール、同期および非同期の取得、選択したプリンタ名を印刷ジョブに入力する方法について説明します。
クイックスタート: プリンタ名を取得
- NuGet を使用して IronPrint をインストールする:
Install-Package IronPrint - ファイルに
using IronPrint;を追加する List<string>を取得する- リストを反復処理し、各名前を表示または保存する
- 印刷時に選択された名前を
PrintSettings.PrinterNameに渡す
-
IronPrint をNuGetパッケージマネージャでインストール
PM > Install-Package IronPrint -
このコード スニペットをコピーして実行します。
using IronPrint; // Retrieve every printer installed on this machine List<string> printers = Printer.GetPrinterNames(); foreach (var name in printers) { Console.WriteLine(name); } -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronPrint を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
Printer.GetPrinterNames()を呼び出す- 返された
List<string>を反復処理する PrintSettings.PrinterNameに名前を割り当て、そのプリンタをターゲットにする- 設定された設定を
Printer.Print()に渡して印刷する
インストールされたすべてのプリンタ名を列挙するには?
List<string>として返します。 このメソッドを一度呼び出し、結果を反復処理します:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-list-all-printers.cs
using IronPrint;
using System;
using System.Collections.Generic;
// Get every printer registered on this Windows machine
List<string> printerNames = Printer.GetPrinterNames();
Console.WriteLine($"Found {printerNames.Count} printer(s):\n");
foreach (string name in printerNames)
{
Console.WriteLine($" • {name}");
}
Imports IronPrint
Imports System
Imports System.Collections.Generic
' Get every printer registered on this Windows machine
Dim printerNames As List(Of String) = Printer.GetPrinterNames()
Console.WriteLine($"Found {printerNames.Count} printer(s):" & vbCrLf)
For Each name As String In printerNames
Console.WriteLine($" • {name}")
Next
コンソール出力
3台のプリンタが見つかりました:
• Microsoft Print to PDF
• HP LaserJet Pro MFP M428
• OneNote (Desktop)
返されるリストには、ローカルプリンタ、ネットワークプリンタ、仮想印刷ドライバが含まれます。 各文字列は、Windowsの設定 > プリンタとスキャナーパネルに表示される正確な名前に一致するため、印刷設定の構成で直接使用できます。
プリンタがインストールされていない場合、メソッドは例外を投げるのではなく、空のリストを返します。 ユーザーにオプションを提示する前に必要なのは、簡単なprinterNames.Countチェックのみです。
プリンタ名を非同期で取得するには?
UIスレッドのブロックが許容されないアプリケーション(WPF、MAUI、またはASP.NET web apps)では、IronPrint はPrinter.GetPrinterNamesAsync()を提供します。 このメソッドはTask<List<string>>を返し、同期バージョンと同様に動作します:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-async-printer-names.cs
using IronPrint;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
// Await the async call to avoid blocking the UI thread
List<string> printerNames = await Printer.GetPrinterNamesAsync();
foreach (string name in printerNames)
{
Console.WriteLine(name);
}
Imports IronPrint
Imports System
Imports System.Collections.Generic
Imports System.Threading.Tasks
' Await the async call to avoid blocking the UI thread
Dim printerNames As List(Of String) = Await Printer.GetPrinterNamesAsync()
For Each name As String In printerNames
Console.WriteLine(name)
Next
他の非同期APIと同様に、呼び出しをawaitします。 結果はList<string>であり、追加の解析や変換は不要です。 この非同期パターンは、async voidイベントハンドラーと自然に統合されます。
名前で特定のプリンタに印刷するには?
プリンタ名を取得したら、それをPrinter.Print()に渡します。 これにより、ダイアログを表示せずに選択されたプリンタにドキュメントが直接送信されます:
:path=/static-assets/print/content-code-examples/how-to/retrieve-printer-names/retrieve-printer-names-print-to-specific-printer.cs
using IronPrint;
using System.Collections.Generic;
// Step 1 — Retrieve available printers
List<string> printers = Printer.GetPrinterNames();
// Step 2 — Select a printer (first match containing "LaserJet" as an example)
string targetPrinter = printers.Find(p => p.Contains("LaserJet"))
?? printers[0]; // fallback to first available
// Step 3 — Configure print settings
PrintSettings settings = new PrintSettings
{
PrinterName = targetPrinter,
PaperSize = PaperSize.A4,
NumberOfCopies = 1
};
// Step 4 — Print the document
Printer.Print("invoice.pdf", settings);
Imports IronPrint
Imports System.Collections.Generic
' Step 1 — Retrieve available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
' Step 2 — Select a printer (first match containing "LaserJet" as an example)
Dim targetPrinter As String = printers.Find(Function(p) p.Contains("LaserJet")) _
OrElse printers(0) ' fallback to first available
' Step 3 — Configure print settings
Dim settings As New PrintSettings With {
.PrinterName = targetPrinter,
.PaperSize = PaperSize.A4,
.NumberOfCopies = 1
}
' Step 4 — Print the document
Printer.Print("invoice.pdf", settings)
PaperMarginsなどの追加プロパティをサポートしています。 完全なリストはPrintSettings API referenceと印刷設定ハウツーガイドでご覧ください。
特定のトレイから紙を引き出す必要がある印刷ジョブのために、Printer.GetPrinterTrays(printerName)を使用して、指定したプリンタの使用可能な用紙トレイも取得します。
次のステップは何ですか?
4つの操作:IronPrintのインストール、PrintSettings.PrinterNameを通じて特定のプリンタにドキュメントをルートする方法について説明しました。
さらに詳しい内容や例については、これらのリソースをご参照ください:
- IronPrintチュートリアル — 印刷文書でエンドツーエンドの印刷手順を説明します。
- DPI、余白、方向などを設定するための印刷設定ハウツー。
- 静的メソッドの完全なリストについては、プリンタ クラス API リファレンスをご覧ください。
無料トライアルライセンスを取得して、ライブ環境ですべての機能をテストするか、ライセンスオプションを見ることがサービスの準備が整ったときに可能です。

