PDF format is widely used to generate various types of documents such as invoices, financial reports, resumes, etc. In the world of automation, PDF documents are generated and manipulated from within the web or desktop applications. Thereby, in certain cases, you may need to add text to the existing PDF files programmatically. In accordance with that, this article shows how to add text to PDF documents using C#.
- C# API to Add Text to PDF
- Add Text to a PDF using C#
- Add Text to PDF using TextParagraph
- Insert Transparent Text in PDF
C# API to Add Text to PDF
In order to add text to PDF files dynamically, we will use Aspose.PDF for .NET. It is a feature-rich API that lets you create and manipulate PDF files from within your .NET applications. You can either download the API or install it using NuGet.
PM> Install-Package Aspose.PDF
Add Text to a PDF using C#
The following are the steps to add text to a PDF file using C#.
- Load the PDF file using Document class.
- Get the desired page of the PDF into a Page object.
- Create a TextFragment object and set its text, position, font, background color, etc.
- Create a TextBuilder object and initialize it with Page object.
- Use TextBuilder.AppendText(TextFragment) method to add text to the PDF’s page.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to add text to a PDF file.
Add Text to PDF using TextParagraph
The following are the steps to add text to a PDF file using TextParagraph.
- Load the PDF file using Document class.
- Get the desired page of PDF into a Page object or add a new one.
- Create a TextBuilder object and initialize it with Page object.
- Create an object of TextParagraph class.
- Specify the location of the paragraph using TextParagraph.Rectangle property.
- Set formatting options of the TextParagraph.
- Create a TextFragment object and set its text, position, font, background color, etc.
- Add the TextFragment to the paragraph using TextParagraph.AppendLine(TextFragment) method.
- Add paragraph to the page using TextBuilder.AppendParagraph(TextParagraph) method.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to add text to a PDF using TextParagraph class.
Add Transparent Text in PDF in C#
Aspose.PDF for .NET also allows adding transparent text to a PDF document, as demonstrated in the following steps.
- Load the PDF file using Document class.
- Get the desired page of PDF into a Page object or add a new one.
- Create and initialize a Graph object.
- Create a Rectangle object and initialize it.
- Set Rectangle.GraphInfo.FillColor property.
- Add rectangle to the Graph using Graph.Shapes.Add(Rectangle) method.
- Add Graph to the paragraphs collection of the page using Page.Paragraphs.Add(Graph) method.
- Create a TextFragment object and set its TextState.ForegroundColor property.
- Add the TextFragment to the page using Page.Paragraphs.Add(TextFragment) method.
- Save the updated PDF file using Document.Save(String) method.
The following code sample shows how to add transparent text to a PDF file.
Get a Free License
You can use Aspose.PDF for .NET without evaluation limitations using a temporary license.
Conclusion
In this article, you have learned how to add text to the existing PDF files using C#. In addition, you have seen how to add transparent text to a PDF dynamically. You can explore more about the C# PDF API using the documentation. Also, you can post your queries on our forum.