IRONPRINTの使用

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

更新済み 1月 14, 2024
共有:

イントロダクション

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

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

このチュートリアルでは、.NET Core環境におけるIronPDF (アイアンPDF)の機能を掘り下げていきます。 プロジェクトのセットアップや最初の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 (アイアンPDF) ライブラリを統合します。 IronPDF (アイアンPDF)は、.NET開発者が簡単にPDFドキュメントを作成、編集、そして最も重要な印刷ができる強力で多機能なライブラリです。 では、インストール手順を説明しよう:

.NET Coreプロジェクトの作成:Visual Studioを開き、"Create a new project "を選択する。プロジェクトのテンプレート選択ウィンドウで、"All platforms "の "Web "でフィルタリングし、"ASP.NET Core Web App "を選択します。

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

IronPDFのインストール:NuGet Package Manager "で "IronPDF "を検索してプロジェクトにインストールしてください。 IronPDFライブラリが正しくインストールされ、プロジェクトファイルで参照されていることを確認してください。 あなたのコードに適切な using ステートメントを含める必要があります。

.NET CoreでPDFファイルを印刷する方法:図2 - IronPDF (アイアンPDF)ライブラリを見つけるためにNuGetブラウザーを使う

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

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

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

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

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

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

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
VB   C#

アプリケーションの実行

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

PDFをダウンロード

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

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

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

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

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

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
VB   C#

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

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

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

結論

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

IronPDF (アイアンPDF)を利用することに興味がある人にとって、このライブラリが以下のものを提供していることは注目に値する。 無料試用そのため、契約前にその機能を評価することができる。 アイアンPDFのライセンスは$599からお求めいただけますので、小規模なプロジェクトから大規模なプロジェクトまでスケーラブルに対応できます。 IronXL (アイアンXL)のライセンス価格は以下の通りです。 これ をクリックしてください。

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

< 以前
ネットワークプリンターを使用してIronPDFからPDFを印刷する方法
次へ >
IronPDFを使用してC#でPDFファイルを印刷する

準備はできましたか? バージョン: 2024.8 新発売

無料のNuGetダウンロード 総ダウンロード数: 7,144 View Licenses >