C#で印刷の紙の余白を設定する方法
印刷の余白は、ドキュメントの内容と紙の端との間の空白を管理します。 正しく設定することで、文字の切断を防ぎ、プリンター間で一貫したレイアウトを保証し、請求書、報告書、法的文書の書式設定要件を満たします。
IronPrintのMarginsクラスはミリメートル単位で値を受け入れ、均一、水平/垂直、およびサイドごとの3つのコンストラクタのオーバーロードを提供します。これにより、あらゆるレイアウト要件に合わせて、1行でマッチさせることができます。以下に、インストールからカスタム余白での印刷まで、各アプローチを説明します。
クイックスタート: 紙の余白を設定する
- NuGetを介してIronPrintをインストールします:
Install-Package IronPrint - ファイルに
using IronPrint;を追加します PrintSettingsオブジェクトを作成しますPaperMarginsにMargins値を割り当てます (ミリメートル単位の値)- ファイルパスと共に、設定を
Printer.Print()に渡します
-
IronPrint をNuGetパッケージマネージャでインストール
-
このコード スニペットをコピーして実行します。
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;
// 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)
Margins(20)はLeft、Top、Right、Bottomのそれぞれに20 mmを設定します。 標準ビジネス文書では、すべての端に均等な空白があれば十分であるため、これが最も一般的な選択です。
IronPrintは、インチの百分の一を使用する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;
// 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)
パラメータの順序はleft、top、right、bottomです。 各値は独立しており、ヘッダー、フッター、綴じ代、パンチホールスペースを考慮した非対称レイアウトを作成できます。 MarginsクラスAPIリファレンスはすべてのフィールドを文書化しています。
一般的な余白レイアウトにはどのような省略オプションがありますか?
IronPrintのMarginsクラスは、均一バージョンおよびサイドごとのバージョンに加えて、1つの追加のコンストラクタと1つの静的な簡略記法フィールドを提供します:
水平/垂直の簡略記法 — 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;
// 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)
ゼロ余白 — Margins.Zeroは、境界なし印刷のためにすべての余白を除去します:
:path=/static-assets/print/content-code-examples/how-to/set-paper-margins/set-paper-margins-zero-margins.cs
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)
ほとんどの物理的プリンターは、ハードウェアの最小印刷可能領域を強制することに注意してください。 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;
// 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)
非同期ワークフロー (WPF、MAUI、またはASP.NET webアプリ) のために、UIスレッドのブロックを避けるために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とは何であり、C#での印刷マージン設定にどのように役立ちますか?
IronPrintはC#で印刷マージンを設定するのを簡素化する.NETライブラリであり、Marginsクラスを提供し、開発者が簡単に一様、各辺ごと、ボーダーレスオプションを使用して印刷マージンをカスタマイズすることができます。
IronPrintを使用してC#で均一なマージンを設定するにはどうすればよいですか?
C#での印刷に均一なマージンを設定するには、IronPrintのMarginsクラスを使用します。このクラスにより、用紙のすべての辺に同じマージンサイズを1行のコードで指定できます。
C#でページの各辺に異なるマージンを設定することは可能ですか?
はい、IronPrintを使えば、C#でページの各辺に異なるマージンを設定できます。Marginsクラスは、上、下、左、右のマージンを個別にカスタマイズするオプションを提供します。
IronPrintを使用してボーダーレス印刷を作成できますか?
IronPrintはボーダーレス印刷の作成をサポートします。Marginsクラスを使用してマージンを調整することにより、マージンをゼロに設定し、効果的にボーダーレス印刷を実現することができます。
紙のマージンを設定するためにIronPrintを使用する利点は何ですか?
IronPrintは、C#での紙マージン設定を簡素で効率的なAPIで合理化します。Marginsクラスはコードを簡素化し、生産性を向上させるため、カスタム印刷要件を実装しやすくします。
広範なコーディング知識なしでIronPrintを使用して印刷マージンを設定できますか?
いいえ、IronPrintを使用して印刷マージンを設定するには広範なコーディング知識は必要ありません。このライブラリはユーザーフレンドリーに設計されており、基本的なC#スキルを持つ人でも簡単にカスタムマージンを実装できるようにしています。
IronPrintはマージン設定時に異なる用紙サイズをどのように処理しますか?
IronPrintはさまざまな用紙サイズに適応し、ドキュメントの特定の寸法に合わせたマージンを指定できるようにします。この柔軟性をもって印刷出力が特定のレイアウトニーズを満たすことを保証します。
IronPrintは他 for .NETアプリケーションと統合できますか?
はい、IronPrintは他 for .NETアプリケーションとシームレスに統合でき、幅広いプロジェクトやワークフローにカスタム印刷マージン設定を組み込むことができます。

