Zum Fußzeileninhalt springen
PRODUKTVERGLEICHE

PDFsharp Seitenzahlen zu PDF hinzufügen VS IronPDF (Beispiel)

The PDF (Portable Document Format) is a universally accepted format for sharing documents across various platforms. Its ability to preserve formatting, fonts, and layout makes it indispensable for business, legal, and educational purposes. As the need for PDF manipulation grows in software development, the .NET ecosystem provides developers with multiple libraries for working with PDFs. Among them, PDFSharp and IronPDF stand out as powerful solutions for PDF creation and manipulation.

This article will show you how to add page numbers to PDF documents using PDFsharp and IronPDF in C#, and compare the features of both libraries to help you decide which one best suits your needs.

What is IronPDF?

PDFsharp Add Page Numbers to PDF VS IronPDF (Example): Figure 1 - image.png

IronPDF is a powerful .NET library designed for seamless PDF creation, manipulation, and rendering. It stands out for its ability to convert HTML, CSS, and JavaScript directly into high-quality PDFs using a Chromium-based engine. This makes IronPDF an ideal solution for developers who need to convert dynamic web pages or complex HTML content into a well-formatted PDF, preserving the layout and style as it appears in the browser.

Key Features of IronPDF:

  • Converts HTML, JavaScript, and CSS to high-quality PDFs.
  • Enables custom headers, footers, watermarks, and other page elements.
  • Supports merging PDF files and splitting them.
  • Works seamlessly with .NET applications, including ASP.NET and MVC frameworks.
  • Provides precise control over PDF rendering, ensuring content is displayed as intended.
  • Offers a straightforward API for easy integration into projects.

To explore more advanced features and examples, refer to the official IronPDF documentation.

Installing IronPDF

To add IronPDF to your project, use the NuGet package manager in Visual Studio. You can install it using the Visual Command-Line interface or search directly in the NuGet Package Manager.

Command-line installation:

Install-Package IronPdf

Alternatively, you can search for "IronPDF" in the NuGet Package Manager and install it from there.

What is PDFSharp?

PDFsharp is a versatile .NET library focused on creating and manipulating PDF documents with high flexibility. Unlike other libraries, PDFsharp allows detailed control over the structure and design of PDFs, making it a great choice for developers who want to create documents from scratch or modify existing PDFs. With its rich API, PDFsharp supports a wide array of document manipulation tasks, including adding text, images, tables, and even page numbers.

Key Features of PDFSharp:

  • Creates, reads, and modifies PDF documents.
  • Customizable page events to add elements such as page numbers or footers.
  • Supports adding images, text, tables, and other content.
  • Offers detailed control over PDF layout and structure.
  • Can modify existing PDFs, including merging or splitting documents.
  • Open-source library with flexibility for developers to customize or extend.

To dive deeper into PDFSharp's functionality and usage, check out the PDFSharp GitHub repository.

Installing PDFsharp

To get started with PDFSharp, install the package via NuGet using the command line:

Command-line installation:

Install-Package PDFSharp

Alternatively, you can search for "PDFSharp" in the NuGet Package Manager and install it from there.

How to Add Page Numbers using IronPDF

IronPDF is a versatile PDF library designed to handle a wide range of PDF operations, including adding page numbers. IronPDF operates internally on a Chromium engine, allowing it to offer precise rendering of HTML content as PDFs. With its simple API, adding page numbers to your PDF is both efficient and straightforward.

The following code is an example of how to add page numbers using IronPDF in C#:

