.NET library for PDF generation
Generate PDF documents using the layout API that can create PDF files as complex or simple as you need. Describe the layout of your document in code using the Fluent API. The powerful C# PDF generator with paging support will do the rest.
Use structural elements as building blocks in C# or VB.NET code to get perfect documents. The elements are pages, containers, tables, paragraphs, images, links, and the like. The library will break document content into pages automatically. You can add page headers and footers. The API supports custom reusable components. Layout API offers built-in components like table of contents or watermarks.
The layout API is available as a free add-on for Docotic.Pdf library. Both the library and the add-on are 100% managed code DLLs. They do not have 3rd-party external dependencies. You can use the API to create PDF documents on Windows, Linux, in Amazon and Azure cloud environments. macOS, iOS, and Android are fine, too.
Docotic.Pdf and Layout add-on are available on NuGet and from our site. Get the library, the add-on, and a free time-limited license key on the Download C# .NET PDF library page.
9.5.17664-dev 9.5.17664-dev14,820 passed Total NuGet downloads 4,998,853
Fluent API to create PDF
The add-on provides a fluent API. You can chain most of the calls together. This leads to a more compact code than with "declarative" frameworks. And you still can use all regular approaches to developing code. For example, you can make your layout implementation more concise with reusable parts from helper classes and methods.
How to create PDF document in C#
Ok, it's time for some examples.
Hello, PDF!
Here is a simple example that shows how to generate PDF in C# using Layout API. It creates a document with two pages. It's kind of synthetic but illustrates the way the API works.
PdfDocumentBuilder.Create().Generate("pages.pdf", doc =>
{
doc.Pages((PageLayout page) =>
{
page.Size(PdfPaperSize.A5);
page.Header().PaddingTop(70).AlignCenter().Text("Hello, PDF!");
page.Content().Column(c =>
{
for (int i = 0; i < 10; i++)
c.Item().AlignCenter().Text($"Page 1 block {i + 1}");
c.Item().PageBreak();
c.Item().Text("Page 2");
});
page.Footer().Text(t => t.CurrentPageNumber());
});
});
The PdfDocumentBuilder.Generate method provides an instance of the Document class. Using the object and its Pages method, it is possible to define a layout for one or more pages.
The sample code sets the page size and describes the contents in the Header
, the Content
and
the Footer
slots. All these slots have the same LayoutContainer
type. The LayoutContainer
is a key building
block of the layout API. Using containers you can generate arbitrary layouts.
The layout engine generates pages to contain all the data. The exact number depends on the content in the PageLayout.Content and other slots. The library repeats contents of the Header and the Footer slots on every page. All the pages will have the same size.
To have pages with different layout or of different size, use more than one call to the Pages method.
Create text PDF in C#
A common task is to convert a string of text into a PDF file. The following code uses the layout API to create a PDF document from a text file in C#.
PdfDocumentBuilder
.Create()
.Generate("long-text.pdf", doc => doc.Pages(page =>
{
string text = File.ReadAllText(@"PDF_Succinctly.txt");
page.Content().Text(text);
}));
The code reads all the text from the file and uses it for the Content slot. There is no need to calculate any sizes or divide text string into chunks. Layout add-on takes care of all these tasks. Using the API, it's easy to convert text to PDF in C# code.
Encrypt PDF in .NET
To produce an encrypted PDF, use an encryption handler like in the following C# code example.
PdfDocumentBuilder
.Create()
.Encryption(new PdfStandardEncryptionHandler("owner", "user"))
.Generate("encrypted-with-password.pdf", doc => doc.Pages(page =>
{
// lay out the page content here
}));
Library will produce a PDF encrypted with the user and owner passwords. It is possible to specify PDF permissions for the users of the encrypted document. You can also protect PDFs with certificates.
Further reading
Layout API provides a lot of means to generate PDF in C#. We encourage you to read about them in the following articles: