How to Create a PDF in C# with Xceed PdfLibrary for .NET
Generating PDFs from your .NET app is a common need: reports, invoices, or documents that users can print or share. Doing it in code gives you full control over layout, text, and structure without manual editing or external tools.
This guide shows how to create a PDF in C# step by step: you’ll add a simple PDF with a title and body text using Xceed PdfLibrary for .NET. By the end you’ll have a runnable snippet, understand the core API, and know where to find samples for bookmarks, watermarks, and more.
What you need
- .NET 5 through .NET 10
- The Xceed PdfLibrary for .NET assembly (trial or license). Add it via NuGet or reference the DLL from your project.
- A couple of minutes
Set up the project and license
Create a console or web project and add the library. From the command line (.NET CLI):
dotnet add package Xceed.PdfLibrary.NETIn Visual Studio you can use the NuGet package manager or browse to Xceed.PdfLibrary.NET on NuGet.
Licensing: Xceed PdfLibrary requires a license key (trial or purchased) before you create or load any PDF. Get a free trial key or a full license from the product page.
Set the key once at application startup, before any PdfDocument calls:
using Xceed.PdfLibrary.NET;
// Set the license key once at startup (e.g. in your app's entry point)
Licenser.LicenseKey = "YOUR-TRIAL-OR-PURCHASED-KEY";
using var doc = PdfDocument.Create("output.pdf");
// ... rest of your codeCreate and save a PDF
To create a PDF in C# after referencing the library, call PdfDocument.Create(path). The path can be relative (e.g. “output.pdf”, written in your app’s current directory) or a full path. The document starts with one page; get it with doc.Pages.First(). From there you add a title and a line of text.
using var doc = PdfDocument.Create("output.pdf");
var page = doc.Pages.First();
var style = TextStyle.WithFont(doc.Fonts.GetStandardFont(StandardFontType.Helvetica), 18);
page.AddParagraph("Hello from the blog", style, new ParagraphStyle(ParagraphHorizontalAlignment.Center));
page.AddText("This PDF was created with Xceed PdfLibrary for .NET.", new Point(50, 120));
doc.Save();The code creates a file, adds a centered title and a line of text at position (50, 120), then saves. Coordinates are in points (72 per inch), with the origin at the top-left of the page and Y increasing downward.
AddParagraph is ideal for headings and blocks that can wrap; AddText places a single line at a given point. Copy the snippet above into your own console or web project and run it.
When to use AddParagraph vs AddText
Use AddParagraph when you have a block of text (a heading or a body paragraph) that should wrap within margins or a rectangle. You can set alignment (left, center, right) via ParagraphStyle. For example, report titles and body paragraphs are good candidates for AddParagraph.
Use AddText when you need a single line at an exact position, for example labels, footers, or positioned snippets such as “Page 1 of 5”. Both methods accept a TextStyle for font, size, and color. Picking the right method keeps layout predictable and avoids manual line breaks.
Add more pages and content
New pages are added with doc.Pages.Add(). You get back the new page and add content the same way: AddParagraph, AddText, or later AddImage, form fields, and so on. The API is the same for every page, so building a multi-page report is a matter of looping and adding content.
For example, loop from 0 to N, call Add(), then add a heading and body text to each new page. Multi-page documents are useful for reports, invoices with itemized pages, or any content that exceeds a single sheet.
The same AddParagraph and AddText calls work on every page; you only need to track the vertical position (Y) if you want to flow content down the page.
You can set default page size and margins when creating the document or per page. Xceed PdfLibrary uses a coordinate system with the origin at the top-left; Y increases downward. That matches how many other document APIs work and is easy to reason about for headers, footers, and columns.
If you need a different page size (e.g. A5 or legal), configure it when creating the document or on the page before adding content.
Styling: fonts and colors
Choose the font and size via TextStyle.WithFont (or WithFontAndColor for colored text). Standard PDF fonts such as Helvetica, Helvetica-Bold, and Times are built in. For custom branding or languages you can embed TrueType fonts from a file path or stream. For centered or right-aligned paragraphs, pass a ParagraphStyle with the alignment you want.
Mixing fonts and sizes in the same document is straightforward: create a TextStyle per combination and pass it to AddParagraph or AddText.
The library works with .NET 5 through .NET 10 and runs on Windows, macOS, and Linux. The same code works in desktop apps, ASP.NET, or other host environments.
Ready to build PDFs in .NET?
Download Xceed PdfLibrary for .NET and try it free for 45 days, no commitment.
Run the code
Paste the snippets above into a console or web project, set your license key, and run. The PDF is written to the path you passed to PdfDocument.Create (e.g. output.pdf). From Visual Studio the current directory is usually the project folder; from the command line it is the folder from which you invoked the app.
Open the generated file in any PDF viewer to confirm the title and text appear as expected. If the file is not where you expect, use an absolute path (e.g. Path.Combine(AppContext.BaseDirectory, “output.pdf”)) or check your working directory in the debugger.
Common questions
Do I need to dispose the document? Yes. Use using (as in the snippet) or call Dispose so file handles and streams are released.
Can I save to a stream instead of a file? Yes. Use the overloads that accept a Stream for creation or SaveAs(stream) when saving. That is useful when generating PDFs in memory for web responses or APIs.
Can I create a PDF in memory without a file? Yes. Create the document with a Stream (e.g. MemoryStream) and call Save or SaveAs to write to that stream. You can then use the buffer for uploads or responses.
What if I get a license or file error? Set Licenser.LicenseKey before any PdfDocument call. If the PDF is not found after running, check that the path is relative to your process current directory or use an absolute path.
Where can I see more samples? See the Xceed PdfLibrary for .NET documentation for advanced topics and more examples.
Can I use this in an ASP.NET Core app? Yes. The same code runs in any .NET host. Set the license key at startup (e.g. in Program.cs or when building the host), then create or load PDFs in your controllers or services. For web responses, create the document with a MemoryStream and return it with the appropriate Content-Type: application/pdf header.
Summary: You’ve seen how to create a PDF in C#: install the package, set the license key once at startup, then use PdfDocument.Create, Pages, AddParagraph, and AddText to build your first PDF.
What else can you do with Xceed PdfLibrary?
With Xceed PdfLibrary for .NET you can add bookmarks, hyperlinks, images, and form fields (text boxes, checkboxes, signature fields). You can load an existing PDF, modify it, and save to a new file, or extract text and metadata. The same patterns you used here (create or load, get pages, add content, save) apply across these features.
Once you’re comfortable with how to create a PDF in C#, try adding images or form fields using the same document and page API.
Next steps
- Download Xceed PdfLibrary for .NET or get a free trial key
- Install via NuGet: Xceed.PdfLibrary.NET – .NET CLI:
- Learn how to extract content from a PDF in C#!
dotnet add package Xceed.PdfLibrary.NET