Giới thiệu
Aspose.PDF FOSS cho C++ là một thư viện mã nguồn mở C++20 để làm việc với tài liệu PDF. Bề mặt API bao phủ quản lý tài liệu và trang, trích xuất văn bản, render raster, mã hoá, chú thích, và các trường AcroForm tương tác — các thao tác thường cần nhất khi một ứng dụng C++ phải tạo, kiểm tra hoặc chuyển đổi các tệp PDF mà không phụ thuộc vào một engine PDF bên ngoài hay SDK thương mại.
Thư viện chỉ liên kết với thư viện chuẩn của C++. Các thành phần nội bộ như bộ mã hoá raster BMP, JPEG và TIFF và bộ rasterizer trang được triển khai từ đầu trong dự án, vì vậy không có phụ thuộc nào vào trình render PDF hệ thống, thành phần PDF thương mại, hay bất kỳ codec hình ảnh bên thứ ba nào. Điều này giữ cho lượng phụ thuộc ở mức không, ngoại trừ một trình biên dịch tuân thủ C++20 và thư viện chuẩn.
Aspose.PDF FOSS cho C++ được phát hành dưới giấy phép MIT. Bài viết này sẽ đi qua các khu vực tính năng chính mà lớp Document và các kiểu hỗ trợ của nó cung cấp, tiếp theo là một ví dụ Khởi động nhanh và danh sách các định dạng hiện đã được xác nhận hỗ trợ.
Các tính năng chính
Quản lý tài liệu và trang
Lớp Document là điểm vào để mở, kiểm tra và lưu các tệp PDF. Các trang được truy cập qua Document.Pages(), cái mà trả về một PageCollection hỗ trợ Count(), Add(), Insert(pageNumber) và Delete(pageNumber). Siêu dữ liệu ở mức tài liệu được đọc và cập nhật qua Document.Info() (một thể hiện DocumentInfo) hoặc thiết lập trực tiếp bằng 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() và Document.OptimizeResources() giảm kích thước tệp đầu ra bằng cách hợp nhất và loại bỏ các tài nguyên không dùng trước khi lưu.
Trích xuất Văn bản
Aspose::Pdf::Text::TextAbsorber trích xuất nội dung văn bản thuần của toàn bộ tài liệu hoặc một trang duy nhất. Visit() duyệt mục tiêu và Text() trả về chuỗi đã tích lũy. TextFragmentAbsorber hoạt động ở mức độ chi tiết hơn, tiết lộ các đoạn văn bản riêng lẻ thông qua một 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";
}
Kết xuất thành Hình ảnh Raster
Các trang được kết xuất sang định dạng raster thông qua các lớp thiết bị theo định dạng — PngDevice, JpegDevice, BmpDevice và TiffDevice — mỗi lớp thực hiện Process(page, output). Độ phân giải đầu ra được kiểm soát bằng lớp 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 tuân theo cùng mẫu Process(page, output) cho đầu ra JPEG. TiffDevice cũng hỗ trợ đầu ra đa trang trong một lần gọi thông qua Process(document, fromPage, toPage, output), và cung cấp RenderingOptions() để kiểm soát cách các trường biểu mẫu được kết xuất vào đầu ra raster.
Mã hoá và Quyền Truy cập
Document.Encrypt() bảo vệ một tài liệu bằng mật khẩu người dùng và chủ sở hữu, chọn thuật toán mã hoá thông qua enum CryptoAlgorithm: RC4x40, RC4x128, AESx128 hoặc AESx256. Các quyền được cấp được mô tả bằng kiểu Permissions, trong đó các giá trị cờ bao gồm PrintDocument, ModifyContent, ExtractContent, ModifyTextAnnotations, FillForm, ExtractContentWithDisabilities, AssembleDocument và 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");
}
Một tài liệu đã mở được mã hóa sẽ được giải mã tại chỗ bằng Document.Decrypt(). Document.IsEncrypted() báo cáo liệu tài liệu đã tải hiện đang chứa một từ điển mã hóa hay không.
Chú thích
Các chú thích ở mức trang — ghi chú văn bản, hình dạng và đánh dấu — được truy cập qua Page.Annotations(), trả về một AnnotationCollection. Các mục riêng lẻ cung cấp các thuộc tính chung như Contents(), Rect(), Color(), Border() và AnnotationType(), và có thể được gỡ bỏ khỏi trang hoặc làm phẳng thành nội dung tĩnh.
#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");
}
Bất kỳ Annotation đơn lẻ nào cũng có thể được chuyển đổi thành nội dung trang tĩnh trực tiếp bằng phương thức Flatten() riêng của nó, độc lập với quy trình làm phẳng biểu mẫu được mô tả bên dưới.
Biểu mẫu tương tác
Các trường AcroForm trên tài liệu được quản lý qua Document.Form(), trả về một thể hiện Form hỗ trợ Count(), HasField(fieldName), Delete(fieldName) và Flatten(). Các trường mới được gắn bằng Form.Add(field, pageNumber), trong đó field là một kiểu con Field cụ thể như TextBoxField, CheckboxField, ChoiceField hoặc ButtonField.
#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");
}
Bắt đầu nhanh
Thêm thư viện như một thư mục con CMake và liên kết với mục tiêu aspose_pdf_foss:
add_subdirectory(aspose.pdf-foss-for-cpp)
target_link_libraries(your_app PRIVATE aspose_pdf_foss)
Mở một tài liệu, trích xuất văn bản và chuyển trang đầu tiên sang 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);
}
Các định dạng được hỗ trợ
| Định dạng | Phần mở rộng | Đọc | Ghi |
|---|---|---|---|
| BMP | bmp | — | ✓ |
| JPEG | jpeg | — | ✓ |
| TIFF | tiff | — | ✓ |
| Text | txt | — | ✓ |
| SVG | svg | ✓ | — |
Mã nguồn mở & Giấy phép
Aspose.PDF FOSS cho C++ được phát hành theo giấy phép MIT, cho phép sử dụng thương mại, sửa đổi và phân phối lại mà không có phí bản quyền. Mã nguồn có sẵn tại github.com/aspose-pdf-foss/Aspose.PDF-FOSS-for-Cpp.
Việc sử dụng thương mại được phép theo các điều khoản của giấy phép MIT. Nếu dự án của bạn yêu cầu hỗ trợ cấp doanh nghiệp, phạm vi định dạng mở rộng, hoặc tích hợp đám mây, Aspose.PDF — Enterprise Product Family là một gói thương mại độc lập.