using IronPdf;
// Disable local disk access or cross-origin requests
Installation.EnableWebSecurity = true;
// Instantiate Renderer
var renderer = new ChromePdfRenderer();
// Create a PDF from a HTML string using C#
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1>");
// Export to a file or Stream
pdf.SaveAs("output.pdf");
// Advanced Example with HTML Assets
// Load external html assets: Images, CSS and JavaScript.
// An optional BasePath 'C:\site\assets\' is set as the file location to load assets from
var myAdvancedPdf = renderer.RenderHtmlAsPdf("<img src='icons/iron.png'>", @"C:\site\assets\");
myAdvancedPdf.SaveAs("html-with-assets.pdf");
Creating PDF files programmatically is essential for modern .NET applications, whether generating invoices, reports, or converting web content to PDF format. This tutorial explores how to create PDF using Aspose C# and IronPDF, providing complete code examples to help developers choose the right library for their needs.
Prerequisites
Before starting to create a PDF file, ensure you have:
.NET Framework 4.6.2+ or .NET Core 3.1+
Visual Studio 2019 or compatible IDE
Basic knowledge of using C# programming
Installation
Installing Aspose PDF via Package Manager Console
Open the Package Manager Console in Visual Studio and run the following command:
Install-Package Aspose.PDF
Installing IronPDF
Use the same method to install the package IronPDF:
Install-Package IronPdf
Note: IronPDF includes an embedded Chrome engine for superior HTML rendering, with support for Windows, Linux, macOS, Docker containers, and cloud platforms.
Creating Your First PDF Document
Let's create PDF files with both libraries to understand their fundamental approaches. These code snippet examples demonstrate the basic logic for PDF generation.
Create a PDF Using Aspose PDF for .NET API
using Aspose.Pdf;
using Aspose.Pdf.Text;
// Create new instance of Document class
var document = new Aspose.Pdf.Document();
// Add pages to the document object
var page = document.Pages.Add();
// Create new TextFragment with Hello World text
var textFragment = new TextFragment("Hello World!");
textFragment.TextState.FontSize = 24;
// Add text to paragraphs collection
page.Paragraphs.Add(textFragment);
// Save the generated PDF document
document.Save("output.pdf");
using Aspose.Pdf;
using Aspose.Pdf.Text;
// Create new instance of Document class
var document = new Aspose.Pdf.Document();
// Add pages to the document object
var page = document.Pages.Add();
// Create new TextFragment with Hello World text
var textFragment = new TextFragment("Hello World!");
textFragment.TextState.FontSize = 24;
// Add text to paragraphs collection
page.Paragraphs.Add(textFragment);
// Save the generated PDF document
document.Save("output.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
This source code creates PDF documents by building a document object model. You create a new document, add pages to the collection, and then add content to those pages. The Aspose.Pdf.Document class provides the foundation, while paragraphs hold your content. For testing purposes, this Hello World example demonstrates the basic process.
Output
Creating PDFs with IronPDF
using IronPdf;
// Create new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF file
var html = "<h1>Hello World!</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Save PDF files using SaveAs method
pdf.SaveAs("output.pdf");
using IronPdf;
// Create new instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Convert HTML string to PDF file
var html = "<h1>Hello World!</h1>";
var pdf = renderer.RenderHtmlAsPdf(html);
// Save PDF files using SaveAs method
pdf.SaveAs("output.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
IronPDF takes a different approach to create a PDF - it renders HTML directly to PDF format using Chrome. This API allows you to convert web content and implement complex layouts easily. The library handles all rendering complexities, making it simpler to accomplish professional results.
Output
Real-World Example: Creating an Invoice
Here's complete code to create PDF files using both libraries for a practical invoice solution.
Invoice with New Aspose PDF
// Create new Document instance
var document = new Document();
var page = document.Pages.Add();
// Add title text
var title = new TextFragment("INVOICE");
title.TextState.FontSize = 28;
title.HorizontalAlignment = HorizontalAlignment.Center;
// Add to paragraphs
page.Paragraphs.Add(title);
// Create table object for invoice items
var table = new Table();
table.ColumnWidths = "200 100 100";
// Add header row to table
var headerRow = table.Rows.Add();
headerRow.Cells.Add("Description");
headerRow.Cells.Add("Quantity");
headerRow.Cells.Add("Price");
// Add data rows
var dataRow = table.Rows.Add();
dataRow.Cells.Add("Product A");
dataRow.Cells.Add("2");
dataRow.Cells.Add("$50.00");
// Add table to page paragraphs
page.Paragraphs.Add(table);
// Save the PDF document
document.Save("invoice.pdf");
// Create new Document instance
var document = new Document();
var page = document.Pages.Add();
// Add title text
var title = new TextFragment("INVOICE");
title.TextState.FontSize = 28;
title.HorizontalAlignment = HorizontalAlignment.Center;
// Add to paragraphs
page.Paragraphs.Add(title);
// Create table object for invoice items
var table = new Table();
table.ColumnWidths = "200 100 100";
// Add header row to table
var headerRow = table.Rows.Add();
headerRow.Cells.Add("Description");
headerRow.Cells.Add("Quantity");
headerRow.Cells.Add("Price");
// Add data rows
var dataRow = table.Rows.Add();
dataRow.Cells.Add("Product A");
dataRow.Cells.Add("2");
dataRow.Cells.Add("$50.00");
// Add table to page paragraphs
page.Paragraphs.Add(table);
// Save the PDF document
document.Save("invoice.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
The Aspose PDF NET API requires building each element programmatically. You create the document object, add pages, and then add content to the paragraphs collection. This gives you precise control but requires more code to accomplish complex layouts.
Invoice with IronPDF
var renderer = new ChromePdfRenderer();
// Configure PDF output settings
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
// HTML string with invoice content
var html = @"
<html>
<head>
<style>
table { width: 100%; }
th, td { padding: 8px; }
</style>
</head>
<body>
<h1>INVOICE</h1>
<table>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td>Product A</td>
<td>2</td>
<td>$50.00</td>
</tr>
</table>
</body>
</html>";
// Generate PDF from HTML
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
var renderer = new ChromePdfRenderer();
// Configure PDF output settings
renderer.RenderingOptions.MarginTop = 25;
renderer.RenderingOptions.MarginBottom = 25;
// HTML string with invoice content
var html = @"
<html>
<head>
<style>
table { width: 100%; }
th, td { padding: 8px; }
</style>
</head>
<body>
<h1>INVOICE</h1>
<table>
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr>
<td>Product A</td>
<td>2</td>
<td>$50.00</td>
</tr>
</table>
</body>
</html>";
// Generate PDF from HTML
var pdf = renderer.RenderHtmlAsPdf(html);
pdf.SaveAs("invoice.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
With IronPDF, you write standard HTML and the system handles rendering. This method makes it easier to create PDF files with professional layouts. You can reference external stylesheets, add images, and include links.
Output
Advanced Features
Converting Web Pages to PDF Files Using IronPDF
// Load and convert URL to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.SaveAs("website.pdf");
// Load and convert URL to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.SaveAs("website.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
Adding Security to Generated PDF
// Create PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
// Implement security settings
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// Save secured file
pdf.SaveAs("secured.pdf");
// Create PDF document
var pdf = renderer.RenderHtmlAsPdf(html);
// Implement security settings
pdf.SecuritySettings.UserPassword = "user123";
pdf.SecuritySettings.AllowUserPrinting = IronPdf.Security.PdfPrintSecurity.FullPrintRights;
// Save secured file
pdf.SaveAs("secured.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText $csharpLabel
IronPDF provides intuitive methods to add security, headers, footers, and other features to your PDF documents. You can also delete pages, merge files, or extract content.
Output
Choosing the Right Library
When to Use IronPDF
IronPDF excels when you:
Need to convert HTML to PDF format
Want simpler code and faster development
Require JavaScript execution
Need cross-platform support
Deploy to Docker or cloud environment
When to Use Aspose PDF
Aspose PDF for .NET applications works well when:
Creating PDFs from scratch programmatically
Need granular control over document object model
Working with complex PDF manipulations
Importing existing PDF files for processing
License Comparison
IronPDF offers straightforward licensing starting at $749, including support and updates. The free trial includes all features without watermarks.
Aspose PDF starts at $1,199 with additional costs for support. Both libraries offer evaluation versions for testing purposes.
Conclusion
Both libraries effectively create PDF documents in C#. Aspose PDF provides granular control through its document object model and TextFragment class, while IronPDF shines with HTML-to-PDF conversion using familiar web technologies.
For most modern .NET applications, IronPDF offers better value with its intuitive API, superior HTML rendering, included support, and the ability to create PDF files using simple code. Whether you need to load web pages, add images like BMP, or implement complex layouts, IronPDF's Chrome-based function streamlines the process.
Note for developers: IronPDF also includes Drawing capabilities and can reproduce complex designs. For questions, contact their engineering team or search their documentation to resolve any issues.
Start with IronPDF's free trial to evaluate how it can accomplish your PDF generation needs. Access all features to create, convert, and manipulate PDF files in your .NET solution.
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of ...Read More