Skip to footer content
USING IRONPDF

How to Create a PDF with C# PDFWriter

Creating PDF documents programmatically in C# used to be a headache. Most C# PDFWriter solutions involve complex APIs and a lot of boilerplate code just to produce a simple PDF file. If you’ve tried older open source libraries, you know how frustrating it can be.

IronPDF changes all that. With just a few lines of code, you can create PDF documents, add new pages, paragraphs, images, headers, and page numbers, and save them, without dealing with low-level details.

In this article, we’ll show you how to use IronPDF’s document object, ChromePdfRenderer, and PDF generation methods to make professional PDF documents in .NET Framework or .NET Core, directly from Visual Studio. By the end, you’ll be ready to generate your own PDF files, whether it’s a quick “Hello World” test or a full-fledged invoice.

What is a PDFWriter in C#?

A PDFWriter is a document object that lets developers generate PDF documents, add paragraphs, images, headers, and manipulate pages programmatically. Traditional libraries often require manual positioning, complex calculations, and explicit resource management.

IronPDF simplifies all of this. You can create PDF documents using HTML content, CSS, using simple code from a familar C# environemnt such as using static void Main(string[] args) or working with standard classes like MemoryStream.

Some libraries, like iTextSharp, have a class named PdfWriter, but in C# the term PDFWriter generally refers to any component or library that programmatically generates PDF documents.

Moving from low-level manipulation to high-level generation boosts productivity. With a new PDFDocument instance in Visual Studio or your IDE, you can create PDFs with minimal code.

As shown below, traditional PDFWriter libraries like iTextSharp need a lot of boilerplate, while IronPDF produces the same PDF document in just a few lines—faster, simpler, and less error-prone.

How to Install IronPDF in Your C# Project?

Getting started with IronPDF takes less than a minute. The simplest installation method uses NuGet Package Manager:

Install-Package IronPdf

Alternatively, in Visual Studio:

  1. Right-click your project in Solution Explorer
  2. Select "Manage NuGet Packages"
  3. Search for "IronPDF"
  4. Click Install

For detailed platform-specific installations, check the IronPDF installation guide.

How to Create Your First PDF with IronPDF?

Unlike traditional PDFWriter implementations, In IronPDF, you don’t need a separate PDFWriter writer class variable. The renderer and PdfDocument objects handle all writing tasks internally. Here's a complete working example:

