create a PDF File using C#

create a PDF File using C#

Design a blueprint of your PDF file, build logic, and create your PDF file using this .NET PDF library. The PDF file format is the most widely used file format at the current time. Equipping your business application with an enterprise-level PDF generator will give your business a competitive edge. This library makes it possible as it offers a wide range of methods that give you control over PDF file manipulation and conversion. Therefore, in this blog post, we will learn how to create a PDF File using C# programmatically.

We will cover the following sections in this article:

C# PDF Generator API Installation

You can create PDF files the way you want as this library lets you automate your business reports and other critical documents. However, the installation procedure of this .NET API is very simple.

Download the DLL files or run the following command in the NuGet package manager to integrate this API into your project.

Install-Package Aspose.PDF 

Create a PDF File using C#

Now, we are all set to create our first PDF file programmatically using C#.

Follow the steps and the code snippet mentioned below:

  1. Instantiate an object of the Document class by calling its empty constructor.
  2. Create a page in the PDF document by calling Pages.Add() method.
  3. Create an object of TextFragment class and add a string inside the segment.
  4. Add the fragment to the paragraphs collection by calling Paragraphs.Add method.
  5. Save the document by calling the Save method.

The following code snippet shows how to create a PDF using C#:

Add a font in a PDF programmatically in C#

Now, we can dive further and explore other features such as adding a font to the PDF file programmatically.

You may follow the following steps:

  1. Create an object of the Document class.
  2. Create a page in a PDF document by calling Pages.Add() method.
  3. Initialize an instance of TextFragment class with an empty string.
  4. Create a TextSegment object to add a text segment to a PDF document.
  5. Get the text state for the text that the TextSegment object represents by calling Text.TextState() method.
  6. Search and find a font with a specified font name by calling FontRepository.FindFont method.
  7. Make the font embedded by setting the IsEmbedded property to true.
  8. Fetch the text state of a text and assign it to the TextState property.
  9. Set the text segment in a fragment element by calling Add method.
  10. Invoke the Paragraphs.Add method to add fragment to the paragraphs collection.
  11. Call the Save method to save the document.

The following sample code explains how to add a font in a PDF using C#:

Search & Replace Text in a PDF file using .NET library

In this section, we will learn how can we edit a PDF file programmatically in C#. We will implement search & replace functionality in an existing PDF file.

We will go through the steps and the code snippet mentioned below:

  1. Open the document by initializing the constructor of the Document class with a source PDF file.
  2. Create TextFragmentAbsorber object to find all instances of the input search phrase.
  3. Pages.Accept method will accept the absorber for all the pages.
  4. Get the extracted text fragments by accessing the TextFragments property.
  5. Loop through the fragments, replace the text by setting the Text property with a new value, and change the font size of the new text by setting FontSize of the TextState property.
  6. Invoke the Save method to save the resulting PDF document.

The sample code below demonstrates how to search and replace text in a PDF document using C#:

The output of this code snippet can be seen in the image below:

create a PDF File using C#

Get a Free License

You can avail of a free temporary license to try the API beyond evaluation limitations.

Summing up

This brings us to the end of this blog post. We have gone through some important topics regarding C# PDF file generator API. You have learned how to create a PDF File using C#, and how to edit existing PDF files such as adding fonts, searching, and replacing the text. In addition, you may visit the documentation of this library to learn about other methods. Further, you may find the code examples of the features in this Github repo. We will publish other useful articles in the near future. Therefore, stay in touch with for the latest updates.

Ask a question

You can let us know about your questions or queries on our forum.

See Also