Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save documentprocessing/376070948af467112d5330557774aa38 to your computer and use it in GitHub Desktop.
Save documentprocessing/376070948af467112d5330557774aa38 to your computer and use it in GitHub Desktop.
Create PDF using PDFSharp API for .NET
using System;
using PdfSharp.Pdf;
using PdfSharp.Drawing;
class Program
{
static void Main(string[] args)
{
// Create a new PDF document
PdfDocument document = new PdfDocument();
document.Info.Title = "Hello World PDF";
// Add a new page to the document
PdfPage page = document.AddPage();
// Create an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);
// Define the font to use
XFont font = new XFont("Verdana", 20, XFontStyle.Bold);
// Draw the text on the page
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);
// Inform the user
Console.WriteLine($"PDF file '{filename}' created successfully!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment