Σε αυτό το σεμινάριο, θα μάθουμε πώς να διαβάζουμε το αρχείο PDF σε C++. Μπορείτε να εξαγάγετε κείμενο ή εικόνες από οποιαδήποτε σελίδα ή ολόκληρο έγγραφο του αρχείου PDF με απλές κλήσεις API σε C++. Η δυνατότητα δεν εξαρτάται από το Adobe Acrobat ή οποιαδήποτε εφαρμογή για ανάγνωση PDF σε πλατφόρμες Windows ή Linux.
Βήματα για την ανάγνωση του αρχείου PDF σε C++
- Εγκαταστήστε το εργαλείο διαχείρισης πακέτων Aspose.Pdf for C++ από το NuGet
- Προσθέστε την αναφορά στον χώρο ονομάτων Aspose::Pdf
- Φόρτωση εισόδου PDF χρησιμοποιώντας Document Class
- Αρχικοποιήστε μια παρουσία κλάσης TextFragmentAbsorber
- Εκτυπώστε το εξαγόμενο κείμενο στην κονσόλα
- Επαναλάβετε σε κάθε σελίδα και εικόνα του εγγράφου
- Αποθηκεύστε την εξαγόμενη εικόνα εξόδου ως αρχείο JPG
Μπορείτε να ανοίξετε και να διαβάσετε αρχείο pdf σε C++ με λίγες απλές γραμμές κώδικα. Μπορεί να εξαγάγει αποτελεσματικά όλο το κείμενο και τις εικόνες από το αρχείο PDF.
Κώδικας για ανάγνωση αρχείου PDF σε 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++; | |
} | |
} | |
} | |
}; |
Στο προηγούμενο θέμα, εξερευνήσαμε το Πώς να ισοπεδώσετε τα πεδία φόρμας PDF σε C#. Αυτό το θέμα εστιάζει στο πώς να διαβάζετε κείμενο από αρχείο PDF σε C++.