// Install-Package IronPdfvar pdf = new IronPdf.ChromePdfRenderer() .RenderHtmlAsPdf("<h1>Sales Report</h1><table><tr><td>Q1</td><td>$50,000</td></tr></table>") .SaveAs("sales-report.pdf");
// Install-Package IronPdf
var pdf = new IronPdf.ChromePdfRenderer()
.RenderHtmlAsPdf("<h1>Sales Report</h1><table><tr><td>Q1</td><td>$50,000</td></tr></table>")
.SaveAs("sales-report.pdf");
// Invoice data model with customer, company, and line item detailspublic class InvoiceModel{ public stringInvoiceNumber { get; set; } = string.Empty; public DateTimeInvoiceDate { get; set; } public DateTimeDueDate { get; set; } public CompanyInfoCompany { get; set; } = new(); public CustomerInfoCustomer { get; set; } = new(); public List<LineItem> Items { get; set; } = new(); // Computed totals - business logic stays in the model public decimalSubtotal => Items.Sum(x => x.Total); public decimalTaxRate { get; set; } = 0.08m; public decimalTaxAmount => Subtotal * TaxRate; public decimalGrandTotal => Subtotal + TaxAmount;}// Company details for invoice headerpublic class CompanyInfo{ public stringName { get; set; } = string.Empty; public stringAddress { get; set; } = string.Empty; public stringCity { get; set; } = string.Empty; public stringPhone { get; set; } = string.Empty; public stringEmail { get; set; } = string.Empty; public stringLogoPath { get; set; } = string.Empty;}// Customer billing informationpublic class CustomerInfo{ public stringName { get; set; } = string.Empty; public stringAddress { get; set; } = string.Empty; public stringCity { get; set; } = string.Empty; public stringEmail { get; set; } = string.Empty;}// Individual invoice line itempublic class LineItem{ public stringDescription { get; set; } = string.Empty; public intQuantity { get; set; } public decimalUnitPrice { get; set; } public decimalTotal => Quantity * UnitPrice;}
// Invoice data model with customer, company, and line item details
public class InvoiceModel
{
public string InvoiceNumber { get; set; } = string.Empty;
public DateTime InvoiceDate { get; set; }
public DateTime DueDate { get; set; }
public CompanyInfo Company { get; set; } = new();
public CustomerInfo Customer { get; set; } = new();
public List<LineItem> Items { get; set; } = new();
// Computed totals - business logic stays in the model
public decimal Subtotal => Items.Sum(x => x.Total);
public decimal TaxRate { get; set; } = 0.08m;
public decimal TaxAmount => Subtotal * TaxRate;
public decimal GrandTotal => Subtotal + TaxAmount;
}
// Company details for invoice header
public class CompanyInfo
{
public string Name { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string LogoPath { get; set; } = string.Empty;
}
// Customer billing information
public class CustomerInfo
{
public string Name { get; set; } = string.Empty;
public string Address { get; set; } = string.Empty;
public string City { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
// Individual invoice line item
public class LineItem
{
public string Description { get; set; } = string.Empty;
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
public decimal Total => Quantity * UnitPrice;
}
ImportsSystemImportsSystem.Collections.GenericImportsSystem.Linq' Invoice data model with customer, company, and line item detailsPublic Class InvoiceModel Public Property InvoiceNumberAsString = String.Empty Public Property InvoiceDateAsDateTime Public Property DueDateAsDateTime Public Property CompanyAs New CompanyInfo() Public Property CustomerAs New CustomerInfo() Public Property ItemsAs New List(OfLineItem)() ' Computed totals - business logic stays in the model PublicReadOnlyPropertySubtotalAsDecimal Get ReturnItems.Sum(Function(x) x.Total)End Get End Property Public Property TaxRateAsDecimal = 0.08D PublicReadOnlyPropertyTaxAmountAsDecimal Get ReturnSubtotal * TaxRateEnd Get End Property PublicReadOnlyPropertyGrandTotalAsDecimal Get ReturnSubtotal + TaxAmountEnd Get End PropertyEnd Class' Company details for invoice headerPublic Class CompanyInfo Public Property NameAsString = String.Empty Public Property AddressAsString = String.Empty Public Property CityAsString = String.Empty Public Property PhoneAsString = String.Empty Public Property EmailAsString = String.Empty Public Property LogoPathAsString = String.EmptyEnd Class' Customer billing informationPublic Class CustomerInfo Public Property NameAsString = String.Empty Public Property AddressAsString = String.Empty Public Property CityAsString = String.Empty Public Property EmailAsString = String.EmptyEnd Class' Individual invoice line itemPublic Class LineItem Public Property DescriptionAsString = String.Empty Public Property QuantityAsInteger Public Property UnitPriceAsDecimal PublicReadOnlyPropertyTotalAsDecimal Get ReturnQuantity * UnitPriceEnd Get End PropertyEnd Class
Imports System
Imports System.Collections.Generic
Imports System.Linq
' Invoice data model with customer, company, and line item details
Public Class InvoiceModel
Public Property InvoiceNumber As String = String.Empty
Public Property InvoiceDate As DateTime
Public Property DueDate As DateTime
Public Property Company As New CompanyInfo()
Public Property Customer As New CustomerInfo()
Public Property Items As New List(Of LineItem)()
' Computed totals - business logic stays in the model
Public ReadOnly Property Subtotal As Decimal
Get
Return Items.Sum(Function(x) x.Total)
End Get
End Property
Public Property TaxRate As Decimal = 0.08D
Public ReadOnly Property TaxAmount As Decimal
Get
Return Subtotal * TaxRate
End Get
End Property
Public ReadOnly Property GrandTotal As Decimal
Get
Return Subtotal + TaxAmount
End Get
End Property
End Class
' Company details for invoice header
Public Class CompanyInfo
Public Property Name As String = String.Empty
Public Property Address As String = String.Empty
Public Property City As String = String.Empty
Public Property Phone As String = String.Empty
Public Property Email As String = String.Empty
Public Property LogoPath As String = String.Empty
End Class
' Customer billing information
Public Class CustomerInfo
Public Property Name As String = String.Empty
Public Property Address As String = String.Empty
Public Property City As String = String.Empty
Public Property Email As String = String.Empty
End Class
' Individual invoice line item
Public Class LineItem
Public Property Description As String = String.Empty
Public Property Quantity As Integer
Public Property UnitPrice As Decimal
Public ReadOnly Property Total As Decimal
Get
Return Quantity * UnitPrice
End Get
End Property
End Class
using IronPdf;// Service class for generating invoice PDFs from Razor viewspublic class InvoiceReportService{ private readonly IRazorViewEngine _razorViewEngine; private readonly ITempDataProvider _tempDataProvider; private readonly IServiceProvider _serviceProvider; publicInvoiceReportService( IRazorViewEngine razorViewEngine, ITempDataProvider tempDataProvider, IServiceProvider serviceProvider) { _razorViewEngine = razorViewEngine; _tempDataProvider = tempDataProvider; _serviceProvider = serviceProvider; } // Generate PDF from invoice model public async Task<byte[]> GenerateInvoicePdfAsync(InvoiceModel invoice) { // Render Razor view to HTML string string html = awaitRenderViewToStringAsync("InvoiceTemplate", invoice); // Configure PDF renderer with margins and paper size var renderer = new ChromePdfRenderer(); renderer.RenderingOptions.MarginTop = 10; renderer.RenderingOptions.MarginBottom = 10; renderer.RenderingOptions.MarginLeft = 10; renderer.RenderingOptions.MarginRight = 10; renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter; // Convert HTML to PDF and return bytes var pdfDocument = renderer.RenderHtmlAsPdf(html); return pdfDocument.BinaryData; } // Helper method to render a Razor view to string private async Task<string> RenderViewToStringAsync<TModel>(string viewName, TModel model) { var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider }; var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor()); using var stringWriter = new StringWriter(); var viewResult = _razorViewEngine.FindView(actionContext, viewName, false); if (!viewResult.Success) throw new InvalidOperationException($"View '{viewName}' not found."); var viewDictionary = new ViewDataDictionary<TModel>( new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model }; var viewContext = new ViewContext(actionContext, viewResult.View, viewDictionary, new TempDataDictionary(actionContext.HttpContext, _tempDataProvider), stringWriter, new HtmlHelperOptions()); await viewResult.View.RenderAsync(viewContext); return stringWriter.ToString(); }}
using IronPdf;
// Service class for generating invoice PDFs from Razor views
public class InvoiceReportService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IServiceProvider _serviceProvider;
public InvoiceReportService(
IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IServiceProvider serviceProvider)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_serviceProvider = serviceProvider;
}
// Generate PDF from invoice model
public async Task<byte[]> GenerateInvoicePdfAsync(InvoiceModel invoice)
{
// Render Razor view to HTML string
string html = await RenderViewToStringAsync("InvoiceTemplate", invoice);
// Configure PDF renderer with margins and paper size
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.MarginLeft = 10;
renderer.RenderingOptions.MarginRight = 10;
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter;
// Convert HTML to PDF and return bytes
var pdfDocument = renderer.RenderHtmlAsPdf(html);
return pdfDocument.BinaryData;
}
// Helper method to render a Razor view to string
private async Task<string> RenderViewToStringAsync<TModel>(string viewName, TModel model)
{
var httpContext = new DefaultHttpContext { RequestServices = _serviceProvider };
var actionContext = new ActionContext(httpContext, new RouteData(), new ActionDescriptor());
using var stringWriter = new StringWriter();
var viewResult = _razorViewEngine.FindView(actionContext, viewName, false);
if (!viewResult.Success)
throw new InvalidOperationException($"View '{viewName}' not found.");
var viewDictionary = new ViewDataDictionary<TModel>(
new EmptyModelMetadataProvider(), new ModelStateDictionary()) { Model = model };
var viewContext = new ViewContext(actionContext, viewResult.View, viewDictionary,
new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
stringWriter, new HtmlHelperOptions());
await viewResult.View.RenderAsync(viewContext);
return stringWriter.ToString();
}
}
ImportsIronPdfImportsMicrosoft.AspNetCore.Mvc.RazorImportsMicrosoft.AspNetCore.Mvc.ViewFeaturesImportsMicrosoft.Extensions.DependencyInjectionImportsMicrosoft.AspNetCore.HttpImportsMicrosoft.AspNetCore.MvcImportsMicrosoft.AspNetCore.RoutingImportsSystem.IOImportsSystem.Threading.Tasks' Service class for generating invoice PDFs from Razor viewsPublic Class InvoiceReportService PrivateReadOnly _razorViewEngine AsIRazorViewEngine PrivateReadOnly _tempDataProvider AsITempDataProvider PrivateReadOnly _serviceProvider AsIServiceProvider Public Sub New(razorViewEngine AsIRazorViewEngine, tempDataProvider AsITempDataProvider, serviceProvider AsIServiceProvider) _razorViewEngine = razorViewEngine _tempDataProvider = tempDataProvider _serviceProvider = serviceProvider End Sub ' Generate PDF from invoice model PublicAsync Function GenerateInvoicePdfAsync(invoice AsInvoiceModel) AsTask(OfByte()) ' Render Razor view to HTML string Dim html AsString = AwaitRenderViewToStringAsync("InvoiceTemplate", invoice) ' Configure PDF renderer with margins and paper size Dim renderer As New ChromePdfRenderer() renderer.RenderingOptions.MarginTop = 10 renderer.RenderingOptions.MarginBottom = 10 renderer.RenderingOptions.MarginLeft = 10 renderer.RenderingOptions.MarginRight = 10 renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter ' Convert HTML to PDF and return bytes Dim pdfDocument = renderer.RenderHtmlAsPdf(html) Return pdfDocument.BinaryData End Function ' Helper method to render a Razor view to string PrivateAsync Function RenderViewToStringAsync(OfTModel)(viewName AsString, model AsTModel) AsTask(OfString) Dim httpContext As New DefaultHttpContextWith {.RequestServices = _serviceProvider} Dim actionContext As New ActionContext(httpContext, New RouteData(), New ActionDescriptor())Using stringWriter As New StringWriter() Dim viewResult = _razorViewEngine.FindView(actionContext, viewName, False) IfNot viewResult.SuccessThenThrow New InvalidOperationException($"View '{viewName}' not found.") End If Dim viewDictionary As New ViewDataDictionary(OfTModel)( New EmptyModelMetadataProvider(), New ModelStateDictionary()) With {.Model = model} Dim viewContext As New ViewContext(actionContext, viewResult.View, viewDictionary, New TempDataDictionary(actionContext.HttpContext, _tempDataProvider), stringWriter, New HtmlHelperOptions())Await viewResult.View.RenderAsync(viewContext) Return stringWriter.ToString()EndUsing End FunctionEnd Class
Imports IronPdf
Imports Microsoft.AspNetCore.Mvc.Razor
Imports Microsoft.AspNetCore.Mvc.ViewFeatures
Imports Microsoft.Extensions.DependencyInjection
Imports Microsoft.AspNetCore.Http
Imports Microsoft.AspNetCore.Mvc
Imports Microsoft.AspNetCore.Routing
Imports System.IO
Imports System.Threading.Tasks
' Service class for generating invoice PDFs from Razor views
Public Class InvoiceReportService
Private ReadOnly _razorViewEngine As IRazorViewEngine
Private ReadOnly _tempDataProvider As ITempDataProvider
Private ReadOnly _serviceProvider As IServiceProvider
Public Sub New(razorViewEngine As IRazorViewEngine, tempDataProvider As ITempDataProvider, serviceProvider As IServiceProvider)
_razorViewEngine = razorViewEngine
_tempDataProvider = tempDataProvider
_serviceProvider = serviceProvider
End Sub
' Generate PDF from invoice model
Public Async Function GenerateInvoicePdfAsync(invoice As InvoiceModel) As Task(Of Byte())
' Render Razor view to HTML string
Dim html As String = Await RenderViewToStringAsync("InvoiceTemplate", invoice)
' Configure PDF renderer with margins and paper size
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.MarginTop = 10
renderer.RenderingOptions.MarginBottom = 10
renderer.RenderingOptions.MarginLeft = 10
renderer.RenderingOptions.MarginRight = 10
renderer.RenderingOptions.PaperSize = IronPdf.Rendering.PdfPaperSize.Letter
' Convert HTML to PDF and return bytes
Dim pdfDocument = renderer.RenderHtmlAsPdf(html)
Return pdfDocument.BinaryData
End Function
' Helper method to render a Razor view to string
Private Async Function RenderViewToStringAsync(Of TModel)(viewName As String, model As TModel) As Task(Of String)
Dim httpContext As New DefaultHttpContext With {.RequestServices = _serviceProvider}
Dim actionContext As New ActionContext(httpContext, New RouteData(), New ActionDescriptor())
Using stringWriter As New StringWriter()
Dim viewResult = _razorViewEngine.FindView(actionContext, viewName, False)
If Not viewResult.Success Then
Throw New InvalidOperationException($"View '{viewName}' not found.")
End If
Dim viewDictionary As New ViewDataDictionary(Of TModel)(
New EmptyModelMetadataProvider(), New ModelStateDictionary()) With {.Model = model}
Dim viewContext As New ViewContext(actionContext, viewResult.View, viewDictionary,
New TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
stringWriter, New HtmlHelperOptions())
Await viewResult.View.RenderAsync(viewContext)
Return stringWriter.ToString()
End Using
End Function
End Class
using IronPdf;using IronSoftware.Drawing;// Configure text-based headers and footersvar renderer = new ChromePdfRenderer();// Set starting page numberrenderer.RenderingOptions.FirstPageNumber = 1;// Add centered header with divider linerenderer.RenderingOptions.TextHeader = new TextHeaderFooter{CenterText = "CONFIDENTIAL - Internal Use Only",DrawDividerLine = true,Font = FontTypes.Arial,FontSize = 10};// Add footer with date on left, page numbers on rightrenderer.RenderingOptions.TextFooter = new TextHeaderFooter{LeftText = "{date} {time}",RightText = "Page {page} of {total-pages}",DrawDividerLine = true,Font = FontTypes.Arial,FontSize = 9};// Set margins to accommodate header/footerrenderer.RenderingOptions.MarginTop = 25;renderer.RenderingOptions.MarginBottom = 20;
using IronPdf;
using IronSoftware.Drawing;
// Configure text-based headers and footers
var renderer = new ChromePdfRenderer();
// Set starting page number
renderer.RenderingOptions.FirstPageNumber = 1;
// Add centered header with divider line
renderer.RenderingOptions.TextHeader = new TextHeaderFooter
{
CenterText = "CONFIDENTIAL - Internal Use Only",
DrawDividerLine = true,
Font = FontTypes.Arial,
FontSize = 10
};
// Add footer with date on left, page numbers on right
renderer.RenderingOptions.TextFooter = new TextHeaderFooter
{
LeftText = "{date} {time}",
RightText = "Page {page} of {total-pages}",
DrawDividerLine = true,
Font = FontTypes.Arial,
FontSize = 9
};
// Set margins to accommodate header/footer
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 20;
ImportsIronPdfImportsIronSoftware.Drawing' Configure text-based headers and footersDim renderer As New ChromePdfRenderer()' Set starting page numberrenderer.RenderingOptions.FirstPageNumber = 1' Add centered header with divider linerenderer.RenderingOptions.TextHeader = New TextHeaderFooterWith { .CenterText = "CONFIDENTIAL - Internal Use Only", .DrawDividerLine = True, .Font = FontTypes.Arial, .FontSize = 10}' Add footer with date on left, page numbers on rightrenderer.RenderingOptions.TextFooter = New TextHeaderFooterWith { .LeftText = "{date} {time}", .RightText = "Page {page} of {total-pages}", .DrawDividerLine = True, .Font = FontTypes.Arial, .FontSize = 9}' Set margins to accommodate header/footerrenderer.RenderingOptions.MarginTop = 25renderer.RenderingOptions.MarginBottom = 20
Imports IronPdf
Imports IronSoftware.Drawing
' Configure text-based headers and footers
Dim renderer As New ChromePdfRenderer()
' Set starting page number
renderer.RenderingOptions.FirstPageNumber = 1
' Add centered header with divider line
renderer.RenderingOptions.TextHeader = New TextHeaderFooter With {
.CenterText = "CONFIDENTIAL - Internal Use Only",
.DrawDividerLine = True,
.Font = FontTypes.Arial,
.FontSize = 10
}
' Add footer with date on left, page numbers on right
renderer.RenderingOptions.TextFooter = New TextHeaderFooter With {
.LeftText = "{date} {time}",
.RightText = "Page {page} of {total-pages}",
.DrawDividerLine = True,
.Font = FontTypes.Arial,
.FontSize = 9
}
' Set margins to accommodate header/footer
renderer.RenderingOptions.MarginTop = 25
renderer.RenderingOptions.MarginBottom = 20
可用的合併域包括{pdf-title}表示文件標題。 這些佔位符在渲染期間會自動替換。
對於帶有標誌、定制字體或複雜多列格式的頁眉,使用支持完整CSS樣式的HTML頁眉:
using IronPdf;using System;var renderer = new ChromePdfRenderer();// Configure HTML header with logo and custom layoutrenderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter{MaxHeight = 30,HtmlFragment = @"<div style='display: flex; justify-content: space-between; align-items: center; width: 100%; font-family: Arial; font-size: 10px; color: #666;'> <img src='logo.png' style='height: 25px;'> <span>Company Name Inc.</span> <span>Invoice Report</span></div>",BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri};// Configure HTML footer with page info and generation daterenderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter{MaxHeight = 20,HtmlFragment = @"<div style='text-align: center; font-size: 9px; color: #999; border-top: 1px solid #ddd; padding-top: 5px;'> Page {page} of {total-pages} | Generated on {date}</div>",DrawDividerLine = false};
using IronPdf;
using System;
var renderer = new ChromePdfRenderer();
// Configure HTML header with logo and custom layout
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter
{
MaxHeight = 30,
HtmlFragment = @"
<div style='display: flex; justify-content: space-between; align-items: center;
width: 100%; font-family: Arial; font-size: 10px; color: #666;'>
<img src='logo.png' style='height: 25px;'>
<span>Company Name Inc.</span>
<span>Invoice Report</span>
</div>",
BaseUrl = new Uri(@"C:\assets\images\").AbsoluteUri
};
// Configure HTML footer with page info and generation date
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter
{
MaxHeight = 20,
HtmlFragment = @"
<div style='text-align: center; font-size: 9px; color: #999;
border-top: 1px solid #ddd; padding-top: 5px;'>
Page {page} of {total-pages} | Generated on {date}
</div>",
DrawDividerLine = false
};
ImportsIronPdfImportsSystemDim renderer As New ChromePdfRenderer()' Configure HTML header with logo and custom layoutrenderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooterWith { .MaxHeight = 30, .HtmlFragment = "<div style='display: flex; justify-content: space-between; align-items: center; width: 100%; font-family: Arial; font-size: 10px; color: #666;'> <img src='logo.png' style='height: 25px;'> <span>Company Name Inc.</span> <span>Invoice Report</span></div>", .BaseUrl = New Uri("C:\assets\images\").AbsoluteUri}' Configure HTML footer with page info and generation daterenderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooterWith { .MaxHeight = 20, .HtmlFragment = "<div style='text-align: center; font-size: 9px; color: #999; border-top: 1px solid #ddd; padding-top: 5px;'> Page {page} of {total-pages} | Generated on {date}</div>", .DrawDividerLine = False}
Imports IronPdf
Imports System
Dim renderer As New ChromePdfRenderer()
' Configure HTML header with logo and custom layout
renderer.RenderingOptions.HtmlHeader = New HtmlHeaderFooter With {
.MaxHeight = 30,
.HtmlFragment = "
<div style='display: flex; justify-content: space-between; align-items: center;
width: 100%; font-family: Arial; font-size: 10px; color: #666;'>
<img src='logo.png' style='height: 25px;'>
<span>Company Name Inc.</span>
<span>Invoice Report</span>
</div>",
.BaseUrl = New Uri("C:\assets\images\").AbsoluteUri
}
' Configure HTML footer with page info and generation date
renderer.RenderingOptions.HtmlFooter = New HtmlHeaderFooter With {
.MaxHeight = 20,
.HtmlFragment = "
<div style='text-align: center; font-size: 9px; color: #999;
border-top: 1px solid #ddd; padding-top: 5px;'>
Page {page} of {total-pages} | Generated on {date}
</div>",
.DrawDividerLine = False
}
// Employee directory data modelspublic class EmployeeDirectoryModel{ public List<Department> Departments { get; set; } = new(); public DateTimeGeneratedDate { get; set; } = DateTime.Now;}// Department grouping with manager infopublic class Department{ public stringName { get; set; } = string.Empty; public stringManagerName { get; set; } = string.Empty; public List<Employee> Employees { get; set; } = new();}// Individual employee detailspublic class Employee{ public stringName { get; set; } = string.Empty; public stringTitle { get; set; } = string.Empty; public stringEmail { get; set; } = string.Empty; public stringPhone { get; set; } = string.Empty; public stringPhotoUrl { get; set; } = string.Empty; public DateTimeHireDate { get; set; }}
// Employee directory data models
public class EmployeeDirectoryModel
{
public List<Department> Departments { get; set; } = new();
public DateTime GeneratedDate { get; set; } = DateTime.Now;
}
// Department grouping with manager info
public class Department
{
public string Name { get; set; } = string.Empty;
public string ManagerName { get; set; } = string.Empty;
public List<Employee> Employees { get; set; } = new();
}
// Individual employee details
public class Employee
{
public string Name { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Phone { get; set; } = string.Empty;
public string PhotoUrl { get; set; } = string.Empty;
public DateTime HireDate { get; set; }
}
' Employee directory data modelsPublic Class EmployeeDirectoryModel Public Property DepartmentsAsList(OfDepartment) = New List(OfDepartment)() Public Property GeneratedDateAsDateTime = DateTime.NowEnd Class' Department grouping with manager infoPublic Class Department Public Property NameAsString = String.Empty Public Property ManagerNameAsString = String.Empty Public Property EmployeesAsList(OfEmployee) = New List(OfEmployee)()End Class' Individual employee detailsPublic Class Employee Public Property NameAsString = String.Empty Public Property TitleAsString = String.Empty Public Property EmailAsString = String.Empty Public Property PhoneAsString = String.Empty Public Property PhotoUrlAsString = String.Empty Public Property HireDateAsDateTimeEnd Class
' Employee directory data models
Public Class EmployeeDirectoryModel
Public Property Departments As List(Of Department) = New List(Of Department)()
Public Property GeneratedDate As DateTime = DateTime.Now
End Class
' Department grouping with manager info
Public Class Department
Public Property Name As String = String.Empty
Public Property ManagerName As String = String.Empty
Public Property Employees As List(Of Employee) = New List(Of Employee)()
End Class
' Individual employee details
Public Class Employee
Public Property Name As String = String.Empty
Public Property Title As String = String.Empty
Public Property Email As String = String.Empty
Public Property Phone As String = String.Empty
Public Property PhotoUrl As String = String.Empty
Public Property HireDate As DateTime
End Class
@model SalesReportModel<!-- Include Chart.js library from CDN --><script src="https://cdn.jsdelivr.net/npm/chart.js"></script><!-- Canvas element for chart rendering --><canvas id="salesChart"></canvas><script> // Initialize bar chart with data from C# model const ctx = document.getElementById('salesChart').getContext('2d'); new Chart(ctx, { type: 'bar', data: { // Serialize model data to JavaScript arrays labels: @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.MonthLabels)), datasets: [{ label: 'Monthly Sales', data: @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.MonthlySales)), backgroundColor: 'rgba(52, 152, 219, 0.7)' }] } });</script>
@model SalesReportModel
<!-- Include Chart.js library from CDN -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<!-- Canvas element for chart rendering -->
<canvas id="salesChart"></canvas>
<script>
// Initialize bar chart with data from C# model
const ctx = document.getElementById('salesChart').getContext('2d');
new Chart(ctx, {
type: 'bar',
data: {
// Serialize model data to JavaScript arrays
labels: @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.MonthLabels)),
datasets: [{
label: 'Monthly Sales',
data: @Html.Raw(System.Text.Json.JsonSerializer.Serialize(Model.MonthlySales)),
backgroundColor: 'rgba(52, 152, 219, 0.7)'
}]
}
});
</script>
HTML
在渲染包含JavaScript生成內容的頁面時,配置渲染器以等待腳本完成執行再抓取頁面:
using IronPdf;string html = "<h1>Report</h1>";// Configure renderer to wait for JavaScript executionvar renderer = new ChromePdfRenderer();renderer.RenderingOptions.WaitFor.JavaScript(500); // Wait 500ms for JS to completevar pdf = renderer.RenderHtmlAsPdf(html);
using IronPdf;
string html = "<h1>Report</h1>";
// Configure renderer to wait for JavaScript execution
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.WaitFor.JavaScript(500); // Wait 500ms for JS to complete
var pdf = renderer.RenderHtmlAsPdf(html);
ImportsIronPdfDim html AsString = "<h1>Report</h1>"' Configure renderer to wait for JavaScript executionDim renderer As New ChromePdfRenderer()renderer.RenderingOptions.WaitFor.JavaScript(500) ' Wait 500ms for JS to completeDim pdf = renderer.RenderHtmlAsPdf(html)
Imports IronPdf
Dim html As String = "<h1>Report</h1>"
' Configure renderer to wait for JavaScript execution
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.WaitFor.JavaScript(500) ' Wait 500ms for JS to complete
Dim pdf = renderer.RenderHtmlAsPdf(html)
<!-- Iterate through inventory items sorted by quantity -->@foreach (var item in Model.Items.OrderBy(x => x.Quantity)){ // Apply CSS class based on stock level thresholds var rowClass = item.Quantity <= Model.CriticalStockThreshold ? "stock-critical" : item.Quantity <= Model.LowStockThreshold ? "stock-low" : ""; <tr class="@rowClass"> <td>@item.SKU</td> <td>@item.ProductName</td> <td class="text-right"> <!-- Badge styling for critical stock items --> <span class="quantity-badge @(item.Quantity <= 5 ? "badge-critical" : "badge-ok")"> @item.Quantity </span> </td> </tr>}
<!-- Iterate through inventory items sorted by quantity -->
@foreach (var item in Model.Items.OrderBy(x => x.Quantity))
{
// Apply CSS class based on stock level thresholds
var rowClass = item.Quantity <= Model.CriticalStockThreshold ? "stock-critical" :
item.Quantity <= Model.LowStockThreshold ? "stock-low" : "";
<tr class="@rowClass">
<td>@item.SKU</td>
<td>@item.ProductName</td>
<td class="text-right">
<!-- Badge styling for critical stock items -->
<span class="quantity-badge @(item.Quantity <= 5 ? "badge-critical" : "badge-ok")">
@item.Quantity
</span>
</td>
</tr>
}
HTML
範例輸出
建立子報告和分段
要將獨立生成的報告合併為一個文件,使用IronPDF的合併功能:
using IronPdf;// Combine multiple reports into a single PDF documentpublic byte[] GenerateCombinedReport(SalesReportModel sales, InventoryReportModel inventory){ var renderer = new ChromePdfRenderer(); // Render each report section separately var salesPdf = renderer.RenderHtmlAsPdf(RenderSalesReport(sales)); var inventoryPdf = renderer.RenderHtmlAsPdf(RenderInventoryReport(inventory)); // Merge PDFs into one document var combined = PdfDocument.Merge(salesPdf, inventoryPdf); return combined.BinaryData;}
using IronPdf;
// Combine multiple reports into a single PDF document
public byte[] GenerateCombinedReport(SalesReportModel sales, InventoryReportModel inventory)
{
var renderer = new ChromePdfRenderer();
// Render each report section separately
var salesPdf = renderer.RenderHtmlAsPdf(RenderSalesReport(sales));
var inventoryPdf = renderer.RenderHtmlAsPdf(RenderInventoryReport(inventory));
// Merge PDFs into one document
var combined = PdfDocument.Merge(salesPdf, inventoryPdf);
return combined.BinaryData;
}
ImportsIronPdf' Combine multiple reports into a single PDF documentPublic Function GenerateCombinedReport(sales AsSalesReportModel, inventory AsInventoryReportModel) AsByte() Dim renderer As New ChromePdfRenderer() ' Render each report section separately Dim salesPdf = renderer.RenderHtmlAsPdf(RenderSalesReport(sales)) Dim inventoryPdf = renderer.RenderHtmlAsPdf(RenderInventoryReport(inventory)) ' Merge PDFs into one document Dim combined = PdfDocument.Merge(salesPdf, inventoryPdf) Return combined.BinaryDataEnd Function
Imports IronPdf
' Combine multiple reports into a single PDF document
Public Function GenerateCombinedReport(sales As SalesReportModel, inventory As InventoryReportModel) As Byte()
Dim renderer As New ChromePdfRenderer()
' Render each report section separately
Dim salesPdf = renderer.RenderHtmlAsPdf(RenderSalesReport(sales))
Dim inventoryPdf = renderer.RenderHtmlAsPdf(RenderInventoryReport(inventory))
' Merge PDFs into one document
Dim combined = PdfDocument.Merge(salesPdf, inventoryPdf)
Return combined.BinaryData
End Function
範例輸出
生成目錄
IronPDF可以基於HTML中的標題元素自動生成目錄:
using IronPdf;// Generate PDF with automatic table of contentsvar renderer = new ChromePdfRenderer();renderer.RenderingOptions.TableOfContents = TableOfContentsTypes.WithPageNumbers;var pdf = renderer.RenderHtmlFileAsPdf("report.html");
using IronPdf;
// Generate PDF with automatic table of contents
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.TableOfContents = TableOfContentsTypes.WithPageNumbers;
var pdf = renderer.RenderHtmlFileAsPdf("report.html");
ImportsIronPdf' Generate PDF with automatic table of contentsDim renderer As New ChromePdfRenderer()renderer.RenderingOptions.TableOfContents = TableOfContentsTypes.WithPageNumbersDim pdf = renderer.RenderHtmlFileAsPdf("report.html")
Imports IronPdf
' Generate PDF with automatic table of contents
Dim renderer As New ChromePdfRenderer()
renderer.RenderingOptions.TableOfContents = TableOfContentsTypes.WithPageNumbers
Dim pdf = renderer.RenderHtmlFileAsPdf("report.html")
using IronPdf;using System.Collections.Concurrent;// Generate multiple invoices in parallel using async processingpublic async Task<List<ReportResult>> GenerateInvoiceBatchAsync(List<InvoiceModel> invoices){ var results = new ConcurrentBag<ReportResult>(); // Process invoices concurrently with controlled parallelism awaitParallel.ForEachAsync(invoices, new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount }, async (invoice, token) => { // Each thread gets its own renderer instance var renderer = new ChromePdfRenderer(); string html = BuildInvoiceHtml(invoice); var pdf = await renderer.RenderHtmlAsPdfAsync(html); // Save individual invoice PDF string filename = $"Invoice_{invoice.InvoiceNumber}.pdf"; await pdf.SaveAsAsync(filename); results.Add(new ReportResult { InvoiceNumber = invoice.InvoiceNumber, Success = true }); }); return results.ToList();}
using IronPdf;
using System.Collections.Concurrent;
// Generate multiple invoices in parallel using async processing
public async Task<List<ReportResult>> GenerateInvoiceBatchAsync(List<InvoiceModel> invoices)
{
var results = new ConcurrentBag<ReportResult>();
// Process invoices concurrently with controlled parallelism
await Parallel.ForEachAsync(invoices,
new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount },
async (invoice, token) =>
{
// Each thread gets its own renderer instance
var renderer = new ChromePdfRenderer();
string html = BuildInvoiceHtml(invoice);
var pdf = await renderer.RenderHtmlAsPdfAsync(html);
// Save individual invoice PDF
string filename = $"Invoice_{invoice.InvoiceNumber}.pdf";
await pdf.SaveAsAsync(filename);
results.Add(new ReportResult { InvoiceNumber = invoice.InvoiceNumber, Success = true });
});
return results.ToList();
}
ImportsIronPdfImportsSystem.Collections.ConcurrentImportsSystem.Threading.Tasks' Generate multiple invoices in parallel using async processingPublicAsync Function GenerateInvoiceBatchAsync(invoices AsList(OfInvoiceModel)) AsTask(OfList(OfReportResult)) Dim results As New ConcurrentBag(OfReportResult)() ' Process invoices concurrently with controlled parallelismAwaitTask.Run(Async Function()AwaitParallel.ForEachAsync(invoices, New ParallelOptionsWith {.MaxDegreeOfParallelism = Environment.ProcessorCount},Async Function(invoice, token) ' Each thread gets its own renderer instance Dim renderer As New ChromePdfRenderer() Dim html AsString = BuildInvoiceHtml(invoice) Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html) ' Save individual invoice PDF Dim filename AsString = $"Invoice_{invoice.InvoiceNumber}.pdf"Await pdf.SaveAsAsync(filename) results.Add(New ReportResultWith {.InvoiceNumber = invoice.InvoiceNumber, .Success = True}) End Function) End Function) Return results.ToList()End Function
Imports IronPdf
Imports System.Collections.Concurrent
Imports System.Threading.Tasks
' Generate multiple invoices in parallel using async processing
Public Async Function GenerateInvoiceBatchAsync(invoices As List(Of InvoiceModel)) As Task(Of List(Of ReportResult))
Dim results As New ConcurrentBag(Of ReportResult)()
' Process invoices concurrently with controlled parallelism
Await Task.Run(Async Function()
Await Parallel.ForEachAsync(invoices,
New ParallelOptions With {.MaxDegreeOfParallelism = Environment.ProcessorCount},
Async Function(invoice, token)
' Each thread gets its own renderer instance
Dim renderer As New ChromePdfRenderer()
Dim html As String = BuildInvoiceHtml(invoice)
Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html)
' Save individual invoice PDF
Dim filename As String = $"Invoice_{invoice.InvoiceNumber}.pdf"
Await pdf.SaveAsAsync(filename)
results.Add(New ReportResult With {.InvoiceNumber = invoice.InvoiceNumber, .Success = True})
End Function)
End Function)
Return results.ToList()
End Function
範例輸出
批次處理範例並行生成多個發票。 這是生成的批次發票之一:
將報告生成與ASP.NET Core背景服務整合
排程報告生成自然適合於ASP.NET Core's 的託管服務架構:
// Background service for scheduled report generationpublic class DailyReportService : BackgroundService{ private readonly IServiceProvider _serviceProvider; protected override async TaskExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { // Calculate next run time (6 AM daily) var nextRun = DateTime.Now.Date.AddDays(1).AddHours(6); awaitTask.Delay(nextRun - DateTime.Now, stoppingToken); // Create scoped service for report generation using var scope = _serviceProvider.CreateScope(); var reportService = scope.ServiceProvider.GetRequiredService<IReportGenerationService>(); // Generate and distribute daily report var salesReport = await reportService.GenerateDailySalesSummaryAsync(); // Email or save reports as needed } }}
// Background service for scheduled report generation
public class DailyReportService : BackgroundService
{
private readonly IServiceProvider _serviceProvider;
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
// Calculate next run time (6 AM daily)
var nextRun = DateTime.Now.Date.AddDays(1).AddHours(6);
await Task.Delay(nextRun - DateTime.Now, stoppingToken);
// Create scoped service for report generation
using var scope = _serviceProvider.CreateScope();
var reportService = scope.ServiceProvider.GetRequiredService<IReportGenerationService>();
// Generate and distribute daily report
var salesReport = await reportService.GenerateDailySalesSummaryAsync();
// Email or save reports as needed
}
}
}
ImportsSystemImportsSystem.ThreadingImportsSystem.Threading.TasksImportsMicrosoft.Extensions.DependencyInjection' Background service for scheduled report generationPublic Class DailyReportServiceInheritsBackgroundService PrivateReadOnly _serviceProvider AsIServiceProviderProtectedOverridesAsync Function ExecuteAsync(stoppingToken AsCancellationToken) AsTask WhileNot stoppingToken.IsCancellationRequested ' Calculate next run time (6 AM daily) Dim nextRun = DateTime.Now.Date.AddDays(1).AddHours(6)AwaitTask.Delay(nextRun - DateTime.Now, stoppingToken) ' Create scoped service for report generationUsing scope = _serviceProvider.CreateScope() Dim reportService = scope.ServiceProvider.GetRequiredService(OfIReportGenerationService)() ' Generate and distribute daily report Dim salesReport = Await reportService.GenerateDailySalesSummaryAsync() ' Email or save reports as neededEndUsingEndWhile End FunctionEnd Class
Imports System
Imports System.Threading
Imports System.Threading.Tasks
Imports Microsoft.Extensions.DependencyInjection
' Background service for scheduled report generation
Public Class DailyReportService
Inherits BackgroundService
Private ReadOnly _serviceProvider As IServiceProvider
Protected Overrides Async Function ExecuteAsync(stoppingToken As CancellationToken) As Task
While Not stoppingToken.IsCancellationRequested
' Calculate next run time (6 AM daily)
Dim nextRun = DateTime.Now.Date.AddDays(1).AddHours(6)
Await Task.Delay(nextRun - DateTime.Now, stoppingToken)
' Create scoped service for report generation
Using scope = _serviceProvider.CreateScope()
Dim reportService = scope.ServiceProvider.GetRequiredService(Of IReportGenerationService)()
' Generate and distribute daily report
Dim salesReport = Await reportService.GenerateDailySalesSummaryAsync()
' Email or save reports as needed
End Using
End While
End Function
End Class
Is it possible to automate report generation with IronPDF?
IronPDF supports automation of report generation through batch processing and can be integrated with ASP.NET Core Background Services for scheduled report generation tasks.
What is the best approach to migrate from Crystal Reports to IronPDF?
The best approach involves mapping Crystal Reports concepts to their IronPDF equivalents, prioritizing high-use reports, referencing existing outputs as specifications, and validating the new implementations against production data.