इस ट्यूटोरियल में, हम सीखेंगे कि PDF फाइल को C++** में कैसे पढ़ें। आप सी++ में साधारण एपीआई कॉल के साथ किसी भी पेज या पीडीएफ फाइल के पूरे दस्तावेज़ से टेक्स्ट या चित्र निकाल सकते हैं। यह सुविधा विंडोज या लिनक्स प्लेटफॉर्म पर पीडीएफ पढ़ने के लिए एडोब एक्रोबैट या किसी एप्लिकेशन पर निर्भर नहीं है।
पीडीएफ फाइल को सी++ में पढ़ने के चरण
- NuGet पैकेज मैनेजर टूल से Aspose.Pdf for C++ इंस्टॉल करें
- Aspose::Pdf नाम स्थान में संदर्भ जोड़ें
- Document Class का उपयोग करके इनपुट PDF लोड करें
- TextFragmentAbsorber क्लास इंस्टेंस को इनिशियलाइज़ करें
- कंसोल पर निकाले गए टेक्स्ट को प्रिंट करें
- प्रत्येक पृष्ठ और दस्तावेज़ की छवि के माध्यम से पुनरावृति
- निकाली गई आउटपुट छवि को JPG फ़ाइल के रूप में सहेजें
आप कोड की कुछ सरल पंक्तियों के साथ सी++* में पीडीएफ फाइल को *खोल और पढ़ सकते हैं। यह पीडीएफ फाइल से सभी टेक्स्ट और इमेज को कुशलतापूर्वक निकाल सकता है।
सी++ में पीडीएफ फाइल पढ़ने के लिए कोड
#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++; | |
} | |
} | |
} | |
}; |
पिछले विषय में, हमने सी # में पीडीएफ फॉर्म फ़ील्ड को कैसे फ़्लैट करें की खोज की थी। यह विषय इस बात पर केंद्रित है कि सी++ में पीडीएफ फाइल से टेक्स्ट कैसे पढ़ा जाए।