.NET CoreでPDFファイルを印刷する方法
Microsoft が開発したオープンソースのクロスプラットフォーム フレームワークである .NET Core は、その柔軟性、パフォーマンス、クラウドベースのアプリケーションのサポートにより人気が高まっています。 ただし、PDF ファイルの操作、特にPDF ドキュメントの印刷などのタスクに関しては、開発者は強力で機能豊富な PDF ライブラリを必要とします。 ここで IronPDF が開発者を支援します。
IronPDF は、.NET Core や ASP.NET Core を含む .NET フレームワーク向けに設計された包括的なライブラリであり、PDF ドキュメントの操作プロセスを簡素化します。 PDF ファイルの作成と操作が可能になるだけでなく、直接プリンターに印刷するか、印刷に適した形式に変換して、これらのドキュメントをシームレスに印刷する方法も提供します。
このチュートリアルでは、.NET Core 環境内での IronPDF の機能について詳しく説明します。 プロジェクトの設定から最初のPDFドキュメントの作成、印刷設定の構成、高度な印刷機能の実装まで、各ステップを丁寧に解説します。このチュートリアルでは、.NET CoreアプリケーションでPDFファイルの印刷を効率的に処理するために必要な知識とツールを習得することを目的としています。
.NET CoreでPDFファイルを印刷する方法
- Visual StudioでASP.NET Core Webプロジェクトを作成する
- NuGet パッケージ マネージャーを使用して PDF ライブラリをインストールする
- コントローラーでPDFドキュメントを作成またはロードする
- PDFライブラリを使用して、読み込んだPDFファイルを印刷します。
.NET Core プロジェクトのセットアップ
IronPDF のインストール - .NET PDF ライブラリ
.NET アプリケーションで PDF の操作を開始するには、まず IronPDF ライブラリを統合する必要があります。 IronPDF は、.NET 開発者が PDF ドキュメントを簡単に作成、編集、そして最も重要なことに印刷できるようにする強力で多用途なライブラリです。 インストールのプロセスを見ていきましょう。
.NET Coreプロジェクトの作成: Visual Studioを開き、"新しいプロジェクトを作成"を選択します。プロジェクトテンプレート選択ウィンドウで、"すべてのプラットフォーム"の下で"Web"からフィルタリングし、"ASP.NET Core Web App"を選択します。
! .NET CoreでPDFファイルを印刷する方法: 図1 - ASP.NET Core Webアプリを選択して新しいプロジェクトを作成する
IronPDF のインストール: "NuGet パッケージ マネージャー"に移動し、"IronPDF"を検索してプロジェクトにインストールします。 IronPDF ライブラリが正しくインストールされ、プロジェクト ファイルで参照されていることを確認します。 コードに適切なusingステートメント (例: using IronPdf;を含める必要があります。
! .NET CoreでPDFファイルを印刷する方法: 図2 - NuGetブラウザを使用してIronPDFライブラリを見つける
ASP.NET Core で基本的な PDF ドキュメントを作成する
IronPDFを使用してASP.NET CoreウェブアプリケーションでPDF文書を作成するには、コントローラーの1つにいくつかのコードを追加し始めます。 始めるための簡単な例を次に示します。
新しいコントローラーを設定する
プロジェクトに、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アプリケーションを実行する
アプリケーションを起動し、 PdfControllerのCreatePdfアクションに移動します。 たとえば、アプリケーションがポート5000のlocalhostで実行されている場合は、 http://localhost:<Your-Port> /Pdf/CreatePdf Web ブラウザでhttp://localhost:<Your-Port> /Pdf/CreatePdf 。
PDFをダウンロード
URL にアクセスすると、PDF ドキュメントが生成され、Web ブラウザを通じてダウンロードされます。 生成された 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"プリンタ名"を、ご使用の環境内のプリンタの実際の名前に置き換えることを忘れないでください。 プリンターは、ASP.NET Core アプリケーションが実行されているサーバーからアクセスできる必要があります。 プログラムを実行し、" **https://localhost:<Your-Port> /Pdf/PrintPdf** URL にアクセスすると、 **https://localhost:<Your-Port> /Pdf/PrintPdf**を実行すると、PDF がプリンターに送信されたことを示す出力メッセージが表示されます。
結論
このチュートリアルでは、ASP.NET Core アプリケーションのコンテキスト内での IronPDF の機能と機能について説明しました。 IronPDF を使用したプロジェクトのセットアップ、PDF ドキュメントの作成と操作、そしてこれらのドキュメントの印刷に伴うより複雑なプロセスに至るまで、IronPDF は .NET Core で PDF を処理するための堅牢で多用途なツールであることが証明されています。
IronPDF の利用に興味がある人にとって、ライブラリが無料トライアルを提供しており、コミットする前にその機能を評価できることは注目に値します。 ニーズに合っていると思われる場合、IronPDF ライセンスは$799から始まり、小規模プロジェクトと大規模プロジェクトの両方にスケーラブルなソリューションを提供します。 以下に IronXL ライセンスの価格を示します。詳細については、ここをクリックしてください。
よくある質問
.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が正しくインストールされていることを確認し、印刷前にドキュメントの形式を検証することによってしばしば解決できます。







