Language: EN

csharp-pdfsharpcore

PDF Manipulation in C# with PdfSharpCore

PdfSharpCore is a popular and robust library for PDF file manipulation in .NET applications.

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

With PdfSharpCore, we can efficiently create, modify, and process PDF documents, 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, allowing its use on different platforms, including Windows, Linux, and macOS.
  • Support for Fonts and Graphics: Integrates custom fonts and graphics into PDF documents.

For more information, detailed documentation, and access to the source code, visit the project repository on GitHub - PdfSharpCore.

Installing PdfSharpCore

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

Install-Package PdfSharpCore

Alternatively, you can use the NuGet Package Manager in Visual Studio.

How to Use PdfSharpCore

Creating a Basic PDF

Here’s 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);
}

Adding Graphics to the PDF

PdfSharpCore also allows adding graphics such as lines, rectangles, and ellipses. Here’s 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);
}

Modifying an Existing PDF

PdfSharpCore also allows modifying existing PDF documents. Here’s 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);
}