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

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;

// 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)LeftTopRightBottomのそれぞれに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)
$vbLabelText   $csharpLabel

パラメータの順序はlefttoprightbottomです。 各値は独立しており、ヘッダー、フッター、綴じ代、パンチホールスペースを考慮した非対称レイアウトを作成できます。 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)
$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 = 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)
$vbLabelText   $csharpLabel

ほとんどの物理的プリンターは、ハードウェアの最小印刷可能領域を強制することに注意してください。 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)
$vbLabelText   $csharpLabel

非同期ワークフロー (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とは何であり、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
テクニカルライター

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

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

準備はできましたか?
Nuget ダウンロード 44,051 | バージョン: 2026.7 リリースされたばかり
Still Scrolling Icon

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

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