C#でドキュメントをサイレント印刷する方法
サイレント印刷は、コードから直接プリンターに文書を送信します - ダイアログボックスなし、ユーザーの操作なし、中断なし。 バッチ請求書処理、キオスクアプリケーション、Windowsサービス背景ジョブのような自動化ワークフローにおいて、印刷ダイアログを排除することは必須の要件です。 ネイティブのSystem.Drawing.Printing名前空間はサイレント印刷の道を提供しますが、イベント駆動のボイラープレートを要求し、チームやプロジェクト全体でのスケーリングが困難です。
IronPrintはサイレント印刷を1回のメソッド呼び出しに簡略化します。 NuGetパッケージを1つインストールし、Printer.Print()を呼び出します - ライブラリはプリンターとの通信、文書のレンダリング、プリントスプーラーとのやり取りを裏で処理します。
- NuGetでIronPrintをインストールします:
Install-Package IronPrint - ファイルに
using IronPrint;を追加します - 文書をデフォルトプリンターに送るために
Printer.Print("filepath")を呼び出します - プリンター名、DPI、コピー数、用紙設定を制御する
PrintSettingsオブジェクトを渡します - 印刷操作が呼び出し元のスレッドをブロックしないようにする場合は
Printer.PrintAsync()を使用します
-
1IronPrint を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
using IronPrint; // Silent print — no dialog, no user interaction Printer.Print("invoice.pdf");Imports IronPrint ' Silent print — no dialog, no user interaction Printer.Print("invoice.pdf")Imports IronPrint ' Silent print — no dialog, no user interaction Printer.Print("invoice.pdf") -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronPrintを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
- サイレント出力のために
Printer.Print("filepath")を呼び出します - カスタム設定には
PrintSettingsオブジェクトを渡します - ブロッキングされない実行のために
Printer.PrintAsync()を使用します - プロジェクトを実行してダイアログなしでサイレント印刷します
.NETでサイレント印刷はどのように機能するのか?
.NET System.Drawing.Printing 名前空間には、印刷操作中のステータス ダイアログを抑制する StandardPrintController クラスが含まれています。 デフォルトでは、.NET は、"Printing page X of Y" ポップアップを表示する PrintControllerWithStatusDialog を使用します。StandardPrintController に切り替えるとそのダイアログは表示されませんが、セットアップ費用は依然としてかなりのものです。
ネイティブアプローチで静かに印刷するには、PrintDocument を作成し、印刷グラフィックスサーフェスにコンテンツを描画する PrintPage イベント ハンドラーをアタッチし、StandardPrintController を割り当て、PrinterSettings を構成し、Print() を呼び出します。 これは1つの文書のセットアップコードに約15〜25行が必要で、新しい文書タイプや形式ごとに、そのPrintPageイベントで独自のレンダリングロジックが必要になります。 特に PDF レンダリングは System.Drawing.Printing に含まれていないため、ページを抽出して Graphics サーフェスに描画するために別途 PDF 分析ライブラリが必要です。
IronPrintはこの完全なパイプラインを静的なPrinterクラスにまとめています。 このPrint()メソッドはファイルパスやバイト配列を受け取り、ファイル形式を検出して適切なエンジンでレンダリングし、デフォルトプリンターに送信します - ダイアログを表示せずに。
using IronPrint;
// Print a PDF silently
Printer.Print("quarterly-report.pdf");
// Print from a byte array
byte[] pdfData = File.ReadAllBytes("shipping-label.pdf");
Printer.Print(pdfData);using IronPrint;
// Print a PDF silently
Printer.Print("quarterly-report.pdf");
// Print from a byte array
byte[] pdfData = File.ReadAllBytes("shipping-label.pdf");
Printer.Print(pdfData);Imports IronPrint
' Print a PDF silently
Printer.Print("quarterly-report.pdf")
' Print from a byte array
Dim pdfData As Byte() = File.ReadAllBytes("shipping-label.pdf")
Printer.Print(pdfData)Imports IronPrint
' Print a PDF silently
Printer.Print("quarterly-report.pdf")
' Print from a byte array
Dim pdfData As Byte() = File.ReadAllBytes("shipping-label.pdf")
Printer.Print(pdfData)このPrint()メソッドはPDF、PNG、TIFF、JPEG、GIF、HTML、BMPファイル形式をサポートしています。 ファイルパスを文字列として、または生のファイルデータをbyte[]として渡すと、IronPrintは自動的にレンダリング戦略を決定します。
サイレント出力のための印刷設定をどのように設定するか?
このPrintSettingsクラスを使用すると、印刷ジョブを完全に制御できます。 ターゲットプリンター、用紙寸法、向き、余白、DPI、カラーモード、コピー数を設定してから設定オブジェクトをPrinter.Print()に渡します。
using IronPrint;
// Configure print settings
var settings = new PrintSettings
{
PrinterName = "HP LaserJet Pro",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PaperMargins = new Margins(10, 10, 10, 10)
};
// Print with custom settings
Printer.Print("report.pdf", settings);using IronPrint;
// Configure print settings
var settings = new PrintSettings
{
PrinterName = "HP LaserJet Pro",
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PaperMargins = new Margins(10, 10, 10, 10)
};
// Print with custom settings
Printer.Print("report.pdf", settings);Imports IronPrint
' Configure print settings
Dim settings As New PrintSettings With {
.PrinterName = "HP LaserJet Pro",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PaperMargins = New Margins(10, 10, 10, 10)
}
' Print with custom settings
Printer.Print("report.pdf", settings)Imports IronPrint
' Configure print settings
Dim settings As New PrintSettings With {
.PrinterName = "HP LaserJet Pro",
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PaperMargins = New Margins(10, 10, 10, 10)
}
' Print with custom settings
Printer.Print("report.pdf", settings)各プロパティは標準的なプリントスポーラー設定にマッピングされています。 Dpiは出力解像度を制御し、300はビジネス文書に一般的で、150はドラフトに適しています。 Grayscale を true に設定することで、色が不要な場合のトナー使用量が減少します。 Marginsの値はミリメートルで指定されます。
特定のプリンターを選択する方法は?
システムにインストールされているすべてのプリンターを列挙するために Printer.GetPrinterNames() を使用し、ターゲットプリンターの名前を PrintSettings.PrinterName に割り当てます。
using IronPrint;
// List all available printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Target a specific network printer
var settings = new PrintSettings
{
PrinterName = printers.First(p => p.Contains("LaserJet"))
};
// Print the document
Printer.Print("document.pdf", settings);using IronPrint;
// List all available printers
List<string> printers = Printer.GetPrinterNames();
foreach (string name in printers)
{
Console.WriteLine(name);
}
// Target a specific network printer
var settings = new PrintSettings
{
PrinterName = printers.First(p => p.Contains("LaserJet"))
};
// Print the document
Printer.Print("document.pdf", settings);Imports IronPrint
' List all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Target a specific network printer
Dim settings As New PrintSettings With {
.PrinterName = printers.First(Function(p) p.Contains("LaserJet"))
}
' Print the document
Printer.Print("document.pdf", settings)Imports IronPrint
' List all available printers
Dim printers As List(Of String) = Printer.GetPrinterNames()
For Each name As String In printers
Console.WriteLine(name)
Next
' Target a specific network printer
Dim settings As New PrintSettings With {
.PrinterName = printers.First(Function(p) p.Contains("LaserJet"))
}
' Print the document
Printer.Print("document.pdf", settings)PrinterNameが指定されていない場合、IronPrintはジョブをOSのデフォルトプリンターにルーティングします。 複数のプリンターがある環境 - 共有オフィス、倉庫、プリントルーム - では、プログラムで正しいプリンターを選択し列挙することで誤って送信されるジョブを防ぎます。
複数のドキュメントを一括印刷するにはどうすればよいですか?
バッチ印刷はわかりやすいループパターンに従います。 ファイルパスのコレクションを反復処理し、各文書に対してPrinter.Print()を呼び出します。 各呼び出しがサイレントであるため、バッチ全体が一度のダイアログプロンプトなしで完了します。
using IronPrint;
// Collect all PDFs in the batch folder
string[] invoices = Directory.GetFiles(@"C:\Invoices\Pending", "*.pdf");
// Configure print settings for the batch
var settings = new PrintSettings
{
PrinterName = "Accounting Printer",
NumberOfCopies = 1,
Grayscale = true
};
// Print each invoice and track successes
int successCount = 0;
foreach (string invoice in invoices)
{
try
{
Printer.Print(invoice, settings);
successCount++;
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}");
}
}
// Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.");using IronPrint;
// Collect all PDFs in the batch folder
string[] invoices = Directory.GetFiles(@"C:\Invoices\Pending", "*.pdf");
// Configure print settings for the batch
var settings = new PrintSettings
{
PrinterName = "Accounting Printer",
NumberOfCopies = 1,
Grayscale = true
};
// Print each invoice and track successes
int successCount = 0;
foreach (string invoice in invoices)
{
try
{
Printer.Print(invoice, settings);
successCount++;
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}");
}
catch (Exception ex)
{
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}");
}
}
// Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.");Imports IronPrint
Imports System.IO
' Collect all PDFs in the batch folder
Dim invoices As String() = Directory.GetFiles("C:\Invoices\Pending", "*.pdf")
' Configure print settings for the batch
Dim settings As New PrintSettings With {
.PrinterName = "Accounting Printer",
.NumberOfCopies = 1,
.Grayscale = True
}
' Print each invoice and track successes
Dim successCount As Integer = 0
For Each invoice As String In invoices
Try
Printer.Print(invoice, settings)
successCount += 1
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}")
Catch ex As Exception
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}")
End Try
Next
' Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.")Imports IronPrint
Imports System.IO
' Collect all PDFs in the batch folder
Dim invoices As String() = Directory.GetFiles("C:\Invoices\Pending", "*.pdf")
' Configure print settings for the batch
Dim settings As New PrintSettings With {
.PrinterName = "Accounting Printer",
.NumberOfCopies = 1,
.Grayscale = True
}
' Print each invoice and track successes
Dim successCount As Integer = 0
For Each invoice As String In invoices
Try
Printer.Print(invoice, settings)
successCount += 1
Console.WriteLine($"Printed: {Path.GetFileName(invoice)}")
Catch ex As Exception
Console.WriteLine($"Failed: {Path.GetFileName(invoice)}: {ex.Message}")
End Try
Next
' Report batch results
Console.WriteLine($"Batch complete: {successCount}/{invoices.Length} documents printed.")各Print()呼び出しをtry/catchでラップすることで、単一の破損したファイルまたはプリンタータイムアウトがバッチ全体を停止させないようにします。 バックグラウンドサービスで動作する大規模なバッチの場合、各結果をデータベースまたはモニタリングシステムに記録することで、運用チームが確認できる監査ル'_TRACK' を提供します。
スレッドをブロックせずに非同期で印刷するには?
Printer.PrintAsync() メソッドは Task を返し、async/await パターンと互換性があります。 これは、ブロックする印刷呼び出しがインターフェイスをフリーズさせるUIアプリケーションや、同時操作を処理するサービスにとって不可欠です。
using IronPrint;
// Print asynchronously without blocking the thread
await Printer.PrintAsync("report.pdf");
// Print a batch of reports asynchronously
string[] files = Directory.GetFiles(@"C:\Reports", "*.pdf");
foreach (string file in files)
{
await Printer.PrintAsync(file);
}using IronPrint;
// Print asynchronously without blocking the thread
await Printer.PrintAsync("report.pdf");
// Print a batch of reports asynchronously
string[] files = Directory.GetFiles(@"C:\Reports", "*.pdf");
foreach (string file in files)
{
await Printer.PrintAsync(file);
}Imports IronPrint
' Print asynchronously without blocking the thread
Await Printer.PrintAsync("report.pdf")
' Print a batch of reports asynchronously
Dim files As String() = Directory.GetFiles("C:\Reports", "*.pdf")
For Each file As String In files
Await Printer.PrintAsync(file)
NextImports IronPrint
' Print asynchronously without blocking the thread
Await Printer.PrintAsync("report.pdf")
' Print a batch of reports asynchronously
Dim files As String() = Directory.GetFiles("C:\Reports", "*.pdf")
For Each file As String In files
Await Printer.PrintAsync(file)
NextPrintAsync() は、Print() と同じパラメーターを受け入れます - ファイルパスまたはバイト配列、そしてオプションの PrintSettings オブジェクトです。 非同期オーバーロードは、大量ドキュメントが同時に印刷キューに入る高スループットシナリオでスレッドプールの枯渇を防ぎます。 これはモダンな.NET開発全体で推奨される非同期タスクベースパターンに従っています。
プラットフォームの考慮点
IronPrintはデスクトップおよびモバイルプラットフォーム間でサイレント印刷をサポートしますが、動作はオペレーティングシステムによって異なります。
| プラットフォーム | サイレント印刷 | ノート |
|---|---|---|
| Windows (7+) | フルサポート | ダイアログなし、完全なPrintSettings制御 |
| macOS (10+) | サポート対象 | ネイティブmacOS印刷サブシステムを使用 |
| iOS (11+) | ダイアログが表示されます | Print()は引き続きシステムの印刷ダイアログを表示します |
| Android (API 21+) | ダイアログが表示されます | Print()は引き続きシステムの印刷ダイアログを表示します |
モバイルプラットフォームでは、OSの制限により真にサイレントな印刷が不可で、Printer.Print()はネイティブの印刷ダイアログを必ず表示します。 デスクトッププラットフォーム(WindowsとmacOS)は注意点なしで完全無人のサイレント印刷をサポートします。
ネイティブ.NET印刷との比較
ライブラリを採用するかネイティブのSystem.Drawing.Printing名前空間を使用するかを評価するエンジニアリングチームのために、次のようにトレードオフを示します:
| System.Drawing.Printing (ネイティブ) | IronPrint | |
|---|---|---|
| 文書ごとのセットアップ | ~15-25行: PrintDocument, PrintPageハンドラー, StandardPrintController, PrinterSettings | シングルPrinter.Print()呼び出し |
| PDFレンダリング | 組み込みではなく - Graphics面にページを抽出するためには別のPDFライブラリが必要 | 組み込み - PDF、PNG、TIFF、JPEG、GIF、HTML、BMPを直接処理 |
| ダイアログ抑制 | 手動: デフォルトの PrintControllerWithStatusDialog の代わりに StandardPrintController を割り当てます | Printer.Print() によってデフォルトでサイレント |
| 形式のサポート | 手作業で構築された各形式ごとに1つのレンダリングパイプライン | 単一のAPIを介して複数の形式 |
ネイティブアプローチは、チームがすでにドキュメントレンダリングインフラを持っているシンプルなシナリオに適しています。 既存のレンダリングコードなしでPDF、画像、HTMLを印刷するチームにとって、IronPrintは開発期間と継続的なメンテナンスの数週間を削減します。 2025年5月のリリースに組み込まれた30%の印刷速度向上は、社内で構築する場合にはエンジニアリングサイクルを消費する最適化です。
次のステップ
IronPrint でのサイレント印刷は、3 つの主要なメソッドに依存しています: 同期サイレント出力用の Printer.Print()、ノンブロッキング実行用の Printer.PrintAsync()、および印刷ジョブを完全に制御するための PrintSettings。 これらは、デスクトッププラットフォーム全体の単一ドキュメント、バッチ、および同時印刷シナリオをカバーします。
IronPrintチュートリアルで詳細な手順を探るか、PrinterクラスAPIリファレンスで完全なメソッド面を確認してください。 印刷設定の方法は、トレイ選択やフラットニングなどの追加の設定オプションをカバーしています。
ライブ環境でサイレント印刷をテストするために無料の30日間トライアルを開始する - クレジットカードは不要です。 展開の準備ができたら、ライセンスオプションを確認してください $999から始まります。
Iron Softwareのエンジニアとチャットすることで、特定の展開シナリオに関してヘルプを受けることができます。
よくある質問
C#におけるサイレント印刷とは?
C#での静音印刷とは、ダイアログボックスを表示したりユーザー操作を必要とせずに、コードから直接プリンタにドキュメントを送信するプロセスを指します。これは、バッチ請求書処理やキオスクアプリケーションのような自動化されたワークフローに特に役立ちます。
IronPrintを使用して静音印刷を行うにはどうすればよいですか?
IronPrintを使用して静音印刷を行うには、NuGet経由でIronPrintライブラリをインストールし、文書のファイルパスを指定してPrinter.Print()メソッドを呼び出し、オプションで追加設定のためにPrintSettingsオブジェクトを渡します。
IronPrintは静音印刷のために異なるドキュメントフォーマットを処理できますか?
はい、IronPrintはPDF、PNG、TIFF、JPEG、GIF、HTML、BMPを含む様々なドキュメントフォーマットを静音印刷のために処理できます。ファイルフォーマットを自動的に検出し、ダイアログを表示することなく処理します。
IronPrintで印刷ジョブの設定をどのようにコントロールしますか?
PrintSettingsクラスを使用することで、IronPrintで印刷ジョブの設定をコントロールできます。ターゲットプリンタ、用紙サイズ、印刷向き、DPI、部数、カラーモード、およびマージンの設定が可能です。
IronPrintでドキュメントを非同期に印刷する方法はありますか?
はい、IronPrintはPrinter.PrintAsync()メソッドを提供しており、Taskを返し、非同期の印刷操作を可能にします。この機能は、印刷中にメインスレッドをブロックしたくないアプリケーションにとって不可欠です。
IronPrintは複数のドキュメントのバッチ印刷をサポートしていますか?
IronPrintはファイルパスのコレクションを反復処理し、各ファイルに対してPrinter.Print()を呼び出すことでバッチ印刷をサポートしています。これにより、ユーザー操作やダイアログのプロンプトなしにバッチ全体が完了します。
IronPrintを使用して特定のプリンタを静音印刷のために指定することはできますか?
はい、Printer.GetPrinterNames()を使用して利用可能なプリンタをリストし、目的のプリンタをPrintSettings.PrinterNameに割り当てることで、特定のプリンタを静音印刷のために指定できます。
IronPrintを使用した静音印刷のプラットフォームに関する考慮事項は何ですか?
IronPrintはWindows(完全)およびmacOS(ネイティブサポート)での静音印刷をサポートしていますが、iOSおよびAndroidのようなモバイルプラットフォームでは、オペレーティングシステムの制限によりシステムプリントダイアログが表示されます。
IronPrintは使いやすさにおいてネイティブの.NET印刷とどう比較されますか?
IronPrintはPrinter.Print()を1回呼び出すだけで印刷プロセスを簡素化しますが、ネイティブの.NET印刷では、セットアップ、イベント処理、およびドキュメントレンダリングのために約15~25行の定型文コードが必要です。
C#プロジェクトでIronPrintを使用するためのインストール手順は何ですか?
C#プロジェクトでIronPrintを使用するには、Install-Package IronPrintでNuGetを介してパッケージをインストールし、コードにusing IronPrint;を含め、文書のファイルパスを指定してPrinter.Print()を呼び出します。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。