이 튜토리얼에서는 **C++에서 PDF 파일을 읽는 방법을 배웁니다. C++에서 간단한 API 호출을 사용하여 PDF 파일의 모든 페이지 또는 전체 문서에서 텍스트 또는 이미지를 추출할 수 있습니다. 이 기능은 Windows 또는 Linux 플랫폼에서 PDF를 읽기 위한 Adobe Acrobat 또는 응용 프로그램에 종속되지 않습니다.
C++에서 PDF 파일을 읽는 단계
- NuGet 패키지 관리자 도구에서 Aspose.Pdf for C++ 설치
- Aspose::Pdf 네임스페이스에 대한 참조 추가
- Document Class를 사용하여 입력 PDF 로드
- TextFragmentAbsorber 클래스 인스턴스 초기화
- 추출된 텍스트를 콘솔에 인쇄
- 문서의 각 페이지와 이미지를 반복합니다.
- 추출된 출력 이미지를 JPG 파일로 저장
몇 줄의 간단한 코드로 C++*에서 *pdf 파일을 열고 읽을 수 있습니다. PDF 파일에서 모든 텍스트와 이미지를 효율적으로 추출할 수 있습니다.
C++에서 PDF 파일을 읽는 코드
#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++; | |
} | |
} | |
} | |
}; |
이전 주제에서는 C#에서 PDF 양식 필드를 병합하는 방법에 대해 살펴보았습니다. 이 항목은 C++*에서 *PDF 파일의 텍스트를 읽는 방법에 중점을 둡니다.