Trong hướng dẫn này, chúng ta sẽ tìm hiểu cách đọc tệp PDF trong C++. Bạn có thể trích xuất văn bản hoặc hình ảnh từ bất kỳ trang nào hoặc toàn bộ tài liệu của tệp PDF bằng các lệnh gọi API đơn giản trong C++. Tính năng này không phụ thuộc vào Adobe Acrobat hoặc bất kỳ ứng dụng nào để đọc PDF trên nền tảng Windows hoặc Linux.
Các bước để đọc tệp PDF trong C ++
- Cài đặt Aspose.Pdf for C++ từ công cụ quản lý gói NuGet
- Thêm tham chiếu vào không gian tên Aspose::Pdf
- Tải PDF đầu vào bằng Document Class
- Khởi tạo một thể hiện của lớp TextFragmentAbsorber
- In văn bản trích xuất trên bàn điều khiển
- Lặp lại qua từng trang và hình ảnh của tài liệu
- Lưu hình ảnh đầu ra được trích xuất dưới dạng tệp JPG
Bạn có thể mở và đọc tệp pdf bằng C++ với vài dòng mã đơn giản. Nó có thể trích xuất tất cả văn bản và hình ảnh từ tệp PDF một cách hiệu quả.
Mã để đọc tệp PDF trong 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++; | |
} | |
} | |
} | |
}; |
Trong chủ đề trước, chúng ta đã khám phá Cách làm phẳng các trường biểu mẫu PDF trong C#. Chủ đề này tập trung vào cách đọc văn bản từ tệp PDF trong C++.