Các trường hợp sử dụng

Các trường hợp sử dụng

Hướng dẫn này trình bày các trường hợp sử dụng thực tế cho Aspose.PDF FOSS cho C++, bao gồm tạo tài liệu, chuyển đổi PDF sang hình ảnh, trích xuất văn bản, phân phối bảo mật và thu thập dữ liệu AcroForm. Thư viện chỉ liên kết với thư viện chuẩn C++ và yêu cầu một trình biên dịch C++20.

Tạo tài liệu

Tạo một PDF từ đầu bằng cách thêm các trang vào một Document mới và lưu kết quả. Mô hình này là nền tảng cho việc tạo hoá đơn, in chứng chỉ và các quy trình sản xuất báo cáo:

#include <aspose/pdf/document.hpp>

int main() {
    Aspose::Pdf::Document doc;
    doc.Pages().Add();
    doc.SetTitle("Monthly Report");
    doc.Save("report.pdf");
}

Chuyển đổi PDF sang hình ảnh

Kết xuất một trang thành ảnh raster để tạo ảnh thu nhỏ, đầu ra sẵn sàng in, hoặc cơ sở chuẩn hồi quy hình ảnh. PngDevice, JpegDevice, BmpDevice và TiffDevice mỗi cái xử lý một trang một lần ở độ phân giải nhất định:

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/png_device.hpp>
#include <aspose/pdf/resolution.hpp>
#include <fstream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    Aspose::Pdf::Devices::PngDevice png(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream out("page1.png", std::ios::binary);
    png.Process(doc.Pages()[1], out);
}

Trích xuất văn bản và lập chỉ mục

Trích xuất toàn bộ văn bản của tài liệu với TextAbsorber để lập chỉ mục tìm kiếm, các pipeline khai thác dữ liệu, hoặc xác minh nội dung tự động trong các bộ kiểm thử:

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("input.pdf");
    Aspose::Pdf::Text::TextAbsorber absorber;
    absorber.Visit(doc);
    std::cout << absorber.Text() << "\n";
}

Phân phối Tài liệu Bảo mật

Mã hoá một tài liệu bằng mật khẩu người dùng và mật khẩu chủ sở hữu riêng biệt trước khi phân phối. Mật khẩu người dùng cần thiết để mở tệp; mật khẩu chủ sở hữu điều khiển các quyền như in và trích xuất nội dung:

#include <aspose/pdf/document.hpp>

int main() {
    Aspose::Pdf::Document doc("report.pdf");
    doc.Encrypt("user-password", "owner-password",
                Aspose::Pdf::Permissions(),
                Aspose::Pdf::CryptoAlgorithm::AESx256);
    doc.Save("report-secured.pdf");
}

AcroForm Thu thập Dữ liệu

Đọc lại các giá trị đã gửi từ một biểu mẫu PDF đã điền bằng cách lặp Form.Fields() và in ra tên và giá trị hiện tại của mỗi trường — hữu ích cho việc xử lý các mẫu đơn đã điền mà không cần máy chủ gửi biểu mẫu riêng:

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/forms/form.hpp>
#include <iostream>

int main() {
    Aspose::Pdf::Document doc("submitted-form.pdf");
    auto& form = doc.Form();
    for (auto* field : form.Fields()) {
        std::cout << field->PartialName() << " = " << field->Value() << "\n";
    }
}

Xem thêm

 Tiếng Việt