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:
- PDF generator API installation
- Create a PDF File using C#
- Add a font in a PDF programmatically
- Search & replace text in a PDF file using .NET library
PDF generator API installation
You can process your 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 fragment to the paragraphs collection by calling Paragraphs.Add method.
- Save the document by calling the Save method.
Add a font in a PDF programmatically
Now, we can dive further and can 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 in 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 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.
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 output of this code snippet can be seen in the image below:
Get a Free License
You can avail 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 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, search & replacing the text. In addition, you may visit the documentation of this library to know about other methods. Further, you may find the code examples of the features in this Github repo. We will publish other useful articles in 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.