ASP.NET Web アプリケーション (.NET Framework) MVC は、Microsoft が提供する Web アプリケーションフレームワークです。 これは、モデル-ビュー-コントローラー (MVC) として知られる構造化されたアーキテクチャ パターンに従い、ウェブアプリケーションの開発を整理して効率化します。
IronPdf.Extensions.Mvc.FrameworkパッケージはメインのIronPDFパッケージの一部です。 ASP.NET MVC でビューを PDF ドキュメントにレンダリングするには、IronPdf.Extensions.Mvc.Framework と IronPdf パッケージの両方が必要です。 この分離により、コアのPDFレンダリング機能を維持しながら、MVCフレームワーク特有の機能を最適化することができます。
Views を PDF ファイルに変換するには、ASP.NET Web アプリケーション (.NET Framework) MVC プロジェクトが必要です。 IronPDFは様々なMVCバージョンをサポートし、要件に応じてPDF出力をカスタマイズするための広範なレンダリングオプションを提供します。
モデルクラスを追加するにはどうすればよいですか?
どこでモデルを作成すればよいですか?
"Models"フォルダーに移動します
C# クラスファイルを新たに作成し、名前を Person にします。 このクラスは個々のデータを表すモデルとして機能します。 以下のコードを使用してください:
namespace ViewToPdfMVCSample.Models{ public class Person { public intId { get; set; } public stringName { get; set; } public stringTitle { get; set; } public stringDescription { get; set; } }}
namespace ViewToPdfMVCSample.Models
{
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
}
NamespaceViewToPdfMVCSample.Models Public Class Person Public Property Id() AsInteger Public Property Name() AsString Public Property Title() AsString Public Property Description() AsString End ClassEndNamespace
Namespace ViewToPdfMVCSample.Models
Public Class Person
Public Property Id() As Integer
Public Property Name() As String
Public Property Title() As String
Public Property Description() As String
End Class
End Namespace
PDF ドキュメントは次のコードを使用してマシンにダウンロードできます:File(pdf.BinaryData, "application/pdf", "viewToPdfMVC.pdf")。
using IronPdf;using System.Collections.Generic;using System.Web.Mvc;using ViewToPdfMVCSample.Models;namespace ViewToPdfMVCSample.Controllers{ public class HomeController : Controller { public ActionResultIndex() { returnView(); } // GET: Person public ActionResultPersons() { // Create a list of Person objects var persons = new List<Person> { new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" }, new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" }, new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" } }; if (HttpContext.Request.HttpMethod == "POST") { // Define the path to the View file var viewPath = "~/Views/Home/Persons.cshtml"; // Instantiate the ChromePdfRenderer ChromePdfRenderer renderer = new ChromePdfRenderer(); // Render the view to a PDF document PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Set headers to view the PDF in-browserResponse.Headers.Add("Content-Disposition", "inline"); // Return the generated PDF file returnFile(pdf.BinaryData, "application/pdf"); } returnView(persons); } public ActionResultAbout() {ViewBag.Message = "Your application description page."; returnView(); } public ActionResultContact() {ViewBag.Message = "Your contact page."; returnView(); } }}
using IronPdf;
using System.Collections.Generic;
using System.Web.Mvc;
using ViewToPdfMVCSample.Models;
namespace ViewToPdfMVCSample.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
// GET: Person
public ActionResult Persons()
{
// Create a list of Person objects
var persons = new List<Person>
{
new Person { Name = "Alice", Title = "Mrs.", Description = "Software Engineer" },
new Person { Name = "Bob", Title = "Mr.", Description = "Software Engineer" },
new Person { Name = "Charlie", Title = "Mr.", Description = "Software Engineer" }
};
if (HttpContext.Request.HttpMethod == "POST")
{
// Define the path to the View file
var viewPath = "~/Views/Home/Persons.cshtml";
// Instantiate the ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Render the view to a PDF document
PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
// Set headers to view the PDF in-browser
Response.Headers.Add("Content-Disposition", "inline");
// Return the generated PDF file
return File(pdf.BinaryData, "application/pdf");
}
return View(persons);
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
ImportsIronPdfImportsSystem.Collections.GenericImportsSystem.Web.MvcImportsViewToPdfMVCSample.ModelsNamespaceViewToPdfMVCSample.Controllers Public Class HomeControllerInheritsController Public Function Index() AsActionResult ReturnView() End Function ' GET: Person Public Function Persons() AsActionResult ' Create a list of Person objects'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property: Dim persons_Conflict = New List(OfPerson) From { New PersonWith { .Name = "Alice", .Title = "Mrs.", .Description = "Software Engineer" }, New PersonWith { .Name = "Bob", .Title = "Mr.", .Description = "Software Engineer" }, New PersonWith { .Name = "Charlie", .Title = "Mr.", .Description = "Software Engineer" } } IfHttpContext.Request.HttpMethod = "POST" Then ' Define the path to the View file Dim viewPath = "~/Views/Home/Persons.cshtml" ' Instantiate the ChromePdfRenderer Dim renderer As New ChromePdfRenderer() ' Render the view to a PDF document Dim pdf AsPdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict) ' Set headers to view the PDF in-browser Response.Headers.Add("Content-Disposition", "inline") ' Return the generated PDF file ReturnFile(pdf.BinaryData, "application/pdf") End If ReturnView(persons_Conflict) End Function Public Function About() AsActionResultViewBag.Message = "Your application description page." ReturnView() End Function Public Function Contact() AsActionResultViewBag.Message = "Your contact page." ReturnView() End Function End ClassEndNamespace
Imports IronPdf
Imports System.Collections.Generic
Imports System.Web.Mvc
Imports ViewToPdfMVCSample.Models
Namespace ViewToPdfMVCSample.Controllers
Public Class HomeController
Inherits Controller
Public Function Index() As ActionResult
Return View()
End Function
' GET: Person
Public Function Persons() As ActionResult
' Create a list of Person objects
'INSTANT VB NOTE: The local variable persons was renamed since Visual Basic will not allow local variables with the same name as their enclosing function or property:
Dim persons_Conflict = New List(Of Person) From {
New Person With {
.Name = "Alice",
.Title = "Mrs.",
.Description = "Software Engineer"
},
New Person With {
.Name = "Bob",
.Title = "Mr.",
.Description = "Software Engineer"
},
New Person With {
.Name = "Charlie",
.Title = "Mr.",
.Description = "Software Engineer"
}
}
If HttpContext.Request.HttpMethod = "POST" Then
' Define the path to the View file
Dim viewPath = "~/Views/Home/Persons.cshtml"
' Instantiate the ChromePdfRenderer
Dim renderer As New ChromePdfRenderer()
' Render the view to a PDF document
Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons_Conflict)
' Set headers to view the PDF in-browser
Response.Headers.Add("Content-Disposition", "inline")
' Return the generated PDF file
Return File(pdf.BinaryData, "application/pdf")
End If
Return View(persons_Conflict)
End Function
Public Function About() As ActionResult
ViewBag.Message = "Your application description page."
Return View()
End Function
Public Function Contact() As ActionResult
ViewBag.Message = "Your contact page."
Return View()
End Function
End Class
End Namespace
// Advanced rendering with custom optionspublic ActionResultPersonsAdvanced(){ var persons = GetPersonsList(); if (HttpContext.Request.HttpMethod == "POST") { var viewPath = "~/Views/Home/Persons.cshtml"; // Configure the renderer with custom options ChromePdfRenderer renderer = new ChromePdfRenderer(); // Set custom rendering options renderer.RenderingOptions.MarginTop = 40; renderer.RenderingOptions.MarginBottom = 40; renderer.RenderingOptions.MarginLeft = 20; renderer.RenderingOptions.MarginRight = 20; // Set custom paper size renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4; renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait; // Add header and footer renderer.RenderingOptions.TextHeader.DrawDividerLine = true; renderer.RenderingOptions.TextHeader.CenterText = "{pdf-title}"; renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica; renderer.RenderingOptions.TextHeader.FontSize = 12; renderer.RenderingOptions.TextFooter.DrawDividerLine = true; renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial; renderer.RenderingOptions.TextFooter.FontSize = 10; renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"; // Enable JavaScript execution if needed renderer.RenderingOptions.EnableJavaScript = true; renderer.RenderingOptions.RenderDelay = 500; // Wait for JS to execute // Render the view to PDF PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons); // Optional: Apply compression to reduce file size pdf.CompressImages(60);Response.Headers.Add("Content-Disposition", "inline"); returnFile(pdf.BinaryData, "application/pdf"); } returnView("Persons", persons);}
// Advanced rendering with custom options
public ActionResult PersonsAdvanced()
{
var persons = GetPersonsList();
if (HttpContext.Request.HttpMethod == "POST")
{
var viewPath = "~/Views/Home/Persons.cshtml";
// Configure the renderer with custom options
ChromePdfRenderer renderer = new ChromePdfRenderer();
// Set custom rendering options
renderer.RenderingOptions.MarginTop = 40;
renderer.RenderingOptions.MarginBottom = 40;
renderer.RenderingOptions.MarginLeft = 20;
renderer.RenderingOptions.MarginRight = 20;
// Set custom paper size
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4;
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait;
// Add header and footer
renderer.RenderingOptions.TextHeader.DrawDividerLine = true;
renderer.RenderingOptions.TextHeader.CenterText = "{pdf-title}";
renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica;
renderer.RenderingOptions.TextHeader.FontSize = 12;
renderer.RenderingOptions.TextFooter.DrawDividerLine = true;
renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial;
renderer.RenderingOptions.TextFooter.FontSize = 10;
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}";
// Enable JavaScript execution if needed
renderer.RenderingOptions.EnableJavaScript = true;
renderer.RenderingOptions.RenderDelay = 500; // Wait for JS to execute
// Render the view to PDF
PdfDocument pdf = renderer.RenderView(this.HttpContext, viewPath, persons);
// Optional: Apply compression to reduce file size
pdf.CompressImages(60);
Response.Headers.Add("Content-Disposition", "inline");
return File(pdf.BinaryData, "application/pdf");
}
return View("Persons", persons);
}
' Advanced rendering with custom optionsPublic Function PersonsAdvanced() AsActionResult Dim persons = GetPersonsList() IfHttpContext.Request.HttpMethod = "POST" Then Dim viewPath = "~/Views/Home/Persons.cshtml" ' Configure the renderer with custom options Dim renderer As New ChromePdfRenderer() ' Set custom rendering options renderer.RenderingOptions.MarginTop = 40 renderer.RenderingOptions.MarginBottom = 40 renderer.RenderingOptions.MarginLeft = 20 renderer.RenderingOptions.MarginRight = 20 ' Set custom paper size renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4 renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait ' Add header and footer renderer.RenderingOptions.TextHeader.DrawDividerLine = True renderer.RenderingOptions.TextHeader.CenterText = "{pdf-title}" renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica renderer.RenderingOptions.TextHeader.FontSize = 12 renderer.RenderingOptions.TextFooter.DrawDividerLine = True renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial renderer.RenderingOptions.TextFooter.FontSize = 10 renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}" ' Enable JavaScript execution if needed renderer.RenderingOptions.EnableJavaScript = True renderer.RenderingOptions.RenderDelay = 500 ' Wait for JS to execute ' Render the view to PDF Dim pdf AsPdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons) ' Optional: Apply compression to reduce file size pdf.CompressImages(60) Response.Headers.Add("Content-Disposition", "inline") ReturnFile(pdf.BinaryData, "application/pdf") End If ReturnView("Persons", persons)End Function
' Advanced rendering with custom options
Public Function PersonsAdvanced() As ActionResult
Dim persons = GetPersonsList()
If HttpContext.Request.HttpMethod = "POST" Then
Dim viewPath = "~/Views/Home/Persons.cshtml"
' Configure the renderer with custom options
Dim renderer As New ChromePdfRenderer()
' Set custom rendering options
renderer.RenderingOptions.MarginTop = 40
renderer.RenderingOptions.MarginBottom = 40
renderer.RenderingOptions.MarginLeft = 20
renderer.RenderingOptions.MarginRight = 20
' Set custom paper size
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.A4
renderer.RenderingOptions.PaperOrientation = IronPdf.Rendering.PdfPaperOrientation.Portrait
' Add header and footer
renderer.RenderingOptions.TextHeader.DrawDividerLine = True
renderer.RenderingOptions.TextHeader.CenterText = "{pdf-title}"
renderer.RenderingOptions.TextHeader.Font = IronPdf.Font.FontTypes.Helvetica
renderer.RenderingOptions.TextHeader.FontSize = 12
renderer.RenderingOptions.TextFooter.DrawDividerLine = True
renderer.RenderingOptions.TextFooter.Font = IronPdf.Font.FontTypes.Arial
renderer.RenderingOptions.TextFooter.FontSize = 10
renderer.RenderingOptions.TextFooter.RightText = "{page} of {total-pages}"
' Enable JavaScript execution if needed
renderer.RenderingOptions.EnableJavaScript = True
renderer.RenderingOptions.RenderDelay = 500 ' Wait for JS to execute
' Render the view to PDF
Dim pdf As PdfDocument = renderer.RenderView(Me.HttpContext, viewPath, persons)
' Optional: Apply compression to reduce file size
pdf.CompressImages(60)
Response.Headers.Add("Content-Disposition", "inline")
Return File(pdf.BinaryData, "application/pdf")
End If
Return View("Persons", persons)
End Function
ビューをPDFとしてレンダリングするための最小限のコードは次の通りです: var pdf = new IronPdf.ChromePdfRenderer.RenderRazorToPdf(this.ControllerContext); IronPDFを使ったこの一行のコードで、現在のビューがPDFドキュメントに変換されます。
ASP.NET MVC PDF生成にはどの拡張パッケージが必要ですか。
ASP.NET MVCアプリケーションには、IronPdf.Extensions.Mvc.Frameworkパッケージが必要です。このエクステンションはASP.NET MVCプロジェクトとのシームレスな統合を提供し、IronPDFのメインパッケージと連携してビューからPDFへの変換機能を実現します。
What additional features can IronPDF offer for PDF enhancements?
IronPDF offers features like converting PDFs to PDFA or PDFUA, merging or splitting PDF documents, rotating pages, adding annotations or bookmarks, and applying watermarks.
How can I optimize PDF sizes using IronPDF?
You can optimize PDF sizes using IronPDF's compression techniques, which allow for reduced file sizes while maintaining quality.
Where can I find a sample project for PDF conversion in ASP.NET MVC?
You can download a complete sample project from the IronPDF website, which includes all necessary configurations to help you quickly start with PDF generation in ASP.NET MVC applications.