フッターコンテンツにスキップ
IRONPRINTの使用

C# プリンターにPDFを送信(ステップバイステップチュートリアル)

role="alert"> IronPrintは、Iron Softwareの新しい.NET印刷ライブラリで、Windows、macOS、Android、iOSを含む幅広いプラットフォームとの互換性を提供します。IronPrintを始めましょう

PDFは「ポータブルドキュメントフォーマット」を意味します。 開発者がアプリケーションでPDFファイルをプログラムで印刷する必要があるシナリオは多くあります。 C#では、これは非常に面倒な作業になる可能性がありますが、IronPDFのおかげで、数行のコードで非常に簡単にできるようになりました。 このツールを使用すると、デフォルトのプリンター設定だけでなく、カスタム印刷オプションでもPDFドキュメントを印刷できます。 このチュートリアルでは、C#言語を使用してPDFを印刷する方法を学びます。

このチュートリアルで取り上げるトピック

ここで取り上げるトピックは以下の通りです:

  • IronPDF ライブラリ
  • C# コンソールプロジェクトの作成
  • IronPDF のインストール
    1. NuGet パッケージマネージャー
    2. NuGet パッケージマネージャーコンソール
    3. DLL ファイルの使用
  • IronPDF 名前空間の追加
  • PDFドキュメントの印刷
    1. PDFドキュメントの作成と印刷
    2. URLからPDFドキュメントを作成して印刷
    3. 高度な印刷
  • 要約
class="hsg-featured-snippet">

C#でPrinterにPDFを送信する方法

  1. PrinterにPDFを送信するC#ライブラリをインストール
  2. Printメソッドを利用してデフォルトプリンター設定でPDFを印刷
  3. PrinterNameプロパティを設定して特定のプリンターに送信
  4. PrinterResolutionプロパティを設定してプリンターの解像度をカスタマイズ
  5. C#で印刷されたページの数量を追跡

IronPDF

IronPDFは、.NET Framework 用のPDFライブラリで、開発者が簡単にPDFファイルを作成できるようにします。 IronPDFのレンダリングは、Google Chromeのデスクトップ版に対して「ピクセルパーフェクト」です。 IronPDFは、1行のコードで簡単にPDFドキュメントを作成します。 それは、Acrobat Readerや他のPDFビューアーを使わずにPDFドキュメントを処理できます。

IronPDFは、HTML文字列、HTMLファイル、またはURLからPDFファイルを作成するために使用できます。 その後、これらのファイルをデフォルトのプリンターに送り、印刷することができます。

A free trial of IronPDF is available.

IronPDFライブラリのいくつかの重要な機能

  • HTML 4および5、CSS、JavaScriptからPDFドキュメントを作成
  • URLからPDFドキュメントを生成
  • PDFをデフォルトの物理プリンターに印刷
  • 印刷ジョブの設定(特定のページを印刷するなど)
  • カスタムネットワークのログイン資格情報、ユーザーエージェント、プロキシ、クッキー、HTTPヘッダー、およびフォームフィールドや変数を使用してURLを読み込み、HTMLログインフォームの背後にあるウェブページへのアクセスを可能にする
  • PDF(ポータブルドキュメントフォーマット)のフォームフィールドデータを読み取りおよび記入
  • PDFファイルから画像とテキストを抽出
  • PDFドキュメントにデジタル署名
  • サードパーティライブラリは必要ありません

1. C#プロジェクトを作成する

このチュートリアルでは、Visual Studio 2022を使用しますが、以前のバージョンを使用することもできます。

  • Visual Studio 2022を開きます。
  • 新しいC# .NETコンソールプロジェクトを作成します。 .NET Coreコンソールアプリケーションを選択します。
class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図1:コンソールアプリケーション

class="content__image-caption">コンソールアプリケーション

  • プロジェクトに名前を付けます。例:DemoApp。
  • 使用する最新で最も安定したバージョンである.NET Framework 6.0。 「作成」ボタンをクリックします。
