C#で印刷の紙の余白を設定する方法
印刷の余白は、ドキュメントの内容と紙の端との間の空白を管理します。 正しく設定することで、文字の切断を防ぎ、プリンター間で一貫したレイアウトを保証し、請求書、報告書、法的文書の書式設定要件を満たします。
IronPrint の Margins クラスは ミリメートル単位で値を受け取り、均一、水平/垂直、各側別の3つのコンストラクタオーバーロードを提供します。そのため、1行で任意のレイアウト要件に対応できます。インストールからカスタムマージンでの印刷まで、各手法を以下で解説します。
- 1.NuGetを介してIronPrintをインストールします:
Install-Package IronPrint - 2.ファイルに
using IronPrint;を追加します - 3.
PrintSettingsオブジェクトを作成します PaperMarginsにMarginsの値を割り当てます (値はミリメートル単位)- 5.設定をファイルパス付きの
Printer.Print()に渡します
-
1IronPrint を NuGet Package Manager でインストールします
-
2このコード スニペットをコピーして実行します。
using IronPrint; // Set 15 mm margins on all sides and print PrintSettings settings = new PrintSettings(); settings.PaperMargins = new Margins(15); Printer.Print("report.pdf", settings);Imports IronPrint ' Set 15 mm margins on all sides and print Dim settings As New PrintSettings() settings.PaperMargins = New Margins(15) Printer.Print("report.pdf", settings)Imports IronPrint ' Set 15 mm margins on all sides and print Dim settings As New PrintSettings() settings.PaperMargins = New Margins(15) Printer.Print("report.pdf", settings) -
3実際の環境でテストするためにデプロイする
今日からご自身のプロジェクトでIronPrintを使い始めるには、無料トライアルをお試しください。
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
PrintSettingsオブジェクトを作成PaperMarginsにMargins値を割り当てます- 設定を
Printer.Print()に渡す - カスタム余白で印刷するにはプロジェクトを実行します
すべての辺に均等な余白を設定できますか?
最も単純なコンストラクタは1つの整数を受け取り、それを四辺すべてに均等に適用します。 値はミリメートル単位で渡します:
using IronPrint;
// Configure a uniform 20 mm margin on all sides
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
// Print the invoice
Printer.Print("invoice.pdf", settings);using IronPrint;
// Configure a uniform 20 mm margin on all sides
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
// Print the invoice
Printer.Print("invoice.pdf", settings);Imports IronPrint
' Configure a uniform 20 mm margin on all sides
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
' Print the invoice
Printer.Print("invoice.pdf", settings)Imports IronPrint
' Configure a uniform 20 mm margin on all sides
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
' Print the invoice
Printer.Print("invoice.pdf", settings)Margins(20) は Left、Top、Right および Bottom をそれぞれ20 mmに設定します。 標準ビジネス文書では、すべての端に均等な空白があれば十分であるため、これが最も一般的な選択です。
IronPrintはミリメートル単位でマージンを測定し、インチの百分の一を使用するSystem.Drawing.Printing.Marginsクラスの混乱を避けます。 IronPrintでの25.4mmマージンはnew System.Drawing.Printing.Margins(100)に相当し、変換計算は必要ありません。
各辺の余白を異なる設定にするにはどうすれば良いですか?
文書の上部にレターヘッド用、または下部にフッター用の余白が必要な場合、4つのパラメータコンストラクタを使用します。
using IronPrint;
// Configure per-side margins (left, top, right, bottom)
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
// Print the letterhead
Printer.Print("letterhead.pdf", settings);using IronPrint;
// Configure per-side margins (left, top, right, bottom)
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
// Print the letterhead
Printer.Print("letterhead.pdf", settings);Imports IronPrint
' Configure per-side margins (left, top, right, bottom)
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
' Print the letterhead
Printer.Print("letterhead.pdf", settings)Imports IronPrint
' Configure per-side margins (left, top, right, bottom)
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
' Print the letterhead
Printer.Print("letterhead.pdf", settings)パラメーターの順序は left、top、right、bottom です。 各値は独立しており、ヘッダー、フッター、綴じ代、パンチホールスペースを考慮した非対称レイアウトを作成できます。 Margins クラスAPIリファレンスがすべてのフィールドを文書化しています。
一般的な余白レイアウトにはどのような省略オプションがありますか?
IronPrintのMarginsクラスは、一律および各側ごとのバージョンに加えて、1つの追加コンストラクタと1つの静的ショートカットフィールドを提供します:
水平 / 垂直ショートカット - Margins(int horizontal, int vertical)は、左+右に最初の値を、上+下に2番目の値を設定します:
using IronPrint;
// Configure horizontal and vertical margin shorthand
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
// Print the landscape report
Printer.Print("report-landscape.pdf", settings);using IronPrint;
// Configure horizontal and vertical margin shorthand
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
// Print the landscape report
Printer.Print("report-landscape.pdf", settings);Imports IronPrint
' Configure horizontal and vertical margin shorthand
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
' Print the landscape report
Printer.Print("report-landscape.pdf", settings)Imports IronPrint
' Configure horizontal and vertical margin shorthand
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
' Print the landscape report
Printer.Print("report-landscape.pdf", settings)ゼロマージン - Margins.Zeroは、枠なし印刷用にすべてのマージンを削除します:
using IronPrint;
// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
PaperMargins = Margins.Zero
};
// Print the poster
Printer.Print("poster.png", borderless);using IronPrint;
// Configure zero margins for edge-to-edge printing
PrintSettings borderless = new PrintSettings
{
PaperMargins = Margins.Zero
};
// Print the poster
Printer.Print("poster.png", borderless);Imports IronPrint
' Configure zero margins for edge-to-edge printing
Dim borderless As New PrintSettings With {
.PaperMargins = Margins.Zero
}
' Print the poster
Printer.Print("poster.png", borderless)Imports IronPrint
' Configure zero margins for edge-to-edge printing
Dim borderless As New PrintSettings With {
.PaperMargins = Margins.Zero
}
' Print the poster
Printer.Print("poster.png", borderless)ほとんどの物理的プリンターは、ハードウェアの最小印刷可能領域を強制することに注意してください。 Margins.Zeroを設定するとゼロマージンの指示をドライバに送信しますが、プリンターの仕様によっては内容が端で切れる場合があります。
余白を他の印刷設定と組み合わせるにはどうすれば良いですか?
PaperMargins は PrintSettings のプロパティの1つです。 用紙サイズ、向き、DPI、コピー数、グレースケールモード、プリンターの選択と組み合わせた1つの構成オブジェクトで使用できます。
using IronPrint;
// Configure full print settings with asymmetric margins
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(15, 20, 15, 25),
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PrinterName = "HP LaserJet Pro MFP M428"
};
// Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings);using IronPrint;
// Configure full print settings with asymmetric margins
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(15, 20, 15, 25),
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Portrait,
Dpi = 300,
NumberOfCopies = 2,
Grayscale = false,
PrinterName = "HP LaserJet Pro MFP M428"
};
// Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings);Imports IronPrint
' Configure full print settings with asymmetric margins
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(15, 20, 15, 25),
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PrinterName = "HP LaserJet Pro MFP M428"
}
' Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings)Imports IronPrint
' Configure full print settings with asymmetric margins
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(15, 20, 15, 25),
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Portrait,
.Dpi = 300,
.NumberOfCopies = 2,
.Grayscale = False,
.PrinterName = "HP LaserJet Pro MFP M428"
}
' Print the Q4 report to the named printer
Printer.Print("Q4-report.pdf", settings)非同期ワークフロー - WPF、MAUI、またはASP.NETウェブアプリの場合、UIスレッドをブロックしないように Printer.Print() を await Printer.PrintAsync() に置き換えてください。 同じPrintSettingsオブジェクトは両方のメソッドで動作します。
次のステップは何ですか?
IronPrintを使用して印刷マージンを設定する4つの方法を紹介しました: Margins(int) を使用した均等マージン、Margins(int, int, int, int) による各辺の制御、水平/垂直の省略形 Margins(int, int)、および Margins.Zero による枠のない印刷。 各アプローチは PrintSettings.PaperMargins に組み込まれ、Printer.Print() および Printer.PrintAsync() の両方で動作します。
さらなる学習のために、これらのリソースを探検してください:
- IronPrintチュートリアル - ドキュメントを印刷で、エンドツーエンドの印刷手順を確認してください。
- DPI、方向、コピーなどの印刷設定ハウツー。
- 完全なコンストラクタとフィールドのドキュメンテーションのための
MarginsクラスAPIリファレンス。 - すべての静的印刷メソッドのための
PrinterクラスAPIリファレンス。
無料トライアルライセンスを取得してすべての機能をライブ環境でテストするか、ライセンスオプションをご覧ください準備ができたら導入してください。
よくある質問
IronPrintでの印刷マージンとは何ですか?
IronPrintでの印刷マージンは、ドキュメントの内容と物理ページの端との間の空白を制御します。これにより、テキストの切り取りを防ぎ、一貫したレイアウトを維持し、さまざまなドキュメントのフォーマット要件を満たします。
C#でIronPrintを使用して均一な印刷マージンを設定するにはどうすればよいですか?
単一の整数値を使用して、Marginsクラスのコンストラクタを使用して均一な印刷マージンを設定できます。例えば、new Margins(20)はすべての側面に20 mmのマージンを設定します。
IronPrintを使用してドキュメントの各辺に異なるマージンを指定するにはどうすればよいですか?
各辺に異なるマージンを設定するには、4つのパラメーター(左、上、右、下)を使用してMarginsコンストラクタを使用します。例えば、new Margins(10, 25, 10, 20)です。
IronPrintでゼロ印刷マージンを設定できますか?
はい、IronPrintではMargins.Zeroを使用してゼロマージンを設定できます。ただし、物理プリンターが最小印刷可能エリアを強制する可能性があることに注意してください。
水平および垂直マージンを設定するためのショートハンド方法とは何ですか?
IronPrintではショートハンド方法としてMargins(int horizontal, int vertical)を提供しており、最初の値で左+右マージンを、2番目の値で上+下マージンを設定します。
IronPrintでマージンと組み合わせることができる他の印刷設定は何ですか?
IronPrintでは、用紙サイズ、向き、DPI、コピー数、プリンター選択などをPrintSettingsオブジェクト内でマージンと組み合わせることができます。
C#プロジェクトにIronPrintをインストールするにはどうすればよいですか?
コマンドInstall-Package IronPrintを使用してNuGetでIronPrintをインストールします。その後、using IronPrint;をプロジェクトファイルに追加します。
IronPrintでマージンを設定する際にミリメートルを使用する利点は何ですか?
ミリメートルを使用することで、100分の1インチを使用するSystem.Drawing.Printing.Marginsクラスに関連する変換エラーを避けることができ、よりシンプルで直感的になります。
IronPrintを使用して非同期にドキュメントを印刷するにはどうすればよいですか?
非同期印刷には、IronPrintベースのワークフローでPrinter.Print()をawait Printer.PrintAsync()に置き換えることでUIスレッドのブロックを防ぎます。
IronPrintのさらなるリソースとチュートリアルをどこで見つけられますか?
IronPrintのさらなるリソースは、IronPrintチュートリアルページ、MarginsおよびPrinterクラスのAPIリファレンス、ウェブサイトのPrint Settings How-Toガイドで見つけることができます。
Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。