Add Text to PDF via C++

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

How to Work with Text in PDF Using C++ Library

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

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Add Text to PDF File via C++


You need Aspose.PDF for C++ to try the code in your environment.

  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++


    // Load the PDF file
    auto document = MakeObject<Document>(_dataDir + inputFileName);

    // get particular page
    auto pdfPage = document->get_Pages()->idx_get(1);

    // create text fragment
    auto textFragment = MakeObject<TextFragment>("Aspose.PDF");
    textFragment->set_Position(MakeObject<Position>(80, 700));

    // set text properties
    textFragment->get_TextState()->set_Font(FontRepository::FindFont(u"Verdana"));
    textFragment->get_TextState()->set_FontSize(14);
    textFragment->get_TextState()->set_ForegroundColor(Color::get_Blue());
    textFragment->get_TextState()->set_BackgroundColor(Color::get_LightGray());

    // create TextBuilder object
    auto textBuilder = MakeObject<TextBuilder>(pdfPage);
    // append the text fragment to the PDF page
    textBuilder->AppendText(textFragment);

    // Save resulting PDF document.
    document->Save(_dataDir + outputFileName);