C#で印刷の紙の余白を設定する方法

This article was translated from English: Does it need improvement?
Translated
View the article in English

印刷の余白は、ドキュメントの内容と紙の端との間の空白を管理します。 正しく設定することで、文字の切断を防ぎ、プリンター間で一貫したレイアウトを保証し、請求書、報告書、法的文書の書式設定要件を満たします。

IronPrintのMarginsクラスはミリメートル単位の値を受け付け、uniform、horizontal/vertical、per-sideの3つのコンストラクタオーバーロードを提供しているため、1行のコードであらゆるレイアウト要件に対応できます。以下では、インストールからカスタム余白での印刷まで、各アプローチについて順を追って説明します。

クイックスタート: 紙の余白を設定する

  1. NuGet 経由で IronPrint をインストールします: Install-Package IronPrint
  2. ファイルに using IronPrint; を追加してください
  3. PrintSettings オブジェクトを作成する
  4. Margins の値を PaperMargins に割り当ててください(値はミリメートル単位)。
  5. ファイルパスを指定して設定を Printer.Print() に渡す
  1. IronPrint をNuGetパッケージマネージャでインストール

    PM > Install-Package IronPrint
  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);
  3. 実際の環境でテストするためにデプロイする

    今日プロジェクトで IronPrint を使い始めましょう無料トライアル

    arrow pointer

すべての辺に均等な余白を設定できますか?

最も単純なコンストラクタは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)
$vbLabelText   $csharpLabel

Margins(20) は、Right、および Bottom をそれぞれ 20 mm に設定します。 標準ビジネス文書では、すべての端に均等な空白があれば十分であるため、これが最も一般的な選択です。

IronPrintは余白をミリメートル単位で測定するため、100分の1インチを使用する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)
$vbLabelText   $csharpLabel

パラメータの順序は、bottom です。 各値は独立しており、ヘッダー、フッター、綴じ代、パンチホールスペースを考慮した非対称レイアウトを作成できます。 Margins クラスの API リファレンスでは、すべてのフィールドについて解説しています。

一般的な余白レイアウトにはどのような省略オプションがありますか?

IronPrintのMarginsクラスは、統一版およびページ単位版に加え、さらに2つのコンストラクタを提供しています:

水平/垂直の省略表記Margins(int horizontal, int vertical) は、left+right を最初の値に、top+bottom を 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)
$vbLabelText   $csharpLabel

余白なし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 = new Margins(0)
};

// 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 = New Margins(0)
}

' Print the poster
Printer.Print("poster.png", borderless)
$vbLabelText   $csharpLabel

ほとんどの物理的プリンターは、ハードウェアの最小印刷可能領域を強制することに注意してください。 Margins.Zeroを設定すると、ドライバーに余白ゼロの指示が送信されますが、プリンターの機能によっては、端付近のコンテンツが切り取られる場合があります。

余白を他の印刷設定と組み合わせるにはどうすれば良いですか?

PaperMarginsPrintSettings 上のプロパティの一つです。 用紙サイズ、向き、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)
$vbLabelText   $csharpLabel

非同期ワークフロー(WPF、MAUI、またはASP.NET Webアプリ)では、UIスレッドのブロックを防ぐために、await Printer.PrintAsync()に置き換えてください。 同じ PrintSettings オブジェクトが、両方のメソッドで動作します。

次のステップは何ですか?

Using IronPrint, we covered four ways to set print margins: uniform margins using Margins(int), per-side control using Margins(int, int, int, int), horizontal/vertical shorthand using Margins(int, int), and borderless printing using Margins.Zero. 各アプローチは PrintSettings.PaperMargins に組み込まれており、Printer.Print() および Printer.PrintAsync() の両方と連携します。

さらなる学習のために、これらのリソースを探検してください:

無料トライアルライセンスを取得して、ライブ環境ですべての機能をテストするか、ライセンスオプションを見ることがサービスの準備が整ったときに可能です。

よくある質問

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アプリケーションとシームレスに統合でき、幅広いプロジェクトやワークフローにカスタム印刷マージン設定を組み込むことができます。

カーティス・チャウ
テクニカルライター

Curtis Chauは、カールトン大学でコンピュータサイエンスの学士号を取得し、Node.js、TypeScript、JavaScript、およびReactに精通したフロントエンド開発を専門としています。直感的で美しいユーザーインターフェースを作成することに情熱を持ち、Curtisは現代のフレームワークを用いた開発や、構造の良い視覚的に魅力的なマニュアルの作成を楽しんでいます。

開発以外にも、CurtisはIoT(Internet of Things)への強い関心を持ち、ハードウェアとソフトウェアの統合方法を模索しています。余暇には、ゲームをしたりDiscordボットを作成したりして、技術に対する愛情と創造性を組み合わせています。

準備はできましたか?
Nuget ダウンロード 41,154 | バージョン: 2026.5 just released
Still Scrolling Icon

まだスクロールしていますか?

すぐに証拠が欲しいですか? PM > Install-Package IronPrint
サンプルを実行する プリンターに出力されるドキュメントを見る。