using IronPdf;
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Create PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is my first PDF!</p>");
// Save the PDF
pdf.SaveAs("output.pdf");
using IronPdf;
// Instantiate the PDF renderer
var renderer = new ChromePdfRenderer();
// Create PDF from HTML string
var pdf = renderer.RenderHtmlAsPdf("<h1>Hello World</h1><p>This is my first PDF!</p>");
// Save the PDF
pdf.SaveAs("output.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The ChromePdfRenderer handles all the complexity internally, providing pixel-perfect rendering of your content into a new document.

Note: You can add images, new pages, headers, and new paragraphs easily in just a few lines, leveraging IronPDF’s methods and document object features.

How to Convert HTML to PDF Documents

The real power of IronPDF emerges when generating complex PDF documents. Whether converting HTML to PDF from existing web pages or creating dynamic reports, the HTML to PDF conversion maintains complete fidelity:

// Convert a URL to PDF
var urlPdf = renderer.RenderUrlAsPdf("https://example.com");
urlPdf.SaveAs("website.pdf");
// Convert an HTML file with IronPDF's PDF writer
var filePdf = renderer.RenderHtmlFileAsPdf("example-invoice.html");
filePdf.SaveAs("invoice.pdf");
// Use advanced rendering options for your C# PDF generator
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.EnableJavaScript = true;
// Convert a URL to PDF
var urlPdf = renderer.RenderUrlAsPdf("https://example.com");
urlPdf.SaveAs("website.pdf");
// Convert an HTML file with IronPDF's PDF writer
var filePdf = renderer.RenderHtmlFileAsPdf("example-invoice.html");
filePdf.SaveAs("invoice.pdf");
// Use advanced rendering options for your C# PDF generator
renderer.RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape;
renderer.RenderingOptions.MarginTop = 20;
renderer.RenderingOptions.EnableJavaScript = true;
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

The renderer supports full CSS3, JavaScript execution, and responsive layouts. This ensure your PDFs look exactly as intended. For more details on rendering options, see the IronPDF documentation.

HTML File Output

How to Create a PDF with C# PDFWriter: Figure 5 - HTML file before and after being rendered into a PDF document

Real-World Use Case: Generate PDF Documents with IronPDF

Real-world PDF generation in C# often involves dynamic data. Here's how to create a professional invoice using IronPDF's PDF creation API. The code below is an example template for an invoice.

string invoiceHtml = $@"
    <html>
    <head>
        <style>
            body {{ font-family: Arial; }}
            .header {{ background: #f0f0f0; padding: 20px; }}
            .total {{ font-weight: bold; font-size: 18px; }}
        </style>
    </head>
    <body>
        <div class='header'>
            <h1>Invoice #{invoiceNumber}</h1>
            <p>Date: {DateTime.Now:yyyy-MM-dd}</p>
        </div>
        <table>
            <tr><td>Product</td><td>Quantity</td><td>Price</td></tr>
            {GenerateLineItems()}
        </table>
        <p class='total'>Total: ${totalAmount:F2}</p>
    </body>
    </html>";
// Use IronPDF's C# PDF writer to create the document
var invoicePdf = renderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs($"invoice-{invoiceNumber}.pdf");
string invoiceHtml = $@"
    <html>
    <head>
        <style>
            body {{ font-family: Arial; }}
            .header {{ background: #f0f0f0; padding: 20px; }}
            .total {{ font-weight: bold; font-size: 18px; }}
        </style>
    </head>
    <body>
        <div class='header'>
            <h1>Invoice #{invoiceNumber}</h1>
            <p>Date: {DateTime.Now:yyyy-MM-dd}</p>
        </div>
        <table>
            <tr><td>Product</td><td>Quantity</td><td>Price</td></tr>
            {GenerateLineItems()}
        </table>
        <p class='total'>Total: ${totalAmount:F2}</p>
    </body>
    </html>";
// Use IronPDF's C# PDF writer to create the document
var invoicePdf = renderer.RenderHtmlAsPdf(invoiceHtml);
invoicePdf.SaveAs($"invoice-{invoiceNumber}.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

This approach combines the flexibility of HTML templating with the reliability of PDF output, making it ideal for generating invoices, reports, certificates, and other business documents. Learn more about creating PDF reports in ASP.NET.

Output

How to Create a PDF with C# PDFWriter: Figure 6 - Generated PDF invoice output

What Advanced Features Enhance Your PDFWriter?

IronPDF extends beyond basic PDF creation with enterprise-ready features:

  • Headers & Footers: Add page numbers and branding with HtmlHeaderFooter
  • Digital Signatures: Secure documents with cryptographic signatures
  • Watermarks: Add new images or custom text watermarks using ApplyStamp()
  • Encryption: Protect sensitive content with password security
  • Form Fields: Create fillable PDF forms with interactive elements
  • Page Manipulation: Merge, split, create custom page sizes, and rotate pages effortlessly

Here's a practical example adding headers with page numbers to your C# PDF documents:

// Configure headers for your .NET PDF writer
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = "<div style='text-align:center'>Annual Report 2024</div>",
    MaxHeight = 25
};
// Add page numbers to PDF programmatically
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<div style='text-align:center'>Page {page} of {total-pages}</div>",
    MaxHeight = 20
};
// Configure headers for your .NET PDF writer
renderer.RenderingOptions.HtmlHeader = new HtmlHeaderFooter()
{
    HtmlFragment = "<div style='text-align:center'>Annual Report 2024</div>",
    MaxHeight = 25
};
// Add page numbers to PDF programmatically
renderer.RenderingOptions.HtmlFooter = new HtmlHeaderFooter()
{
    HtmlFragment = "<div style='text-align:center'>Page {page} of {total-pages}</div>",
    MaxHeight = 20
};
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

When we use this, we can generate PDF files with the page number in the footer, and a custom header. To demonstrate, I'll create a simple multi-paged PDF from an HTML string:

// Generate long HTML content to create multiple pages for demonstration
// Multi-page HTML with explicit page breaks
string multiPageHtml = "";
for (int i = 1; i <= 5; i++) // 5 pages
{
    multiPageHtml += $@"
            <div style='page-break-after: always;'>
                <h1>Section {i}</h1>
                <p>This is section {i} of the report. Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                This content will appear on its own page thanks to the CSS page-break.</p>
            </div>";
}
//render HTML string a PDF
var multipagePdf = renderer.RenderHtmlAsPdf(multiPageHtml);
//save PDF
multipagePdf.SaveAs("multiPageReport.pdf");
// Generate long HTML content to create multiple pages for demonstration
// Multi-page HTML with explicit page breaks
string multiPageHtml = "";
for (int i = 1; i <= 5; i++) // 5 pages
{
    multiPageHtml += $@"
            <div style='page-break-after: always;'>
                <h1>Section {i}</h1>
                <p>This is section {i} of the report. Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
                This content will appear on its own page thanks to the CSS page-break.</p>
            </div>";
}
//render HTML string a PDF
var multipagePdf = renderer.RenderHtmlAsPdf(multiPageHtml);
//save PDF
multipagePdf.SaveAs("multiPageReport.pdf");
IRON VB CONVERTER ERROR developers@ironsoftware.com
$vbLabelText   $csharpLabel

Output with Page Numbers

How to Create a PDF with C# PDFWriter: Figure 7 - PDF output from the Code Example Above

Why Choose IronPDF for Your C# PDF Generation Needs?

IronPDF makes PDF generation in C# straightforward and reliable. You don’t need a dedicated PdfWriter class, instead with IronPDF, the renderer and PdfDocument object handle everything from HTML content to page size, headers, and footers. Whether you’re creating invoices, reports, or certificates for Microsoft Office integration, IronPDF helps you get the job done in just a few lines of code.

With good documentation, strong support options, and a free trial version, getting started is simple. You can experiment with new PDF documents, add images, or adjust font size and page layout without headaches. IronPDF turns PDF creation from a technical chore into a smooth, productive workflow.

Get stated with IronPDF now.
green arrow pointer

Ready to modernize your C# PDF writer workflow? Start your free trial and experience how IronPDF simplifies PDF creation in .NET. With comprehensive documentation and responsive support, you'll be generating professional PDFs in minutes, not hours.

Transform your document creation today with IronPDF and join thousands of developers who have already switched to modern PDF generation in C#.

Chipego
Software Engineer
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