Skip to footer content
USING IRONPDF

How to Create PDF Using Aspose C# and IronPDF

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.

How to Create PDF Using Aspose C# and IronPDF: Image 1 - IronPDF

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

How to Create PDF Using Aspose C# and IronPDF: Image 2 - Cross Platform

Installation

Installing Aspose PDF via Package Manager Console

Open the Package Manager Console in Visual Studio and run the following command:

PM> Install-Package Aspose.PDF
PM> Install-Package Aspose.PDF
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

How to Create PDF Using Aspose C# and IronPDF: Image 3 - Aspose.PDF Installation

Installing IronPDF

Use the same method to install the package IronPDF:

PM> Install-Package IronPdf
PM> Install-Package IronPdf
SHELL

How to Create PDF Using Aspose C# and IronPDF: Image 4 - Installation

Note: IronPDF includes an embedded Chrome engine for superior HTML rendering, with support for Windows, Linux, macOS, Docker containers, and cloud platforms.

How to Create PDF Using Aspose C# and IronPDF: Image 5 - Create PDF - IronPDF

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 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 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 Document class provides the foundation, while paragraphs hold your content. For testing purposes, this Hello World example demonstrates the basic process.

Output

How to Create PDF Using Aspose C# and IronPDF: Image 6 - PDF 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

How to Create PDF Using Aspose C# and IronPDF: Image 7 - IronPDF 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 Aspose PDF

using Aspose.Pdf;
using Aspose.Pdf.Text;
// 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");
using Aspose.Pdf;
using Aspose.Pdf.Text;
// 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

using 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");
using 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");
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

How to Create PDF Using Aspose C# and IronPDF: Image 8 - IronPDF Invoice Output

Advanced Features

Converting Web Pages to PDF Files Using IronPDF

using IronPdf;
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://ironpdf.com");
pdf.SaveAs("website.pdf");
using IronPdf;
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

using IronPdf;
var renderer = new ChromePdfRenderer();
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");
using IronPdf;
var renderer = new ChromePdfRenderer();
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

How to Create PDF Using Aspose C# and IronPDF: Image 9 - IronPDF Secure PDF 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

How to Create PDF Using Aspose C# and IronPDF: Image 10 - Features

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.

How to Create PDF Using Aspose C# and IronPDF: Image 11 - Licensing

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.

Frequently Asked Questions

What is the primary purpose of using Aspose C# and IronPDF for PDF creation?

The primary purpose of using Aspose C# and IronPDF is to create PDF files programmatically for modern .NET applications, such as generating invoices, reports, or converting web content to PDF format.

How does IronPDF enhance PDF creation in .NET applications?

IronPDF enhances PDF creation in .NET applications by providing comprehensive tools and code examples that simplify the process, making it easier for developers to generate and manipulate PDFs.

Can I use IronPDF to convert web content into a PDF format?

Yes, IronPDF allows you to convert web content into PDF format seamlessly, which is ideal for creating accurate and professional-looking documents from HTML sources.

What are some typical use cases for generating PDFs using IronPDF?

Typical use cases for generating PDFs using IronPDF include creating invoices, generating reports, and converting web pages or HTML content into PDF documents.

Does IronPDF offer complete code examples for developers?

Yes, IronPDF provides complete code examples to assist developers in efficiently using the library to create and manage PDF files.

Is it possible to integrate IronPDF with other .NET libraries for enhanced functionality?

Yes, IronPDF can be integrated with other .NET libraries, such as Aspose C#, to enhance the functionality and capabilities of PDF creation and manipulation.

What are the advantages of using IronPDF over other PDF libraries?

Advantages of using IronPDF include its ease of use, comprehensive documentation, and the availability of complete code examples, which make it a versatile choice for developers.

Can IronPDF handle complex PDF generation tasks such as adding watermarks or headers?

Yes, IronPDF can handle complex PDF generation tasks, including adding watermarks, headers, and footers to documents, providing flexibility in document customization.

Is IronPDF suitable for beginners in .NET development?

IronPDF is suitable for both beginners and experienced developers, as it offers user-friendly features and detailed documentation to guide users through the PDF creation process.

How does IronPDF ensure the quality of generated PDF documents?

IronPDF ensures high-quality PDF documents by offering robust tools for precise layout control and support for a wide range of PDF features and standards.

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...

Read More