class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図2:.NET Framework

class="content__image-caption">.NET Framework

2. IronPDFライブラリをインストールする

IronPDFライブラリをインストールするには、以下の方法を使用できます。

2.1. NuGet パッケージマネージャー

NuGet パッケージマネージャーから、IronPDF C# .NET Core ライブラリをインストールできます。

ツール > NuGet パッケージマネージャー > ソリューション用のNuGetパッケージの管理をクリックして、パッケージマネージャーを開きます。

class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図3:パッケージマネージャー

class="content__image-caption">パッケージマネージャー

または、ソリューションエクスプローラーでプロジェクトを右クリックし、NuGetパッケージの管理をクリックします。

class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図4:NuGet パッケージマネージャー - ソリューションエクスプローラー

class="content__image-caption">NuGet パッケージマネージャー - ソリューションエクスプローラー

IronPDFを検索します。 IronPDFを選択し、インストールします。 ライブラリのインストールを開始します。

class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図5:IronPDFをインストールする

class="content__image-caption">IronPDFをインストール

2.2. NuGet パッケージマネージャーコンソール

ツール > NuGet パッケージマネージャー > パッケージマネージャーコンソールをクリックしてNuGetパッケージマネージャーコンソールを開きます。

コマンドラインに次のコマンドを入力します。

Install-Package IronPrint
class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図6:NuGetパッケージマネージャーコンソール

class="content__image-caption">パッケージマネージャーコンソール

2.3. DLL ファイルの使用

プロジェクトでIronPDFを使用する別の方法は、IronPDFライブラリからDLLファイルを追加することです。 このリンクからDLLファイルをダウンロードできます。

  • DLL zipファイルをダウンロードし、特定のフォルダーに解凍します。
  • Visual Studio でプロジェクトを開きます。 ソリューションエクスプローラーで「参照」を右クリックし、IronPDF DLLファイルを参照します。

2.4. IronPDF名前空間を追加

インストールが終了したら、プログラムファイルにIronPDFとSystem.Drawing名前空間を追加します。

using IronPdf;
using System.Drawing;
using IronPdf;
using System.Drawing;
Imports IronPdf
Imports System.Drawing
$vbLabelText   $csharpLabel

注意:IronPDFの機能を使用するファイルには、これらの参照を毎回追加する必要があります。

IronPDFがインストールされ、使用する準備ができました! これで、.NET Coreアプリケーション用の最初のPDFドキュメントを作成し、デフォルトのプリンターに送信して印刷できます。 次に、コード例を使用してその中のいくつかを見てみましょう。

3. PDFドキュメントの印刷

3.1. HTMLからPDFドキュメントを作成して印刷する

HTML文字列を処理してPDF形式に変換するのはとても簡単です。 この新しく作成されたファイルは、その後IronPDFを使用して印刷できます。 ここにPDFを簡単に作成するコードがあります。

// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();

// Render any HTML fragment to a PDF document
using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>");

// Send the PDF to the default printer
pdfDocument.Print();

// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();

// Render any HTML fragment to a PDF document
using var pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>");

// Send the PDF to the default printer
pdfDocument.Print();

// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
' Create an instance of ChromePdfRenderer
Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer()

' Render any HTML fragment to a PDF document
Dim pdfDocument = chromePdfRenderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1><p>This tutorial will help to print this text to a PDF file.</p>")

' Send the PDF to the default printer
pdfDocument.Print()

' Create a PrintDocument object that can be used for further configurations
Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
$vbLabelText   $csharpLabel

このコードは、RenderHtmlAsPdf関数に渡されたHTMLコンテンツでPDFファイルを作成します。 この関数は、HTMLフラグメントをPDFドキュメントに変換します。

IronPDFライブラリを使用してPDFファイルやPDFページを生成するには、HTMLタグに精通している必要があります。 Print関数を使用して、PDFファイルの出力をプリンターに送信します。 プリンターダイアログが表示され、印刷ジョブを確認することができます。

