IRONPRINTの使用

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

チャクニット・ビン
チャクニット・ビン
2024年1月14日
共有:

イントロダクション

マイクロソフトが開発したオープンソースのクロスプラットフォームフレームワークである.NETコアは、その柔軟性、パフォーマンス、クラウドベースのアプリケーションのサポートで人気を集めている。 しかし、特にPDFドキュメントを印刷するような作業を行う場合、開発者は堅牢で機能が充実したPDFライブラリを必要とします。 そこでIronPDFが開発者をサポートします。

IronPDF は、.NETフレームワーク (.NET CoreおよびASP.NET Coreを含む) 用に設計された包括的なライブラリで、PDF文書を扱うプロセスを簡素化します。 PDFファイルの作成と操作ができるだけでなく、これらの文書を直接プリンターで印刷したり、印刷に適した形式に変換したりと、シームレスな印刷方法を提供します。

このチュートリアルでは、.NET Core環境におけるIronPDFの機能を掘り下げていきます。 プロジェクトのセットアップや最初のPDFドキュメントの作成から、印刷設定の構成や高度な印刷機能の実装まで、各ステップを通してご案内します。このチュートリアルは、.NETコアアプリケーションで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 を開き、「新しいプロジェクトの作成」を選択します。プロジェクトテンプレート選択ウィンドウで、「すべてのプラットフォーム」の下にある「Web」でフィルターし、「ASP.NET Core Web アプリ」を選択します。

.NET CoreでPDFファイルを印刷する方法: 図1 - 新しいプロジェクトを作成するためのASP.NET Core Web Appの選択

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

.NET CoreでPDFファイルを印刷する方法: 図2 - NuGetブラウザを使用してIronPDFライブラリを見つける

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

ASP.NET Core WebアプリケーションでIronPDFを使ってPDFドキュメントを作成するには、まずコントローラにコードを追加します。 簡単な例を挙げよう:

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

プロジェクトに新しいコントローラを作成し、PDF作成要求の処理を担当させます。 例えばPdfControllerと名付けることができます。

アクション・メソッドを書く

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

using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
namespace YourProjectName.Controllers
{
    public class PdfController : Controller
    {
        public IActionResult CreatePdf()
        {
            // Create a new PDF document
            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 PDF to the server's memory
            var content = pdf.Stream.ToArray();
            // Return the PDF to the browser as a file download
            return File(content, "application/pdf", "MyFirstPdf.pdf");
        }
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Namespace YourProjectName.Controllers
	Public Class PdfController
		Inherits Controller

		Public Function CreatePdf() As IActionResult
			' Create a new PDF document
			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 PDF to the server's memory
			Dim content = pdf.Stream.ToArray()
			' Return the PDF to the browser as a file download
			Return File(content, "application/pdf", "MyFirstPdf.pdf")
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

アプリケーションの実行

アプリケーションを起動し、PdfController 内の CreatePdf アクションに移動します。 たとえば、アプリケーションがポート5000localhostで実行されている場合、ウェブブラウザでhttp://localhost:<Your-Port>/Pdf/CreatePdfにアクセスしてください。

PDFをダウンロード

URLにアクセスすると、PDF文書が生成され、ウェブブラウザからダウンロードされます。 生成されたPDFをご覧になりたい場合は、お使いのコンピュータでPDFビューアを使用してお読みください。

IronPDFを使用した.NETコアでのPDFドキュメントの印刷

ASP.NET Core Web AppでPDFドキュメントの作成をマスターしたら、次のステップは印刷機能を実装することです。 IronPDFはプロジェクト内でPDFドキュメントを印刷する簡単な方法を提供します。

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

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

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

using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
using IronPdf;
using Microsoft.AspNetCore.Mvc;
public class PdfController : Controller
{
    public IActionResult PrintPdf()
    {
        // Assuming 'htmlContent' is the HTML string you want to print
        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 PrintDocument from the PDF
        var printDoc = pdf.GetPrintDocument();
        // Set the printer name
        printDoc.PrinterSettings.PrinterName = "Your Printer Name"; // Replace with your printer's name
        // Optional: Set other printer settings
        // printDoc.PrinterSettings.Copies = 2;
        // printDoc.DefaultPageSettings.Landscape = true;
        // Print the document
        printDoc.Print();
        // Return a response to the client, e.g., a confirmation message
        return Content("The document has been sent to the printer.");
    }
}
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc
Public Class PdfController
	Inherits Controller

	Public Function PrintPdf() As IActionResult
		' Assuming 'htmlContent' is the HTML string you want to print
		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 PrintDocument from the PDF
		Dim printDoc = pdf.GetPrintDocument()
		' Set the printer name
		printDoc.PrinterSettings.PrinterName = "Your Printer Name" ' Replace with your printer's name
		' Optional: Set other printer settings
		' printDoc.PrinterSettings.Copies = 2;
		' printDoc.DefaultPageSettings.Landscape = true;
		' Print the document
		printDoc.Print()
		' Return a response to the client, e.g., a confirmation message
		Return Content("The document has been sent to the printer.")
	End Function
End Class
$vbLabelText   $csharpLabel

Your Printer Name "は、お使いの環境での実際のプリンタ名に置き換えてください。 プリンターは、ASP.NETコアアプリケーションが実行されているサーバーからアクセスできる必要があります。 プログラムを実行し、次のURL「**https://localhost:<Your-Port>/Pdf/PrintPdf**」にアクセスすると、次のメッセージが表示されます:

.NET CoreでPDFファイルを印刷する方法: 図3 - 前述のコードからの出力メッセージ

PDFがプリンターに送られることを意味します。

結論

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

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

.NET CoreでPDFファイルを印刷する方法:図4 - IronPDFライセンスページ

チャクニット・ビン
ソフトウェアエンジニア
ChaknithはIronXLとIronBarcodeで作業しています。彼はC#と.NETに深い専門知識を持ち、ソフトウェアの改善と顧客サポートを支援しています。ユーザーとの対話から得た彼の洞察は、より良い製品、文書、および全体的な体験に貢献しています。
< 以前
ネットワークプリンターを使用してIronPDFからPDFを印刷する方法
次へ >
IronPDFを使用してC#でPDFファイルを印刷する