![Add Text to PDF in C#](images/PDF.png#center)
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. Following that, this article shows how to add text to PDF files in C#.
- C# Library to Add Text to PDF
- Add Text to a PDF using C#
- Add Text to PDF using TextParagraph
- Insert Transparent Text in PDF
C# PDF Text Manipulation Library
To add text to PDF files dynamically, we will use Aspose.PDF for .NET. It is a feature-rich library that lets you create and manipulate PDF files from within your .NET applications. You can either download the library or install it using NuGet.
PM> Install-Package Aspose.PDF
C# Adding Text to a PDF
The following are the steps to add text to a PDF file in 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.
C# PDF Text Insertion Code
The following code sample shows how to insert text to a PDF file in C#.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// Open document | |
Document pdfDocument = new Document("input.pdf"); | |
// Get particular page | |
Page pdfPage = (Page)pdfDocument.Pages[1]; | |
// Create text fragment | |
TextFragment textFragment = new TextFragment("main text"); | |
textFragment.Position = new Position(100, 600); | |
// Set text properties | |
textFragment.TextState.FontSize = 12; | |
textFragment.TextState.Font = FontRepository.FindFont("TimesNewRoman"); | |
textFragment.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.LightGray); | |
textFragment.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Red); | |
// Create TextBuilder object | |
TextBuilder textBuilder = new TextBuilder(pdfPage); | |
// Append the text fragment to the PDF page | |
textBuilder.AppendText(textFragment); | |
// Save resulting PDF document. | |
pdfDocument.Save("output.pdf"); |
C# Insert 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 C# code sample shows text insertion in a PDF using TextParagraph class.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
string fontFile = "font.ttf"; | |
// Load input PDF file | |
Document doc = new Document("input.pdf"); | |
// Create text builder object for first page of document | |
TextBuilder textBuilder = new TextBuilder(doc.Pages[1]); | |
// Create text fragment with sample string | |
TextFragment textFragment = new TextFragment("Hello world"); | |
if (fontFile != "") | |
{ | |
// Load the TrueType font into stream object | |
using (FileStream fontStream = File.OpenRead(fontFile)) | |
{ | |
// Set the font name for text string | |
textFragment.TextState.Font = FontRepository.OpenFont(fontStream, FontTypes.TTF); | |
// Specify the position for Text Fragment | |
textFragment.Position = new Position(10, 10); | |
// Add the text to TextBuilder so that it can be placed over the PDF file | |
textBuilder.AppendText(textFragment); | |
} | |
// Save resulting PDF document | |
doc.Save("output.pdf"); | |
} |
Insert 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 in C#.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET | |
// Create Document instance | |
Document doc = new Document("input.pdf"); | |
// Create page to pages collection of PDF file | |
Aspose.Pdf.Page page = doc.Pages.Add(); | |
// Create Graph object | |
Aspose.Pdf.Drawing.Graph canvas = new Aspose.Pdf.Drawing.Graph(100, 400); | |
// Create rectangle instance with certain dimensions | |
Aspose.Pdf.Drawing.Rectangle rect = new Aspose.Pdf.Drawing.Rectangle(100, 100, 400, 400); | |
// Create color object from Alpha color channel | |
rect.GraphInfo.FillColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.FromArgb(128, System.Drawing.Color.FromArgb(12957183))); | |
// Add rectanlge to shapes collection of Graph object | |
canvas.Shapes.Add(rect); | |
// Add graph object to paragraphs collection of page object | |
page.Paragraphs.Add(canvas); | |
// Set value to not change position for graph object | |
canvas.IsChangePosition = false; | |
// Create TextFragment instance with sample value | |
TextFragment text = new TextFragment("transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text transparent text "); | |
// Create color object from Alpha channel | |
Aspose.Pdf.Color color = Aspose.Pdf.Color.FromArgb(30, 0, 255, 0); | |
// Set color information for text instance | |
text.TextState.ForegroundColor = color; | |
// Add text to paragraphs collection of page instance | |
page.Paragraphs.Add(text); | |
// Save the updated PDF file | |
doc.Save("output.pdf"); |
Free PDF Text Insertion Library
You can get a free temporary license for text insertion in PDF files without any limitations.
C# PDF Library
You can explore more about the C# PDF library using the documentation. Also, you can post your queries on our forum.
Conclusion
In this article, you have learned how to add text to existing PDF files using C#. In addition, you have seen how to add transparent text to a PDF dynamically. You can easily install the library and use the provided methods of text insertion in PDF files.