C#でpdf 印刷の紙の余白を設定する方法
印刷の余白は、ドキュメントの内容と紙の端との間の空白を管理します。 正しく設定することで、文字の切断を防ぎ、プリンター間で一貫したレイアウトを保証し、請求書、報告書、法的文書の書式設定要件を満たします。
IronPrint の Margins クラスはミリメートルで値を受け取ることができ、均等、水平/垂直、各辺別の3つのコンストラクタのオーバーロードを提供するので、1行で任意のレイアウト要件に合わせることができます。以下に、インストールからカスタム余白での印刷までの各アプローチを示します。
クイックスタート: 紙の余白を設定する
- NuGet を使用して IronPrint をインストールします:
Install-Package IronPrint using IronPrint;をファイルに追加しますPrintSettingsオブジェクトを作成しますPaperMarginsにMargins値を割り当てます(ミリメートル単位の値)- 設定をファイルパスとともに
Printer.Print()に渡します
-
IronPrint をNuGetパッケージマネージャでインストール
PM > Install-Package IronPrint -
このコード スニペットをコピーして実行します。
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); -
実際の環境でテストするためにデプロイする
今日プロジェクトで IronPrint を使い始めましょう無料トライアル
最小限のワークフロー(5ステップ)
- IronPrint C#印刷ライブラリをインストール
PrintSettingsオブジェクトを作成PaperMarginsにMargins値を割り当てます- 設定を
Printer.Print()に渡す - カスタム余白で印刷するにはプロジェクトを実行します
すべての辺に均等な余白を設定できますか?
最も単純なコンストラクタは1つの整数を受け取り、それを四辺すべてに均等に適用します。 ミリメートルで値を渡します:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-uniform-margins.cs
using IronPrint;
// 20 mm margin on every side
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(20),
PaperSize = PaperSize.A4
};
Printer.Print("invoice.pdf", settings);
Imports IronPrint
' 20 mm margin on every side
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(20),
.PaperSize = PaperSize.A4
}
Printer.Print("invoice.pdf", settings)
Margins(20) は Bottom の各辺をそれぞれ 20 mm に設定します。 標準ビジネス文書では、すべての端に均等な空白があれば十分であるため、これが最も一般的な選択です。
IronPrint はミリメートルで余白を測定しますが、これは 1/100 インチを使用する System.Drawing.Printing.Margins クラスの混乱を避けます。 IronPrint では 25.4 mm の余白が new System.Drawing.Printing.Margins(100) に相当し、変換計算は不要です。
各辺の余白を異なる設定にするにはどうすれば良いですか?
文書の上部にレターヘッド用、または下部にフッター用の余白が必要な場合、4つのパラメータコンストラクタを使用します。
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-per-side-margins.cs
using IronPrint;
// Left: 10 mm, Top: 25 mm, Right: 10 mm, Bottom: 20 mm
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 25, 10, 20),
PaperOrientation = PaperOrientation.Portrait
};
Printer.Print("letterhead.pdf", settings);
Imports IronPrint
' Left: 10 mm, Top: 25 mm, Right: 10 mm, Bottom: 20 mm
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 25, 10, 20),
.PaperOrientation = PaperOrientation.Portrait
}
Printer.Print("letterhead.pdf", settings)
パラメータの順番は bottom です。 各値は独立しており、ヘッダー、フッター、綴じ代、パンチホールスペースを考慮した非対称レイアウトを作成できます。 Margins クラス API リファレンス は、すべてのフィールドを文書化しています。
一般的な余白レイアウトにはどのような省略オプションがありますか?
IronPrint の Margins クラスは、均等および各辺別のバージョンに加えて、2つの追加のコンストラクタを提供しています。
水平/垂直省略形 — Margins(int horizontal, int vertical) は左+右を最初の値に、上+下を2番目の値に設定します:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-shorthand-margins.cs
using IronPrint;
// 10 mm left & right, 20 mm top & bottom
PrintSettings settings = new PrintSettings
{
PaperMargins = new Margins(10, 20)
};
Printer.Print("report-landscape.pdf", settings);
Imports IronPrint
' 10 mm left & right, 20 mm top & bottom
Dim settings As New PrintSettings With {
.PaperMargins = New Margins(10, 20)
}
Printer.Print("report-landscape.pdf", settings)
ゼロ余白 — Margins.Zero は枠なし印刷用にすべての余白を削除します:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-zero-margins.cs
PrintSettings borderless = new PrintSettings
{
PaperMargins = Margins.Zero
};
Printer.Print("poster.png", borderless);
Dim borderless As New PrintSettings With {
.PaperMargins = Margins.Zero
}
Printer.Print("poster.png", borderless)
ほとんどの物理的プリンターは、ハードウェアの最小印刷可能領域を強制することに注意してください。 Margins.Zero を設定するとゼロ余白の指示がドライバーに送信されますが、プリンターの能力によっては、端付近の内容が切り取られる可能性があります。
余白を他の印刷設定と組み合わせるにはどうすれば良いですか?
PaperMargins は PrintSettings の1つのプロパティです。 用紙サイズ、向き、DPI、コピー数、グレースケールモード、プリンターの選択と組み合わせた1つの構成オブジェクトで使用できます。
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-combined-settings.cs
using IronPrint;
// Full print configuration for a quarterly report
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"
};
Printer.Print("Q4-report.pdf", settings);
Imports IronPrint
' Full print configuration for a quarterly report
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"
}
Printer.Print("Q4-report.pdf", settings)
非同期ワークフローの場合 — WPF、MAUI、または ASP.NET ウェブアプリ — Printer.Print() を await Printer.PrintAsync() に置き換えてUIスレッドのブロックを避けます。 同じ 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リファレンスで全ての静的印刷メソッドを確認できます。
無料トライアルライセンスを取得して、ライブ環境ですべての機能をテストするか、ライセンスオプションを見ることがサービスの準備が整ったときに可能です。

