Create a Hello World PDF in C++

The SimpleExample function creates a PDF containing Hello, world! at position (100, 600). It uses Times New Roman at 12 points with a blue background and yellow text.

Configure the Aspose.PDF for C++ SDK and ensure Times New Roman is available. Pass a writable PDF path as outputFile.

Create and style the text

  1. Create a Document.
  2. Add a page with PageCollection::Add().
  3. Create a TextFragment containing Hello, world!.
  4. Set its location with a Position at (100, 600).
  5. Configure the TextFragmentState with size 12 and Times New Roman from FontRepository::FindFont().
  6. Set the text background to blue and foreground to yellow using Color.
  7. Create a TextBuilder for the page and call AppendText().
  8. Save the PDF with Document::Save().
#include <Aspose.PDF.Cpp/Document.h>
#include <Aspose.PDF.Cpp/Page.h>
#include <Aspose.PDF.Cpp/PageCollection.h>
#include <Aspose.PDF.Cpp/Color.h>
#include <Aspose.PDF.Cpp/Text/Position.h>
#include <Aspose.PDF.Cpp/Text/FontRepository.h>
#include <Aspose.PDF.Cpp/Text/TextBuilder.h>
#include <Aspose.PDF.Cpp/Text/TextFragment.h>
#include <Aspose.PDF.Cpp/Text/TextFragmentState.h>

using namespace System;
using namespace Aspose::Pdf;
using namespace Aspose::Pdf::Text;

void SimpleExample(String outputFile)
{
    auto document = MakeObject<Document>();
    auto page = document->get_Pages()->Add();
    auto text = MakeObject<TextFragment>(u"Hello, world!");
    text->set_Position(MakeObject<Position>(100, 600));
    text->get_TextState()->set_FontSize(12);
    text->get_TextState()->set_Font(FontRepository::FindFont(u"Times New Roman"));
    text->get_TextState()->set_BackgroundColor(Color::get_Blue());
    text->get_TextState()->set_ForegroundColor(Color::get_Yellow());
    MakeObject<TextBuilder>(page)->AppendText(text);
    document->Save(outputFile);
}

After running SimpleExample(outputFile), open the saved PDF to inspect the text position and colors.