Created
October 19, 2024 07:55
-
-
Save documentprocessing/376070948af467112d5330557774aa38 to your computer and use it in GitHub Desktop.
Create PDF using PDFSharp API for .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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