using IronPdf;
class Program
{
    static void Main(string[] args)
    {
        var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";

        // Instantiate ChromePdfRenderer
        ChromePdfRenderer renderer = new ChromePdfRenderer()
        {
            // Set rendering options for page header
            RenderingOptions = {
                HtmlHeader = new HtmlHeaderFooter
                {
                    HtmlFragment = "<center><i>{page} of {total-pages}</i></center>" // Page number format
                },
            }
        };

        // Render given HTML as PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

        // Save the resulting PDF with page numbers
        pdf.SaveAs("pageNumbers.pdf");
    }
}
using IronPdf;
class Program
{
    static void Main(string[] args)
    {
        var html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>";

        // Instantiate ChromePdfRenderer
        ChromePdfRenderer renderer = new ChromePdfRenderer()
        {
            // Set rendering options for page header
            RenderingOptions = {
                HtmlHeader = new HtmlHeaderFooter
                {
                    HtmlFragment = "<center><i>{page} of {total-pages}</i></center>" // Page number format
                },
            }
        };

        // Render given HTML as PDF
        PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

        // Save the resulting PDF with page numbers
        pdf.SaveAs("pageNumbers.pdf");
    }
}
Imports IronPdf
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		Dim html = "<h1>Hello World!</h1><p>This document was generated using IronPDF</p>"

		' Instantiate ChromePdfRenderer
		Dim renderer As New ChromePdfRenderer() With {
			.RenderingOptions = {
				HtmlHeader = New HtmlHeaderFooter With {.HtmlFragment = "<center><i>{page} of {total-pages}</i></center>"}
			}
		}

		' Render given HTML as PDF
		Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

		' Save the resulting PDF with page numbers
		pdf.SaveAs("pageNumbers.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

Output

PDFsharp Add Page Numbers to PDF VS IronPDF (Example): Figure 2

This code demonstrates how to easily insert page numbers in the header of a PDF document. The HtmlHeaderFooter object is used to specify the header content, and placeholders like {page} and {total-pages} automatically populate with the page number and the total page count respectively.

With this approach, IronPDF simplifies the process of rendering HTML as PDFs while managing page number placement effortlessly.

How to Add Page Numbers using PDFsharp

PDFSharp is a comprehensive library for working with PDFs in C#. It provides tools for creating, modifying, and reading PDFs. Though it is not designed specifically for HTML-to-PDF conversion, it offers robust control over PDF documents, including the ability to add page numbers using custom page events. Whether you wish to add page numbers to just the first page, or second page, a range of pages, or just all of them.

Here’s an example of how to add page numbers using PDFSharp:

using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
internal class Program
{
    static void Main(string[] args)
    {
        // Create a new PDF document
        var doc = new PdfDocument();
        doc.Info.Title = "Page Numbers Example";

        // Add a new page to the document
        PdfPage page = doc.AddPage();

        // Create graphics object for drawing
        var gfx = XGraphics.FromPdfPage(page);
        var font = new XFont("Arial", 12);

        // Draw page number in the footer
        gfx.DrawString("Page " + doc.PageCount, font, XBrushes.Black, new XPoint(500, 770));

        // Save the PDF file
        doc.Save("PdfSharpOutput.pdf");
    }
}
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Drawing;
internal class Program
{
    static void Main(string[] args)
    {
        // Create a new PDF document
        var doc = new PdfDocument();
        doc.Info.Title = "Page Numbers Example";

        // Add a new page to the document
        PdfPage page = doc.AddPage();

        // Create graphics object for drawing
        var gfx = XGraphics.FromPdfPage(page);
        var font = new XFont("Arial", 12);

        // Draw page number in the footer
        gfx.DrawString("Page " + doc.PageCount, font, XBrushes.Black, new XPoint(500, 770));

        // Save the PDF file
        doc.Save("PdfSharpOutput.pdf");
    }
}
Imports PdfSharp.Pdf
Imports PdfSharp.Pdf.IO
Imports PdfSharp.Drawing
Friend Class Program
	Shared Sub Main(ByVal args() As String)
		' Create a new PDF document
		Dim doc = New PdfDocument()
		doc.Info.Title = "Page Numbers Example"

		' Add a new page to the document
		Dim page As PdfPage = doc.AddPage()

		' Create graphics object for drawing
		Dim gfx = XGraphics.FromPdfPage(page)
		Dim font = New XFont("Arial", 12)

		' Draw page number in the footer
		gfx.DrawString("Page " & doc.PageCount, font, XBrushes.Black, New XPoint(500, 770))

		' Save the PDF file
		doc.Save("PdfSharpOutput.pdf")
	End Sub
End Class
$vbLabelText   $csharpLabel

Output

PDFsharp Add Page Numbers to PDF VS IronPDF (Example): Figure 3

Conclusion

In summary, IronPDF stands out for its ability to easily add page numbers to existing PDF documents in one concise block of code, giving you full control over how and where your page numbers are displayed. While PDFsharp is a versatile option for creating and manipulating PDFs with fine-grained control, it does lead to a more manual, harder-to-implement approach to adding page numbers to PDF files.

IronPDF's commercial licensing is available, allowing users to evaluate its features before committing to a purchase.

For more information on IronPDF, visit the documentation page, and for PDFSharp, check out the GitHub repository.

Hinweis:PDFsharp is a registered trademark of its respective owner. This site is not affiliated with, endorsed by, or sponsored by PDFsharp. All product names, logos, and brands are property of their respective owners. Comparisons are for informational purposes only and reflect publicly available information at the time of writing.

Häufig gestellte Fragen

Wie kann ich Seitenzahlen zu einem PDF mit C# hinzufügen?

Sie können Seitenzahlen zu einem PDF in C# mit IronPDF hinzufügen, indem Sie benutzerdefinierte Kopf- oder Fußzeilen mit der HtmlHeaderFooter-Klasse einrichten und Platzhalter wie {page} und {total-pages} verwenden, um Seitenzahlen anzuzeigen.

Was sind die Vorteile der Verwendung von IronPDF für die PDF-Manipulation?

IronPDF bietet eine umfassende Reihe von Funktionen wie die Konvertierung von HTML, CSS und JavaScript in PDF, das Hinzufügen von Wasserzeichen und die Integration von benutzerdefinierten Kopf- und Fußzeilen. Es verwendet eine Chromium-basierte Engine, die eine hochwertige PDF-Darstellung gewährleistet und ideal für Webentwickler ist.

Wie unterscheidet sich PDFsharp von IronPDF in Bezug auf die PDF-Erstellung?

PDFsharp bietet einen eher manuellen Ansatz zur PDF-Erstellung, der eine detaillierte Kontrolle über den Dokumentaufbau und das Design ermöglicht. Es konzentriert sich darauf, Elemente wie Text, Bilder und Tabellen direkt hinzuzufügen, während IronPDF den Prozess mit HTML-basierter Darstellung vereinfacht.

Was sind die Installationsschritte für IronPDF in einer .NET-Anwendung?

Um IronPDF in einer .NET-Anwendung zu installieren, verwenden Sie den NuGet-Paketmanager in Visual Studio. Sie können den Befehl Install-Package IronPdf in der Kommandozeile ausführen oder direkt im NuGet-Paketmanager nach 'IronPDF' suchen.

Kann IronPDF zur Konvertierung von Websites in PDF verwendet werden?

Ja, IronPDF kann ganze Websites oder spezifische HTML-Inhalte in PDF-Dokumente umwandeln. Es nutzt eine Chromium-basierte Engine, um die genaue Darstellung von Webseiten einschließlich CSS und JavaScript zu gewährleisten.

Ist IronPDF geeignet, um Wasserzeichen zu PDF hinzuzufügen?

IronPDF ist sehr gut geeignet, um Wasserzeichen zu PDFs hinzuzufügen. Es ermöglicht die nahtlose Integration von text- oder bildbasierten Wasserzeichen in Ihre PDF-Dokumente während des Renderingprozesses.

Was sind die typischen Anwendungsfälle für die Verwendung von PDFsharp?

PDFsharp wird typischerweise für Projekte verwendet, die eine detaillierte Kontrolle über die Erstellung von PDF-Dokumenten erfordern, wie das Hinzufügen von Text, Bildern und Tabellen mit präzisen Layouteinstellungen. Es wird von Entwicklern bevorzugt, die die PDF-Struktur manuell manipulieren müssen.

Wie können Entwickler zwischen IronPDF und PDFsharp wählen?

Entwickler sollten sich für IronPDF entscheiden, wenn sie eine einfache, qualitativ hochwertige Renderinglösung mit weniger manueller Intervention benötigen, insbesondere bei der Verarbeitung von HTML-Inhalten. PDFsharp ist besser für diejenigen geeignet, die umfangreiche Anpassungen und manuelle Kontrolle über PDF-Elemente benötigen.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen