Add Text to PDF via C#

Add text to PDF document with .NET. Use Aspose.PDF to modify PDF documents programmatically

How to Work with Text in PDF Using .NET Library

To add Text into PDF File, we’ll use Aspose.PDF for .NET API, which is a feature-rich, powerful, and easy-to-use document manipulation API for .NET. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Add Text to PDF File via C#

To try the code in your environment, you need Aspose.PDF for .NET.

  1. Load the PDF with an instance of Document.
  2. Create a TextParagraph and define its properties.
  3. Add the TextParagraph to Page using TextBuilder.
  4. Save the file again.

Add Text to PDF - C#

This sample code shows how to add text into PDF document - C#

    // Open document
    Document pdfDocument = new Document(dataDir + "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
    dataDir = dataDir + "AddText_out.pdf";
    // Save resulting PDF document.