3.2. URLからPDFドキュメントを作成して印刷する

URLを使用してPDFドキュメントを作成することもできます。

// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();

// Render a PDF from a URL
var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer
pdfDocument.Print();

// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
// Create an instance of ChromePdfRenderer
var chromePdfRenderer = new IronPdf.ChromePdfRenderer();

// Render a PDF from a URL
var pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/");

// Send the PDF to the default printer
pdfDocument.Print();

// Create a PrintDocument object that can be used for further configurations
System.Drawing.Printing.PrintDocument printDocument = pdfDocument.GetPrintDocument();
' Create an instance of ChromePdfRenderer
Dim chromePdfRenderer = New IronPdf.ChromePdfRenderer()

' Render a PDF from a URL
Dim pdfDocument = chromePdfRenderer.RenderUrlAsPdf("https://ironpdf.com/")

' Send the PDF to the default printer
pdfDocument.Print()

' Create a PrintDocument object that can be used for further configurations
Dim printDocument As System.Drawing.Printing.PrintDocument = pdfDocument.GetPrintDocument()
$vbLabelText   $csharpLabel

ここで、指定されたURLからPDFファイルが作成され、その後印刷されます。

class="content-img-align-center">
class="center-image-wrapper"> C#を使用してPDFをプリンターに送信する方法、図7:URLから生成されたPDFを印刷

class="content__image-caption">URLから生成されたPDFを印刷

4. 高度な印刷オプション

IronPDFは多機能で、プリンターの検索や印刷解像度の設定など、印刷機能を扱う能力を持っています。

4.1 プリンターを指定する

プリンターを指定するには、現在の印刷ドキュメントオブジェクト(GetPrintDocumentメソッドを使用)を取得し、PrinterSettings.PrinterNameプロパティを使用します。 任意の利用可能なプリンターを選択できます。

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Print the document
    printDocument.Print();
}
using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Print the document
    printDocument.Print();
}
Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Print the document
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

上記のコードサンプルでは、「Microsoft Print to PDF」がプリンターとして選択されています。 特定の印刷設定の設定に関する詳細情報は、ドキュメンテーションページにあります。

4.2 プリンターの解像度を設定

PDFを印刷するための解像度を設定することもできます。 解像度は、ピクセルの数に関して印刷物の品質を指します。 印刷ドキュメントの解像度を、PDFドキュメントのDefaultPageSettings.PrinterResolutionプロパティを使用して設定できます。

// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
    Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
// Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
{
    Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
    X = 1200,
    Y = 1200
};
' Set custom printer resolution
printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With {
	.Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
	.X = 1200,
	.Y = 1200
}
$vbLabelText   $csharpLabel

4.3 C#を使用した印刷プロセスのトレース

次のコード例では、プリンター名を変更し、解像度を設定し、印刷されたページの数を取得する方法を示します。

int printedPages;

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
    {
        Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Track number of printed pages
    printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;

    // Print the document
    printDocument.Print();
}
int printedPages;

using (var printDocument = pdfDocument.GetPrintDocument())
{
    // Specify the printer name
    printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF";

    // Set custom printer resolution
    printDocument.DefaultPageSettings.PrinterResolution = new System.Drawing.Printing.PrinterResolution
    {
        Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
        X = 1200,
        Y = 1200
    };

    // Track number of printed pages
    printedPages = 0;
    printDocument.PrintPage += (sender, args) => printedPages++;

    // Print the document
    printDocument.Print();
}
Dim printedPages As Integer

