In questo breve tutorial impareremo come creare PDF utilizzando C++. Utilizzando l’esempio C++ crea file PDF, è possibile generare facilmente un PDF in poche chiamate API. L’esempio può essere utilizzato in qualsiasi ambiente di sviluppo MS Windows che supporta C++.
Passaggi per creare PDF utilizzando C++
- Includi la libreria Aspose.Pdf for C++ utilizzando il gestore di pacchetti NuGet
- Includi il riferimento allo spazio dei nomi Aspose::Pdf
- Inizializza l’oggetto documento per creare PDF da C++
- Inizializza l’istanza di TextFragment per impostare il testo e le relative proprietà
- Usando il metodo Salva in C++ crea PDF su disco
In C++ genera un documento PDF usando pochi semplici passaggi. Puoi ottenere ciò creando un PDF vuoto utilizzando Document Class e aggiungendo una pagina al suo interno. Quindi il testo PDF e le sue proprietà vengono impostati inizializzando l’istanza TextBuilder Class. Il file PDF generato verrà salvato su disco utilizzando il metodo Salva.
Codice per generare PDF usando C++
#pragma once | |
#include <system/string.h> | |
#include <system/shared_ptr.h> | |
#include <stdio.h> | |
#include <system/console.h> | |
#include <system/environment.h> | |
#include <system/object_ext.h> | |
#include <Aspose.PDF.Cpp/License.h> | |
#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/TextFragment.h> | |
#include <Aspose.PDF.Cpp/Text/Position.h> | |
#include <Aspose.PDF.Cpp/Text/TextBuilder.h> | |
#include <Aspose.PDF.Cpp/Text/TextFragmentCollection.h> | |
#include <Aspose.PDF.Cpp/Text/TextFragmentState.h> | |
#include <Aspose.PDF.Cpp/Text/FontRepository.h> | |
using namespace System; | |
using namespace Aspose::Pdf; | |
using namespace Aspose::Pdf::Text; | |
void CreatePdfInCpp() | |
{ | |
// Set the license for Aspose.PDF for C++ to create PDF | |
SharedPtr<License> CreatePDFLicense = System::MakeObject<License>(); | |
CreatePDFLicense->SetLicense(u"Aspose.PdfCPP.lic"); | |
// Initialize document object create PDF from C++ | |
SharedPtr<Document> PdfDocument = MakeObject<Document>(); | |
// Add empty page in PDF | |
SharedPtr<Page> pdfPage = PdfDocument->get_Pages()->Add(); | |
// Initialize Text fragment and set text | |
SharedPtr <TextFragment> textFragments = MakeObject<TextFragment>(u"Knowledgebase Text"); | |
// Apply text related properties | |
textFragments->set_Position(MakeObject < Position>(100, 600)); | |
textFragments->get_TextState()->set_FontSize(12); | |
textFragments->get_TextState()->set_Font(FontRepository::FindFont(u"TimesNewRoman")); | |
textFragments->get_TextState()-> | |
set_BackgroundColor((MakeObject<Aspose::Pdf::Color>())->FromRgb(System::Drawing::Color::get_Wheat())); | |
textFragments->get_TextState()-> | |
set_ForegroundColor((MakeObject<Aspose::Pdf::Color>())->FromRgb(System::Drawing::Color::get_Red())); | |
// Instantiate TextBuilder object | |
SharedPtr < TextBuilder> textBuilders = MakeObject< TextBuilder>(pdfPage); | |
// Append added fragment to the PDF page | |
textBuilders->AppendText(textFragments); | |
// Create PDF using C++ | |
PdfDocument->Save(u"Generated_out.pdf"); | |
} |
In C++ PDF generator può essere sviluppato utilizzando semplici chiamate API. Abbiamo assistito a come generare PDF utilizzando C++ senza alcuna dipendenza da altre API o Adobe PDF. In pochi passaggi siamo riusciti a creare un PDF da zero inserendo del testo e impostandone le rispettive proprietà testuali.
Nell’ultimo argomento, ci siamo concentrati su come appiattire un modulo PDF usando C++. Questo argomento tratta come creare PDF in C++ usando pochi semplici passaggi.