소개
Aspose.PDF FOSS for C++은 PDF 문서를 다루기 위한 오픈소스 C++20 라이브러리입니다. API 영역은 문서 및 페이지 관리, 텍스트 추출, 래스터 렌더링, 암호화, 주석, 그리고 인터랙티브 AcroForm 필드를 아우르며—C++ 애플리케이션이 외부 PDF 엔진이나 상용 SDK에 의존하지 않고 PDF 파일을 생성, 검사 또는 변환해야 할 때 가장 일반적으로 필요한 작업들입니다.
이 라이브러리는 C++ 표준 라이브러리 외에는 어떤 것도 링크하지 않습니다. BMP, JPEG, TIFF 래스터 인코더와 페이지 래스터라이저와 같은 내부 구성 요소는 프로젝트 내에서 처음부터 직접 구현되었으므로 시스템 PDF 렌더러, 상용 PDF 컴포넌트, 혹은 서드파티 이미지 코덱에 대한 의존성이 없습니다. 이는 C++20을 준수하는 컴파일러와 표준 라이브러리를 제외하고는 의존성 풋프린트가 제로임을 의미합니다.
Aspose.PDF FOSS for C++은 MIT 라이선스로 배포됩니다. 이 글에서는 Document 클래스와 그 지원 타입이 제공하는 주요 기능 영역을 살펴보고, 이어서 빠른 시작 예제와 현재 확인된 포맷 지원 현황을 소개합니다.
주요 기능
문서 및 페이지 관리
Document 클래스는 PDF 파일을 열고, 검사하고, 저장하기 위한 진입점입니다. 페이지는 Document.Pages()을 통해 접근하며, 이는 Count(), Add(), Insert(pageNumber), Delete(pageNumber)을 지원하는 PageCollection를 반환합니다. 문서 수준 메타데이터는 Document.Info() (즉, DocumentInfo 인스턴스)를 통해 읽고 업데이트하거나, Document.SetTitle()로 직접 설정할 수 있습니다.
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Page count: " << doc.Pages().Count() << "\n";
// Insert a blank page after page 1, then drop the last page
doc.Pages().Insert(2);
doc.Pages().Delete(doc.Pages().Count());
doc.SetTitle("Generated Report");
doc.Save("output.pdf");
}
Document.Optimize()와 Document.OptimizeResources()은 저장하기 전에 사용되지 않는 리소스를 통합하고 정리하여 출력 파일 크기를 줄입니다.
텍스트 추출
Aspose::Pdf::Text::TextAbsorber은 전체 문서 또는 단일 페이지의 순수 텍스트 내용을 추출합니다. Visit()은 대상을 순회하고 Text()는 누적된 문자열을 반환합니다. TextFragmentAbsorber은 더 세밀한 수준에서 작동하며, 개별 텍스트 실행을 TextFragmentCollection를 통해 노출합니다.
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/text_fragment_absorber.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
// Extract all text from the document
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
// Extract text fragments from page 1
Aspose::Pdf::Text::TextFragmentAbsorber fragmentAbsorber;
fragmentAbsorber.Visit(doc.Pages()[1]);
auto fragments = fragmentAbsorber.TextFragments();
std::cout << "Fragments on page 1: " << fragments.Count() << "\n";
}
래스터 이미지로 렌더링
페이지는 형식별 장치 클래스 — PngDevice, JpegDevice, BmpDevice, 및 TiffDevice — 를 통해 래스터 형식으로 렌더링되며, 각각은 Process(page, output)를 구현합니다. 출력 해상도는 Resolution 클래스로 제어됩니다.
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/bmp_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto page = doc.Pages()[1];
Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
std::ofstream pngOut("page1.png", std::ios::binary);
png.Process(page, pngOut);
Aspose::Pdf::Devices::BmpDevice bmp(Aspose::Pdf::Devices::Resolution(150));
std::ofstream bmpOut("page1.bmp", std::ios::binary);
bmp.Process(page, bmpOut);
}
JpegDevice은 JPEG 출력에 대해 동일한 Process(page, output) 패턴을 따릅니다. TiffDevice는 또한 Process(document, fromPage, toPage, output)를 통해 단일 호출로 다중 페이지 출력을 지원하며, 폼 필드가 래스터 출력에 렌더링되는 방식을 제어하기 위해 RenderingOptions()를 노출합니다.
암호화 및 액세스 권한
Document.Encrypt()은 사용자 및 소유자 비밀번호로 문서를 보호하며, CryptoAlgorithm 열거형을 통해 암호를 선택합니다: RC4x40, RC4x128, AESx128, 또는 AESx256. 부여된 권한은 Permissions 타입으로 설명되며, 해당 플래그 값에는 PrintDocument, ModifyContent, ExtractContent, ModifyTextAnnotations, FillForm, ExtractContentWithDisabilities, AssembleDocument, 및 PrintingQuality가 포함됩니다.
#include <aspose/pdf/document.hpp>
int main() {
Aspose::Pdf::Document doc("input.pdf");
doc.Encrypt("user-password", "owner-password",
Aspose::Pdf::Permissions(),
Aspose::Pdf::CryptoAlgorithm::AESx256);
doc.Save("encrypted.pdf");
}
이미 열려 있는 암호화된 문서는 Document.Decrypt()으로 제자리에서 복호화됩니다. Document.IsEncrypted()은 로드된 문서가 현재 암호화 사전을 가지고 있는지 여부를 보고합니다.
주석
페이지 수준 주석 — 텍스트 메모, 도형 및 마크업 — 은 Page.Annotations()을 통해 접근할 수 있으며, 이는 AnnotationCollection을 반환합니다. 개별 항목은 Contents(), Rect(), Color(), Border(), AnnotationType()와 같은 공유 속성을 노출하며, 페이지에서 제거하거나 정적 콘텐츠로 평탄화할 수 있습니다.
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto& annotations = doc.Pages()[1].Annotations();
std::cout << "Annotation count: " << annotations.Count() << "\n";
for (int i = 0; i < static_cast<int>(annotations.Count()); ++i) {
std::cout << "Contents: " << annotations[i].Contents() << "\n";
}
// Remove the first annotation on the page
if (annotations.Count() > 0) {
annotations.Delete(annotations[0]);
}
doc.Save("output.pdf");
}
단일 Annotation도 자체 Flatten() 메서드를 사용하여 아래에 설명된 폼 평탄화 워크플로와는 무관하게 직접 정적 페이지 콘텐츠로 변환할 수 있습니다.
대화형 양식
AcroForm 필드는 Document.Form()을 통해 관리되며, 이는 Count(), HasField(fieldName), Delete(fieldName), Flatten()를 지원하는 Form 인스턴스를 반환합니다. 새 필드는 Form.Add(field, pageNumber)으로 첨부되며, 여기서 field은 TextBoxField, CheckboxField, ChoiceField, 또는 ButtonField와 같은 구체적인 Field 하위 유형입니다.
#include <aspose/pdf/document.hpp>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
auto& form = doc.Form();
std::cout << "Form fields: " << form.Count() << "\n";
if (form.HasField("Signature1")) {
form.Delete("Signature1");
}
// Convert every remaining field into static page content
form.Flatten();
doc.Save("flattened.pdf");
}
빠른 시작
라이브러리를 CMake 서브디렉터리로 추가하고 aspose_pdf_foss 타깃에 링크합니다:
add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)
문서를 열고 텍스트를 추출한 뒤 첫 페이지를 PNG로 렌더링합니다:
#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>
#include <iostream>
int main() {
Aspose::Pdf::Document doc("input.pdf");
std::cout << "Pages: " << doc.Pages().Count() << "\n";
Aspose::Pdf::Text::TextAbsorber absorber;
absorber.Visit(doc);
std::cout << absorber.Text() << "\n";
Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
std::ofstream out("page1.png", std::ios::binary);
png.Process(doc.Pages()[1], out);
}
지원되는 형식
| 형식 | 확장자 | 읽기 | 쓰기 |
|---|---|---|---|
| BMP | bmp | — | ✓ |
| JPEG | jpeg | — | ✓ |
| TIFF | tiff | — | ✓ |
| Text | txt | — | ✓ |
| SVG | svg | ✓ | — |
오픈 소스 및 라이선스
Aspose.PDF FOSS for C++는 MIT 라이선스로 출시되어 로열티 없이 상업적 사용, 수정 및 재배포가 허용됩니다. 소스는 github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp에서 확인할 수 있습니다.
MIT 라이선스 조건에 따라 상업적 사용이 허용됩니다. 프로젝트에 엔터프라이즈급 지원, 확장된 형식 지원 또는 클라우드 통합이 필요하다면 Aspose.PDF — Enterprise Product Family는 별도의 상업용 제공입니다.