Create PDF Files using C# – .NET PDF API

Create PDF Documents using C#

Automated generation and processing of PDF documents have become a demanding feature in recent days. In various applications, PDF documents are generated dynamically such as invoices, receipts, and different types of reports. In accordance with that, this article covers the basic implementation of PDF automation within .NET applications. Ultimately, you will learn how to create PDF files using C#. The code samples will demonstrate how to insert text, images, tables, and other components in PDF using C#.

Info: Aspose provides a free online web app that allows you to view PDFs online, another that allows you to convert PDFs to video, and one that allows you to edit PDFs online.

C# .NET PDF API – Free Download

Aspose.PDF for .NET is a PDF file manipulation API that lets you generate and process PDF files from within your .NET applications. Using the API you can generate various kinds of reports and business documents dynamically. The API is available as a downloadable DLL as well as hosted on NuGet.

Install-Package Aspose.Pdf

Create PDF Files in C#

Let’s start by creating a simple PDF document containing a text fragment. The following are the steps to create a PDF file using C#.

The following code sample shows how to create a simple PDF file using C#.

Read more about creating complex PDF documents.

Edit a PDF File in C#

Modifying a PDF file is as simple as creating a new one. Simply load the file using the Document class, perform your desired operations, and save it. The following are the steps to modify a PDF.

  • Create an object of the Document class and provide a PDF file’s path to its constructor.
  • Manipulate the pages or content of the document.
  • Save the document using Document.Save() method.

The following code sample shows how to modify a PDF using C#.

Insert Image in PDF using C#

Let’s now check how to insert an image into the PDF document. The following are the steps to do so.

  • Create an object of Document class to open a PDF document.
  • Access the page you want to add an image to using Page class.
  • Add the image to the page’s Resources collection.
  • Use operators to place the image on the page:
    • GSave operator to save the current graphical state.
    • ConcatenateMatrix operator to specify where the image is to be placed.
    • Do operator to draw the image on the page.
    • Finally, use the GRestore operator to save the updated graphical state.
  • Save the PDF file.

The following code sample shows how to add an image to a PDF document using C#.

Read more about insert images in PDF.

Create a Table in PDF using C#

The tables are an essential component of the documents that are used to organize the data in the form of rows and columns. Aspose.PDF for .NET provides you with quite a simple way to create and insert tables in PDF documents. The following are the steps to perform this operation.

  • Load the PDF file using Document class.
  • Initialize a table and set its columns and rows using Table class.
  • Set table’s settings (i.e. borders).
  • Populate table by creating rows using Table.Rows.Add() method.
  • Add the table to the page using Document.Pages[1].Paragraphs.Add(Table) method.
  • Save the PDF file.

The following code sample shows how to create and add a table in a PDF document in C#.

Read more about creating tables in PDF.

Create a Form in PDF in C#

Forms in PDFs are used to collect the data from the readers. You can insert textboxes, checkboxes, radio buttons, and other supported controls in PDF forms. PDF format supports two types of forms; Acro forms and XFA forms (see details). The following are the steps to create and add forms in a PDF.

The following code sample shows how to add forms to the PDF document using C#.

Read more about creating forms in PDF.

Conclusion

In this post, you have learned how to create PDF files from scratch using C#. Furthermore, you have come to know how to insert various types of components such as text, images, tables, and forms in a PDF document programmatically. You can learn more about the PDF API using the documentation.

See Also