كيف تقرأ ملف PDF في C ++

في هذا البرنامج التعليمي ، سوف نتعلم كيفية ** قراءة الملف PDF في C ++ **. يمكنك استخراج نص أو صور من أي صفحة أو مستند كامل لملف PDF باستخدام استدعاءات API بسيطة في C ++. لا تعتمد الميزة على Adobe Acrobat أو أي تطبيق لقراءة PDF على أنظمة Windows أو Linux.

خطوات قراءة ملف PDF في C ++

  1. قم بتثبيت Aspose.Pdf for C++ من أداة إدارة حزمة NuGet
  2. أضف المرجع إلى مساحة الاسم Aspose::Pdf
  3. تحميل إدخال PDF باستخدام Document Class
  4. تهيئة مثيل فئة TextFragmentAbsorber
  5. اطبع النص المستخرج على وحدة التحكم
  6. كرر خلال كل صفحة وصورة من المستند
  7. احفظ صورة الإخراج المستخرجة كملف 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 ++ *.

 عربي