csharp-pdfsharpcore

PDF Manipulation in C# with PdfSharpCore

  • 3 min

PdfSharpCore is a popular and robust library for manipulating PDF files in .NET applications.

Derived from the famous PdfSharp library, this version offers cross-platform support through .NET Core.

With PdfSharpCore, we can create, modify, and process PDF documents efficiently, providing an essential tool for applications that need to handle PDF documents programmatically.

Features of PdfSharpCore,

  • PDF Creation: Generate PDF documents from scratch with text, images, graphics, and more.
  • PDF Modification: Allows modifying existing PDF documents, adding new content, changing existing content, etc.
  • Cross-Platform Compatibility: Works on .NET Core, enabling its use on different platforms, including Windows, Linux, and macOS.
  • Font and Graphics Support: Integrate custom fonts and graphics into PDF documents.

Installing PdfSharpCore

To start using PdfSharpCore in your .NET project, you can add the corresponding NuGet package by running the following command in your NuGet Package Manager console:

Install-Package PdfSharpCore

Or, by using the NuGet Package Manager in Visual Studio.

How to Use PdfSharpCore

Creating a Basic PDF

The following shows how to create a basic PDF document with text using PdfSharpCore:

using PdfSharpCore.Drawing;
using PdfSharpCore.Pdf;

public void CreatePdf()
{
    // Create a new PDF document
    PdfDocument document = new PdfDocument();
    document.Info.Title = "Creating a PDF with PdfSharpCore";

    // Create a new page
    PdfPage page = document.AddPage();

    // Get the XGraphics object for the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Create a font
    XFont font = new XFont("Verdana", 20, XFontStyle.Bold);

    // Draw the text
    gfx.DrawString("Hello, world!", font, XBrushes.Black, new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);

    // Save the document
    string filename = "HelloWorld.pdf";
    document.Save(filename);
}
Copied!

Adding Graphics to the PDF

PdfSharpCore also allows adding graphics such as lines, rectangles, and ellipses. Here is an example of how to do it:

public void CreatePdfWithGraphics()
{
    PdfDocument document = new PdfDocument();
    document.Info.Title = "PDF with Graphics";

    PdfPage page = document.AddPage();
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Draw a rectangle
    gfx.DrawRectangle(XPens.Black, XBrushes.LightGray, new XRect(50, 50, 200, 100));

    // Draw an ellipse
    gfx.DrawEllipse(XPens.Blue, new XRect(300, 50, 200, 100));

    // Draw a line
    gfx.DrawLine(XPens.Red, 50, 200, 450, 200);

    string filename = "GraphicsSample.pdf";
    document.Save(filename);
}
Copied!

Modifying an Existing PDF

PdfSharpCore also allows modifying existing PDF documents. Here is an example of how to open an existing PDF document and add text to it:

public void ModifyExistingPdf(string existingPdfPath)
{
    // Open the existing PDF document
    PdfDocument document = PdfReader.Open(existingPdfPath, PdfDocumentOpenMode.Modify);

    // Get the first page of the document
    PdfPage page = document.Pages[0];

    // Get the XGraphics object for the page
    XGraphics gfx = XGraphics.FromPdfPage(page);

    // Create a font
    XFont font = new XFont("Verdana", 14, XFontStyle.Regular);

    // Draw the text on the existing page
    gfx.DrawString("Text added to an existing PDF.", font, XBrushes.Black, new XPoint(50, 50));

    // Save the modified document
    document.Save(existingPdfPath);
}
Copied!