Lavora con testo in PDF tramite C++

Come lavorare con il testo in un PDF utilizzando la libreria C++

Come lavorare con il testo in un PDF utilizzando la libreria C++

Per aggiungere testo al file PDF, useremo l’API Aspose.PDF for C++, che è un’API di manipolazione di documenti ricca di funzionalità, potente e facile da usare per la piattaforma C++. Apri il gestore di pacchetti NuGet, cerca Aspose.pdf.cpp e installa. È inoltre possibile utilizzare il seguente comando dalla console di Package Manager.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Aggiungi testo al file PDF tramite C++


È necessario Aspose.PDF for C++ per provare il codice nel proprio ambiente.

  1. Carica il PDF con un’istanza di Document.
  2. Crea un TextParagraph e definisci le sue proprietà.
  3. Aggiungi il TextParagraph alla pagina usando TextBuilder.
  4. Salvate nuovamente il file.

Aggiungi testo al PDF - C++

<% text.code-block.subtitle %>


    // 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);