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
- Create a PDF File using C#
- Add a font in a PDF programmatically in C#
- Search & replace text in a PDF file using .NET library
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.
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:
- Instantiate an object of the Document class by calling its empty constructor.
- Create a page in the PDF document by calling Pages.Add() method.
- Create an object of TextFragment class and add a string inside the segment.
- Add the fragment to the paragraphs collection by calling Paragraphs.Add method.
- 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:
- Create an object of the Document class.
- Create a page in a PDF document by calling Pages.Add() method.
- Initialize an instance of TextFragment class with an empty string.
- Create a TextSegment object to add a text segment to a PDF document.
- Get the text state for the text that the TextSegment object represents by calling Text.TextState() method.
- Search and find a font with a specified font name by calling FontRepository.FindFont method.
- Make the font embedded by setting the IsEmbedded property to true.
- Fetch the text state of a text and assign it to the TextState property.
- Set the text segment in a fragment element by calling Add method.
- Invoke the Paragraphs.Add method to add fragment to the paragraphs collection.
- 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:
- Open the document by initializing the constructor of the Document class with a source PDF file.
- Create TextFragmentAbsorber object to find all instances of the input search phrase.
- Pages.Accept method will accept the absorber for all the pages.
- Get the extracted text fragments by accessing the TextFragments property.
- 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.
- 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:
Get a Free License
You can avail of a free temporary license to try the API beyond evaluation limitations.
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 conholdate.com for the latest updates.
Ask a question
You can let us know about your questions or queries on our forum.