Using printDocument = pdfDocument.GetPrintDocument()
	' Specify the printer name
	printDocument.PrinterSettings.PrinterName = "Microsoft Print to PDF"

	' Set custom printer resolution
	printDocument.DefaultPageSettings.PrinterResolution = New System.Drawing.Printing.PrinterResolution With {
		.Kind = System.Drawing.Printing.PrinterResolutionKind.Custom,
		.X = 1200,
		.Y = 1200
	}

	' Track number of printed pages
	printedPages = 0
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: printDocument.PrintPage += (sender, args) => printedPages++;
	AddHandler printDocument.PrintPage, Sub(sender, args) printedPages
	printedPages += 1

	' Print the document
	printDocument.Print()
End Using
$vbLabelText   $csharpLabel

5. 要約

IronPDFはPDFドキュメントを扱うための完全なソリューションです。 さまざまな形式からPDFへの変換機能を提供します。 PDFファイルの操作と書式設定は、IronPDFライブラリ関数を使用すると非常に簡単になります。 必要なことは、PDFファイルを作成し、フォーマットするための数行のコードだけです。また、プログラム上でPDFを印刷することもできます。 これは、PDFをコンピューターのデフォルトプリンターに送信することによって行われます。 私たちは、ユーザーに印刷ダイアログウィンドウを表示したり、Printメソッドのオーバーロードを使用して静かに印刷することができます。

A free trial of IronPDF is also available to test its full potential to generate and print PDF documents in your applications. ライセンスに関する詳細情報はこのリンクで見つけることができます。

さらに、現在の特別オファーでは、Iron Software製品5つを2つの価格で取得できます。

よくある質問

C#でプログラム的にPDFドキュメントを印刷するにはどうすればよいですか?

C#でプログラム的にPDFドキュメントを印刷するにはIronPDFが使用できます。Printなどのメソッドを活用することで、デフォルトまたは指定されたプリンターにカスタマイズ可能な印刷オプションでPDFを送信できます。

C#で印刷用のPDFライブラリをインストールする手順は何ですか?

IronPDFライブラリをインストールするには、Visual StudioのNuGetパッケージマネージャー、NuGetパッケージマネージャーコンソールを使用するか、DLLファイルを介してライブラリを追加できます。

このライブラリを使用してHTMLからPDFを作成できますか?

はい、IronPDFを使用すると、RenderHtmlAsPdfメソッドを使用してHTMLからPDFを作成できます。これにより、HTML、CSS、およびJavaScriptをPDFドキュメントに変換できます。

このライブラリを使用して特定のプリンターにPDFを送信するにはどうすればよいですか?

特定のプリンターにPDFを送信するには、IronPDF内のPrintDocumentオブジェクトのPrinterSettings.PrinterNameプロパティを希望のプリンター名に設定することができます。

このライブラリがサポートする高度な印刷オプションは何ですか?

IronPDFは、プリンター設定の指定、印刷解像度の調整、印刷されたページの数の追跡などの高度な印刷オプションをサポートしています。

このPDFライブラリでカスタム印刷解像度を設定することは可能ですか?

はい、IronPDFのPrintDocumentオブジェクトのDefaultPageSettings.PrinterResolutionを使用してカスタム印刷解像度を設定することができます。

このライブラリでURLをPDFとしてレンダリングするにはどうすればよいですか?

IronPDFのRenderUrlAsPdfメソッドを使用すると、URLをPDFとしてレンダリングすることができ、ウェブページをPDFドキュメントに変換できます。

このライブラリは無料のトライアルを提供していますか?

はい、IronPDFは無料のトライアルを提供しており、ユーザーはPDFドキュメントの生成および印刷機能を探索できます。

このライブラリを使用して印刷されたページの数を追跡できますか?

はい、IronPDFを使用すると、印刷ジョブ中にPrintDocumentオブジェクトのプロパティにアクセスすることで印刷されたページの数を追跡することができます。

このPDF印刷ライブラリはどのプラットフォームに互換性がありますか?

IronPDFはWindows、macOS、Android、iOSなどのさまざまなプラットフォームに対応しており、異なる開発環境に柔軟に対応します。

Curtis Chau
テクニカルライター

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

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