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

.NET CoreでPDFファイルを印刷する方法

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

.NET Core は、Microsoft によって開発されたオープンソースでクロスプラットフォームのフレームワークで、柔軟性、パフォーマンス、クラウドベースのアプリケーションのサポートにより人気を集めています。 しかし、PDF ドキュメントの印刷のようなタスクを扱う際には、開発者は堅牢で機能豊富な PDF ライブラリを必要とします。 ここで IronPDF は開発者を支援します。

IronPDF は、.NET フレームワーク、.NET Core、および ASP.NET Core を対象に設計された包括的なライブラリで、PDF ドキュメントを扱うプロセスを簡素化します。 それは PDF ファイルの作成と操作を可能にし、これらのドキュメントをプリンタに直接、または印刷に適した形式に変換する形で、シームレスに印刷する方法を提供します。

このチュートリアルでは、.NET Core 環境での IronPDF の機能を掘り下げます。 プロジェクトのセットアップから、最初の PDF ドキュメントを作成し、印刷設定を構成し、印刷機能を実装するまで、各ステップを説明します。このチュートリアルは、.NET Core アプリケーションで PDF ファイルを効率的に印刷するために必要な知識とツールを身につけることを目指しています。

.NET Core で PDF ファイルを印刷する方法

  1. Visual Studio で ASP.NET Core Web プロジェクトを作成します
  2. NuGet パッケージ マネージャーを使用して PDF ライブラリをインストールします
  3. コントローラーで PDF ドキュメントを作成またはロードします
  4. PDF ライブラリを使用して読み込んだ PDF ファイルを印刷します

.NET Core プロジェクトのセットアップ

IronPDF - .NET PDF ライブラリのインストール

.NET アプリケーションで PDF を扱い始めるために、最初のステップは IronPDF ライブラリを統合することです。 IronPDF は強力で多用途のライブラリであり、.NET 開発者が PDF ドキュメントを簡単に作成、編集、そして最も重要なことに印刷できるようにします。 インストールプロセスを見てみましょう:

.NET Core プロジェクトの作成: Visual Studio を開き、「Create a new project」を選択します。プロジェクト テンプレート選択画面で、すべてのプラットフォームの下にある「Web」でフィルターをかけ、「ASP.NET Core Web App」を選択します。

How To Print PDF Files in .NET Core: Figure 1 - Selecting the ASP.NET Core Web App to create a new project

IronPDF のインストール: 「NuGet パッケージ マネージャー」に移動し、「IronPDF」で検索してプロジェクトにそれをインストールします。 プロジェクト ファイルに IronPDF ライブラリが正しくインストールされ、参照されていることを確認します。 コードにusing IronPdf;などの適切なusing文を含める必要があります。

How To Print PDF Files in .NET Core: Figure 2 - Using the NuGet browser to find the IronPDF library

ASP.NET Core で基本的な PDF ドキュメントを作成する

ASP.NET Core ウェブアプリケーションで IronPDF を使用して PDF ドキュメントを作成するには、コントローラーにいくつかのコードを追加することで始めます。 始めるための簡単な例をここに示します:

新しいコントローラーのセットアップ

プロジェクトに新しいコントローラを作成します。これが PDF 作成リクエストを処理する責任を負います。 例えば、これにPdfControllerという名前を付けられます。

アクション メソッドの作成

新しいコントローラー内で、結果として PDF ファイルを返すCreatePdf というアクション メソッドを書きます。

