Generate PDF Documents in .NET C#

Introduction

Programmatically creating PDF documents is a frequent requirement for many applications. In the .NET ecosystem, GrapeCity's GcPdf is a robust library that enables developers to generate, modify, and manipulate PDF documents with ease. This blog post aims to demonstrate how to use GcPdf to generate PDF documents programmatically in .NET C#, providing practical examples to illustrate its application.

What is GcPdf?

GcPdf is a .NET library that provides comprehensive support for PDF document generation and manipulation. It offers a wide range of features, including:

  • Creating PDF documents from scratch.
  • Adding text, images, and shapes to PDF pages.
  • Customizing fonts and styles.
  • Creating tables and charts.
  • Embedding hyperlinks and bookmarks.
  • Exporting PDFs to different formats.
  • Adding security features like password protection and encryption.

Let's dive into how to get started with GcPdf and create a basic PDF document.

Getting Started with GcPdf

Before we begin, make sure you have Visual Studio or your preferred C# development environment set up. You'll also need to install the GrapeCity Documents for PDF NuGet package (GcPdf) in your project.

Creating a Simple PDF Document

In this example, we'll create a simple PDF document containing text and a rectangular shape.

using System;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Text;

class Program
{
    static void Main(string[] args)
    {
        // Create a new PDF document
        var doc = new GcPdfDocument();

        // Add a page to the document
        var page = doc.NewPage();

        // Create a graphics object for drawing on the page
        var g = page.Graphics;

        // Add content to the page
        var text = "Hello, World!";
        var font = StandardFonts.Helvetica;
        var fontSize = 24;
        var textFormat = new TextFormat()
        {
            Font = font,
            FontSize = fontSize,
        };

        g.DrawString(text, textFormat, new PointF(100, 100));

        // Create a rectangle
        var rect = new RectangleF(100, 200, 200, 150);
        g.DrawRectangle(rect, Color.Red);

        // Specify the file path where you want to save the PDF
        var filePath = "example.pdf";

        // Save the document to a PDF file
        doc.Save(filePath);

        Console.WriteLine($"PDF created at {filePath}");
    }
}

Code explanation

  • We create a new GcPdfDocument object to represent the PDF document.
  • Add a page to the document using doc.NewPage().
  • Create a graphics object (g) for drawing on the page.
  • Add text and a rectangle to the page using g.DrawString() and g.DrawRectangle().
  • Specify the file path where you want to save the PDF.
  • Save the document to a PDF file using doc.Save().

After running this code, you will have a PDF file named "example.pdf" in your project directory.

Advanced Features with GcPdf

GcPdf provides extensive capabilities for creating sophisticated PDF documents. Here are a few advanced features you can explore:

Adding Images

You can add images to your PDF document using the g.DrawImage() method. This allows you to include logos, graphics, or photographs in your PDFs.

var image = Image.FromFile("logo.png");
g.DrawImage(image, new RectangleF(50, 50, 100, 100));

Creating Tables

Tables are commonly used in PDF documents for displaying tabular data. GcPdf provides a Table class for creating tables with various formatting options.

var table = new Table();
table.DataSource = GetTableData(); // Replace with your data source
page.Elements.Add(table);

Adding Hyperlinks

You can include hyperlinks in your PDFs using the g.DrawString() method with a link destination.

var hyperlinkText = "Visit our website";
var linkDestination = new LinkDestinationURI("https://example.com");
g.DrawString(hyperlinkText, textFormat, new PointF(100, 300), linkDestination);

PDF Security

GcPdf allows you to secure your PDFs by adding passwords or encrypting them. You can set document permissions and control who can view or edit the document.

var options = new PdfSaveOptions
{
    Security = new PdfSecuritySettings
    {
        OwnerPassword = "owner_password",
        UserPassword = "user_password",
        Permissions = PdfPermissions.Print | PdfPermissions.Copy,
    },
};
doc.Save("secure.pdf", options);

Conclusion

Creating customized PDFs for various applications in .NET C# by using GcPdf programmatically is a potent and versatile method. GcPdf offers all the necessary features and flexibility to generate reports, invoices, or other types of documents quickly and efficiently. To enhance your PDF generation capabilities with more in-depth information and examples, please refer to the GcPdf documentation. We wish you happy coding!


Recommended Free Ebook
Similar Articles