IronPrintを使ってC#でpdf 印刷設定を構成する方法
IronPrint の PrintSettings クラスを使用して C# で印刷設定を構成し、用紙サイズ、向き、DPI、余白などを制御します。 単純に PrintSettings をインスタンス化し、好みを設定して、それを Print メソッドに渡します。
クイックスタート: 印刷設定を構成
- NuGet 経由で IronPrint をインストールします:
Install-Package IronPrint using IronPrint;をファイルに追加しますPrintSettingsオブジェクトを作成しますPaperSize、Dpi、PaperOrientation、NumberOfCopies、Grayscaleなどのプロパティを設定します- 設定を
Printer.Print()またはPrinter.ShowPrintDialog()に渡します
-
IronPrint をNuGetパッケージマネージャでインストール
PM > Install-Package IronPrint -
このコード スニペットをコピーして実行します。
using IronPrint; // Print with custom settings Printer.Print("document.pdf", new PrintSettings { PaperSize = PaperSize.A4, PaperOrientation = PaperOrientation.Landscape, Dpi = 300, NumberOfCopies = 2, Grayscale = true }); -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronPrint を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
- `PrintSettings`オブジェクトを作成
- 用紙サイズ、向き、DPI、コピー数などを構成します
- 設定を `Printer.Print()` または `Printer.ShowPrintDialog()` に渡します
- 印刷された文書が構成された設定と一致することを確認します
印刷設定はどのようにすればよいですか?
印刷設定を構成するには、PrintSettingsクラスをインスタンス化し、好みに応じて構成します。 Print または ShowPrintDialog メソッドでは、PrintSettings オブジェクトを 2番目のパラメーターとして渡します。 以下のコード例はこの使用方法を示しています。 より詳細な例については、印刷設定コード例ページを確認してください。
// Import the necessary namespace for IronPrint
using IronPrint;
// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();
// Configure various print settings
settings.PaperSize = PaperSize.A4; // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300; // Set print resolution to 300 DPI
settings.NumberOfCopies = 2; // Set the number of copies to 2
settings.PrinterName = "MyPrinter"; // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true; // Print in grayscale
// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
// Import the necessary namespace for IronPrint
using IronPrint;
// Initialize a new instance of the PrintSettings class
PrintSettings settings = new PrintSettings();
// Configure various print settings
settings.PaperSize = PaperSize.A4; // Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape; // Set paper orientation to Landscape
settings.Dpi = 300; // Set print resolution to 300 DPI
settings.NumberOfCopies = 2; // Set the number of copies to 2
settings.PrinterName = "MyPrinter"; // Set the name of the printer
settings.PaperMargins = new Margins(10, 10, 10, 10); // Set margins to 10mm on each side
settings.Grayscale = true; // Print in grayscale
// Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings);
' Import the necessary namespace for IronPrint
Imports IronPrint
' Initialize a new instance of the PrintSettings class
Private settings As New PrintSettings()
' Configure various print settings
settings.PaperSize = PaperSize.A4 ' Set paper size to A4
settings.PaperOrientation = PaperOrientation.Landscape ' Set paper orientation to Landscape
settings.Dpi = 300 ' Set print resolution to 300 DPI
settings.NumberOfCopies = 2 ' Set the number of copies to 2
settings.PrinterName = "MyPrinter" ' Set the name of the printer
settings.PaperMargins = New Margins(10, 10, 10, 10) ' Set margins to 10mm on each side
settings.Grayscale = True ' Print in grayscale
' Use the PrintSettings in the Print method
IronPrint.Printer.Print(document, settings)
なぜ印刷設定を構成する必要があるのですか?
印刷設定とは、ドキュメントまたはコンテンツの印刷方法を指定する構成またはパラメータのセットを指します。 これらの設定には、用紙サイズ、向き(縦または横)、印刷解像度(1インチあたりのドット数 - DPI)、部数、プリンタの選択、余白、グレースケール印刷などのオプションなどの詳細が含まれます。 特定の印刷の好みや要件を達成するために、これらの設定をカスタマイズします。
IronPrintの包括的な印刷設定機能は、開発者に印刷プロセスのあらゆる側面をきめ細かくコントロールすることを提供します。 デスクトップアプリケーションを構築する場合でも、ASP.NET Webアプリケーションを構築する場合でも、適切な設定を行うことで、異なる環境でも一貫した結果を得ることができます。
ドキュメント 印刷のカスタム設定はいつ使うべきですか?
カスタム印刷設定は、印刷出力を正確に制御する必要がある場合に不可欠です。たとえば、特定の余白を使用してレポートを印刷する場合、複数部のドキュメントを生成する場合、ビジネスニーズに合わせてドキュメントを正しい向きで印刷する場合などです。
ここでは、特定の要件を満たす請求書を印刷するための実用的な例を紹介します:
// Example: Printing invoices with business requirements
using IronPrint;
// Invoice printing with specific business settings
var invoiceSettings = new PrintSettings
{
PaperSize = PaperSize.Letter, // US Letter size for business documents
PaperOrientation = PaperOrientation.Portrait,
Dpi = 600, // High quality for professional output
NumberOfCopies = 3, // Original + customer copy + file copy
PaperMargins = new Margins(15, 15, 15, 25), // Extra bottom margin for footer
Grayscale = false, // Keep company logo in color
PrinterName = "Office Color Printer" // Specific high-quality printer
};
// Print the invoice
Printer.Print("invoice_2024_001.pdf", invoiceSettings);
// Example: Printing invoices with business requirements
using IronPrint;
// Invoice printing with specific business settings
var invoiceSettings = new PrintSettings
{
PaperSize = PaperSize.Letter, // US Letter size for business documents
PaperOrientation = PaperOrientation.Portrait,
Dpi = 600, // High quality for professional output
NumberOfCopies = 3, // Original + customer copy + file copy
PaperMargins = new Margins(15, 15, 15, 25), // Extra bottom margin for footer
Grayscale = false, // Keep company logo in color
PrinterName = "Office Color Printer" // Specific high-quality printer
};
// Print the invoice
Printer.Print("invoice_2024_001.pdf", invoiceSettings);
Imports IronPrint
' Example: Printing invoices with business requirements
' Invoice printing with specific business settings
Dim invoiceSettings As New PrintSettings With {
.PaperSize = PaperSize.Letter, ' US Letter size for business documents
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 600, ' High quality for professional output
.NumberOfCopies = 3, ' Original + customer copy + file copy
.PaperMargins = New Margins(15, 15, 15, 25), ' Extra bottom margin for footer
.Grayscale = False, ' Keep company logo in color
.PrinterName = "Office Color Printer" ' Specific high-quality printer
}
' Print the invoice
Printer.Print("invoice_2024_001.pdf", invoiceSettings)
印刷設定を指定しないとどうなりますか?
印刷設定が指定されていない場合、IronPrintはシステムのデフォルトプリンターのデフォルト設定を使用します。 システム上で利用可能なプリンタを検出するには、GetPrinterNames メソッドを使用して、接続されているすべてのプリンタをプログラムで取得します。
どのような印刷設定が可能ですか?
利用可能なすべての印刷設定オプションを以下でご覧ください。 完全なAPIリファレンスは、各プロパティとメソッドの詳細なドキュメントを提供します:
| 設定 | 翻訳内容 | デフォルト値 | 備考 |
|---|---|---|---|
| DefaultSettings | IronPrint.PrintSettingsクラスの新しいインスタンスをデフォルト値で初期化します。 | 該当なし | 該当なし |
| 用紙サイズ | プリンターで使用する用紙サイズを設定します。 | IronPrint.PaperSize.PrinterDefault | 該当なし |
| 論文の方向性 | 用紙の向きを指定します(縦向き、横向きなど)。 | IronPrint.PaperOrientation.Portrait | 該当なし |
| Dpi | 印刷解像度を1インチあたりのドット数で表します。 | 300 | 印刷に使用される実際のDPIは、プリンタの機能によって制限される場合があります。 |
| 部数 | ドキュメントを印刷する際に生成される同一コピーの数を示します。 | 1 | プラットフォームによっては、複数コピーの正確な複製を妨げる制限が存在する場合があります。 そのような場合、IronPrint.PrintSettings.NumberOfCopies の指定値が無視される可能性があり、印刷されるのは1部だけになります。 |
| プリンタ名。 | 印刷に使用するプリンタ名を指定します。 | null(OSデフォルトのプリンタを使用) | PrintDialogでプリンタを選択した場合、この設定は無視されます。 利用可能なプリンター名を取得するには、IronPrint.Printer.GetPrinterNames または IronPrint.Printer.GetPrinterNamesAsync を使用してプリンター名リストを取得できます。 |
| ペーパーマージン | 印刷に使用する余白をミリメートル単位で設定します。 | null(プリンタのデフォルトマージンを使用) | 該当なし |
| グレースケール | グレースケールで印刷するかどうかを示します。 | カラー印刷 | 該当なし |
| フラット化 | 印刷前にPDFを平坦化し、フォームフィールドの値や画像を表示するのに便利です。 | 偽 | 該当なし |
| トレイ。 | 印刷に使用するプリンタトレイ。 これにより、ユーザーはプリンタに給紙するトレイを指定することができます。 | null(プリンタのデフォルトトレイを使用) | PrintDialogでトレイを選択した場合、この設定は無視されます。 利用可能なトレイを取得するには、IronPrint.Printer.GetPrinterTrays(System.String) または IronPrint.Printer.GetPrinterTraysAsync(System.String) を使用できます。 このトレイ選択プロパティは、Windowsでのみ使用できます。 |
どの印刷設定を常に構成する必要がありますか?
ほとんどのビジネスアプリケーションでは、常に PaperSize、PaperOrientation、および Dpi を構成して、異なるプリンターとシステム間で一貫した出力を保証します。 この3つの設定は、文書の外観と読みやすさに最も影響します。
ダイアログベースの印刷を使用する場合は、ShowPrintDialogメソッドを使用して、カスタム設定とユーザーとの対話を組み合わせてください:
// Pre-configure settings but allow user to modify
var presetSettings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300
};
// Show dialog with preset values
Printer.ShowPrintDialog("report.pdf", presetSettings);
// Pre-configure settings but allow user to modify
var presetSettings = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300
};
// Show dialog with preset values
Printer.ShowPrintDialog("report.pdf", presetSettings);
Imports System
' Pre-configure settings but allow user to modify
Dim presetSettings As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300
}
' Show dialog with preset values
Printer.ShowPrintDialog("report.pdf", presetSettings)
プラットフォーム固有の設定をどのように扱えばよいですか?
トレイの選択など、一部の設定はWindowsでのみ利用可能です。 プラットフォーム固有の機能を使用する場合は、常にプラットフォームの互換性を確認し、クロスプラットフォームアプリケーションのフォールバック動作を提供します。 プラットフォーム固有の問題のトラブルシューティングについては、エンジニアリング・サポート・ガイドを参照してください。
一般的な印刷設定の組み合わせは何ですか?
一般的な組み合わせとしては、標準的な文書にはA4/ポートレート/300 DPI、詳細なレポートにはA3/ランドスケープ/600 DPI、インク節約のためのドラフト印刷にはレター/ポートレート/300 DPI/グレースケールなどがあります。
以下は、さまざまなシナリオの例です:
// Standard office document
var standardDocument = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300
};
// Detailed engineering drawing
var technicalDrawing = new PrintSettings
{
PaperSize = PaperSize.A3,
PaperOrientation = PaperOrientation.Landscape,
Dpi = 600,
Grayscale = false
};
// Draft mode for review
var draftMode = new PrintSettings
{
PaperSize = PaperSize.Letter,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 150,
Grayscale = true,
NumberOfCopies = 5
};
// High-volume batch printing
var batchPrint = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 100,
Tray = "Tray 2" // Large capacity tray on Windows
};
// Standard office document
var standardDocument = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300
};
// Detailed engineering drawing
var technicalDrawing = new PrintSettings
{
PaperSize = PaperSize.A3,
PaperOrientation = PaperOrientation.Landscape,
Dpi = 600,
Grayscale = false
};
// Draft mode for review
var draftMode = new PrintSettings
{
PaperSize = PaperSize.Letter,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 150,
Grayscale = true,
NumberOfCopies = 5
};
// High-volume batch printing
var batchPrint = new PrintSettings
{
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 100,
Tray = "Tray 2" // Large capacity tray on Windows
};
Imports System
' Standard office document
Dim standardDocument As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300
}
' Detailed engineering drawing
Dim technicalDrawing As New PrintSettings With {
.PaperSize = PaperSize.A3,
.PaperOrientation = PaperOrientation.Landscape,
.Dpi = 600,
.Grayscale = False
}
' Draft mode for review
Dim draftMode As New PrintSettings With {
.PaperSize = PaperSize.Letter,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 150,
.Grayscale = True,
.NumberOfCopies = 5
}
' High-volume batch printing
Dim batchPrint As New PrintSettings With {
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 100,
.Tray = "Tray 2" ' Large capacity tray on Windows
}
より包括的な例と高度な印刷シナリオについては、印刷ドキュメント チュートリアルを参照してください。
本番環境、特に Web.config を使用する Web アプリケーションで印刷設定を実装する場合は、setting license keys in Web.config のガイドを確認し、適切な設定を行うようにしてください。
よくある質問
C#で印刷設定を構成するにはどうすればよいですか?
C#で印刷設定を行うには、IronPrintからPrintSettingsクラスをインスタンス化し、PaperSize、PaperOrientation、Dpi、NumberOfCopies、Grayscaleなどのプロパティを設定します。そしてこのPrintSettingsオブジェクトをPrintまたはShowPrintDialogメソッドの2番目のパラメータとして渡します。
どのような印刷設定をカスタマイズできますか?
IronPrintのPrintSettingsクラスでは、用紙サイズ(A4、Letterなど)、向き(Portrait/Landscape)、DPI解像度、部数、プリンター選択、用紙余白、グレースケール印刷オプションをカスタマイズできます。
用紙サイズと向きの設定方法を教えてください。
Printメソッドを呼び出す前に、IronPrint PrintSettingsオブジェクトのPaperSizeプロパティ(例:PaperSize.A4)を使用して用紙サイズを設定し、PaperOrientationプロパティ(例:PaperOrientation.Landscape)を使用して向きを設定します。
ドキュメントを複数部印刷できますか?
はい、PrintSettingsクラスのNumberOfCopiesプロパティを設定することで、複数部を印刷することができます。例えば、settings.NumberOfCopies = 2と設定すると、IronPrintを使ってドキュメントを2部印刷します。
印刷用のカスタムマージンを設定する方法を教えてください。
PrintSettingsのPaperMarginsプロパティを使用して、Marginsクラスでカスタムマージンを設定します。例:settings.PaperMargins = new Margins(10, 10, 10, 10)は、IronPrintで印刷する際に全辺に10mmの余白を設定します。
カラーではなくグレースケールで印刷できますか?
はい、PrintSettingsオブジェクトのGrayscaleプロパティをtrueに設定することで、グレースケール印刷を有効にします。これによりIronPrintで印刷する際にカラードキュメントがグレースケールに変換されます。