using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for creating a PDF document
        public IActionResult CreatePdf()
        {
            // Create a new PDF document from HTML content
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");

            // Save the generated PDF to the server's memory
            var content = pdf.Stream.ToArray();

            // Return the PDF to the browser as a downloadable file
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for creating a PDF document
        public IActionResult CreatePdf()
        {
            // Create a new PDF document from HTML content
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>");

            // Save the generated PDF to the server's memory
            var content = pdf.Stream.ToArray();

            // Return the PDF to the browser as a downloadable file
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		' Action method for creating a PDF document
		Public Function CreatePdf() As IActionResult
			' Create a new PDF document from HTML content
			Dim renderer = New ChromePdfRenderer()
			Dim pdf = renderer.RenderHtmlAsPdf("<h1>Hello, IronPDF!</h1><p>This is a simple PDF document created in an ASP.NET Core web app.</p>")

			' Save the generated PDF to the server's memory
			Dim content = pdf.Stream.ToArray()

			' Return the PDF to the browser as a downloadable file
			Return File(content, "application/pdf", "MyFirstPdf.pdf")
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

アプリケーションを実行する

アプリケーションを開始し、PdfControllerCreatePdf アクションに移動します。 例えば、アプリケーションがポート5000localhostで実行されている場合、次のリンクへアクセスします。 http://localhost:<Your-Port>/Pdf/CreatePdf

PDF のダウンロード

URL にアクセスすると、PDF ドキュメントが生成され、ウェブブラウザ経由でダウンロードされます。 生成された PDF を見るには、コンピュータに PDF ビューアがインストールされている必要があります。

IronPDF を使用して .NET Core で PDF ドキュメントを印刷する

ASP.NET Core Web アプリ内で PDF ドキュメントの作成に精通したら、次に印刷機能を実装します。 IronPDF は、プロジェクト内で PDF ドキュメントを印刷する簡単な方法を提供し、アプリケーションが実行されているサーバーによってアクセス可能なプリンターに印刷します。

デフォルトプリンターとプリンタ名の設定

PDF ドキュメントを印刷するには、アプリケーション内でプリンタ設定を構成する必要があります。 IronPDF では、名前でプリンターを指定できますが、これはローカルにインストールされたプリンターかネットワークプリンターのいずれかです。 さらに、用紙ソースや向きなどの他の設定も定義できます。

こちらは、PdfController クラス内のプリンタ設定を構成し、印刷ジョブを開始する例のメソッドです:

using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for printing a PDF document
        public IActionResult PrintPdf()
        {
            // HTML string to be converted to PDF
            var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";

            // Render the HTML content to a PDF in memory
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Get the print document from the PDF
            var printDoc = pdf.GetPrintDocument();

            // Set the printer name (replace with your printer's actual name)
            printDoc.PrinterSettings.PrinterName = "Your Printer Name"; 

            // Optional: Configure additional printer settings
            // e.g., printDoc.PrinterSettings.Copies = 2;
            // e.g., printDoc.DefaultPageSettings.Landscape = true;

            // Send the document to the printer
            printDoc.Print();

            // Return a confirmation response to the client
            return Content("The document has been sent to the printer.");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;

namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        // Action method for printing a PDF document
        public IActionResult PrintPdf()
        {
            // HTML string to be converted to PDF
            var htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>";

            // Render the HTML content to a PDF in memory
            var renderer = new ChromePdfRenderer();
            var pdf = renderer.RenderHtmlAsPdf(htmlContent);

            // Get the print document from the PDF
            var printDoc = pdf.GetPrintDocument();

            // Set the printer name (replace with your printer's actual name)
            printDoc.PrinterSettings.PrinterName = "Your Printer Name"; 

            // Optional: Configure additional printer settings
            // e.g., printDoc.PrinterSettings.Copies = 2;
            // e.g., printDoc.DefaultPageSettings.Landscape = true;

            // Send the document to the printer
            printDoc.Print();

            // Return a confirmation response to the client
            return Content("The document has been sent to the printer.");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc

Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		' Action method for printing a PDF document
		Public Function PrintPdf() As IActionResult
			' HTML string to be converted to PDF
			Dim htmlContent = "<h1>Invoice</h1><p>Thank you for your business!</p>"

			' Render the HTML content to a PDF in memory
			Dim renderer = New ChromePdfRenderer()
			Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)

			' Get the print document from the PDF
			Dim printDoc = pdf.GetPrintDocument()

			' Set the printer name (replace with your printer's actual name)
			printDoc.PrinterSettings.PrinterName = "Your Printer Name"

			' Optional: Configure additional printer settings
			' e.g., printDoc.PrinterSettings.Copies = 2;
			' e.g., printDoc.DefaultPageSettings.Landscape = true;

			' Send the document to the printer
			printDoc.Print()

			' Return a confirmation response to the client
			Return Content("The document has been sent to the printer.")
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

"Your Printer Name" を環境内の実際のプリンターの名前に置き換えることを忘れないでください。 プリンターは ASP.NET Core アプリケーションが実行されているサーバーからアクセス可能でなければなりません。 プログラムを実行し、URL "**https://localhost:<Your-Port>/Pdf/PrintPdf**" にアクセスすると、PDF がプリンターに送信されたことを示す出力メッセージが表示されます。

How To Print PDF Files in .NET Core: Figure 3 - Output message from previous code

結論

このチュートリアル全体を通じて、ASP.NET Core アプリケーションでの IronPDF の機能と能力を探ってきました。 IronPDF を使用したプロジェクトのセットアップ、PDF ドキュメントの作成および操作から、これらのドキュメントを印刷するために関連するより複雑なプロセスにわたって、IronPDF は .NET Core で PDF を扱うための堅牢で多用途のツールであることが証明されています。

IronPDF の使用に興味がある人にとって、ライブラリは 無料試用版 を提供しており、コミットする前にその機能を評価できます。 ニーズに合う場合、IronPDF のライセンスは $799 から始まり、小規模および大規模プロジェクトの両方に対応可能なスケーラブルなソリューションを提供します。 以下に IronXL ライセンスの価格設定が表示されており、詳細を表示するにはここをクリックしてください。

How To Print PDF Files in .NET Core: Figure 4 - IronPDF licensing page

よくある質問

.NET CoreプロジェクトでPDFを印刷するためのセットアップ方法は?

PDFを印刷するために.NET Coreプロジェクトをセットアップするには、Visual Studioで新しいASP.NET Core Webプロジェクトを作成し、NuGetパッケージマネージャーを介してIronPDFをインストールします。このセットアップにより、PDFの作成と印刷にIronPDFの機能を利用できるようになります。

.NET CoreでPDFドキュメントを印刷するにはどのような手順がありますか?

.NET CoreでPDFを印刷するには、IronPDFを使用してPDFドキュメントを作成または読み込み、プリンター設定を構成し、アプリケーションからの印刷コマンドを実行してドキュメントをプリンターに送信します。

ASP.NET CoreでHTMLコンテンツをPDFに変換する方法は?

IronPDFのChromePdfRendererクラスを使用して、HTML文字列またはファイルを効率的にPDFドキュメントにレンダリングして、ASP.NET CoreでHTMLコンテンツをPDFに変換できます。

.NET CoreアプリケーションからIronPDFで直接プリンターに印刷できますか?

はい、IronPDFは.NET Coreアプリケーションから直接プリンターに印刷できます。コード内でプリンター設定を構成し、IronPDFのメソッドを使用して印刷ジョブを開始する必要があります。

PDFを印刷するときにどのようなプリンター設定を構成できますか?

IronPDFを使用してPDFを印刷する際には、プリンター名、印刷部数、ページの向き、およびその他の関連する印刷オプションをアプリケーションコード内で直接構成できます。

IronPDFを購入前に試すことは可能ですか?

はい、購入を決定する前にその機能と能力を探ることができる無料トライアルがあります。

IronPDFと互換性のあるオペレーティングシステムは?

IronPDFはWindows、macOS、Android、iOSなどの複数のオペレーティングシステムと互換性があり、クロスプラットフォーム開発に適した柔軟なソリューションです。

.NET CoreでPDFを印刷する際の一般的な問題を解決する方法は?

.NET CoreでPDFを印刷する際の一般的な問題は、プリンターの設定を確認し、IronPDFが正しくインストールされていることを確認し、印刷前にドキュメントの形式を検証することによってしばしば解決できます。

Curtis Chau
テクニカルライター

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

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