En este tutorial, aprenderemos cómo leer el archivo PDF en C++. Puede extraer texto o imágenes de cualquier página o documento completo del archivo PDF con simples llamadas API en C++. La función no depende de Adobe Acrobat ni de ninguna aplicación para leer PDF en plataformas Windows o Linux.
Pasos para leer un archivo PDF en C++
- Instale Aspose.Pdf for C++ desde la herramienta de administración de paquetes NuGet
- Agregue la referencia al espacio de nombres Aspose::Pdf
- Cargue el PDF de entrada usando Document Class
- Inicializar una instancia de clase TextFragmentAbsorber
- Imprime el texto extraído en la consola
- Iterar a través de cada página e imagen del documento
- Guarde la imagen de salida extraída como un archivo JPG
Puede abrir y leer un archivo pdf en C++ con unas pocas líneas de código simples. Puede extraer eficientemente todo el texto y las imágenes del archivo PDF.
Código para leer archivos PDF en C++
#pragma once | |
#include <iostream> | |
#include <system/smart_ptr.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/Resources.h> | |
#include <Aspose.PDF.Cpp/XImageCollection.h> | |
#include <Aspose.PDF.Cpp/XImage.h> | |
#include <Aspose.PDF.Cpp/Text/TextFragmentAbsorber.h> | |
#include <system/console.h> | |
#include <system/io/file.h> | |
#include <drawing/imaging/image_format.h> | |
using namespace System; | |
using namespace Aspose::Pdf; | |
class ReadPDFEx { | |
public: | |
static void ReadPDF() | |
{ | |
// Set the license for Aspose.PDF for CPP to Read PDF file | |
SharedPtr<License> ReadPdfLicense = System::MakeObject<License>(); | |
ReadPdfLicense->SetLicense(u"Aspose.PDF.NET.lic"); | |
// Load input PDF file with Document class object | |
SharedPtr<Document> doc = MakeObject<Document>(u"Test.pdf"); | |
// Initialize a TextFragmentAbsorber class instance | |
SharedPtr<Aspose::Pdf::Text::TextFragmentAbsorber> absorber = MakeObject<Aspose::Pdf::Text::TextFragmentAbsorber>(); | |
doc->get_Pages()->Accept(absorber); | |
// Print the extracted text on console | |
System::Console::WriteLine(absorber->get_Text()); | |
// Set the image counter variable | |
int ImageCount = 1; | |
// Iterate through each page of the PDF document | |
for (SharedPtr<Page> page : doc->get_Pages()) | |
{ | |
// Iterate through all the images | |
for (SharedPtr<XImage> image : page->get_Resources()->get_Images()) | |
{ | |
// Initialize an object of the FileStream type | |
System::SharedPtr<System::IO::FileStream> outputImage = | |
System::IO::File::Create(String::Format(u"Image{0}.jpg", ImageCount)); | |
// Save the output image | |
image->Save(outputImage, System::Drawing::Imaging::ImageFormat::get_Jpeg()); | |
// Close the FileStream | |
outputImage->Close(); | |
ImageCount++; | |
} | |
} | |
} | |
}; |
En el tema anterior, exploramos Cómo acoplar campos de formulario PDF en C#. Este tema se centra en cómo leer texto de un archivo PDF en C++.