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

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

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

IronPrint の Margins クラスはミリメートルで値を受け取ることができ、均等、水平/垂直、各辺別の3つのコンストラクタのオーバーロードを提供するので、1行で任意のレイアウト要件に合わせることができます。以下に、インストールからカスタム余白での印刷までの各アプローチを示します。

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

  1. NuGet を使用して IronPrint をインストールします: Install-Package IronPrint
  2. using IronPrint; をファイルに追加します
  3. PrintSettings オブジェクトを作成します
  4. PaperMarginsMargins 値を割り当てます(ミリメートル単位の値)
  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;

// 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)
$vbLabelText   $csharpLabel

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)
$vbLabelText   $csharpLabel

パラメータの順番は 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)
$vbLabelText   $csharpLabel

ゼロ余白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)
$vbLabelText   $csharpLabel

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

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

PaperMarginsPrintSettings の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)
$vbLabelText   $csharpLabel

非同期ワークフローの場合 — 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() で動作します。

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

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

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

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

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

準備はできましたか?
Nuget ダウンロード 38,930 | バージョン: 2026.4 リリース
Still Scrolling Icon

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

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