Bắt đầu nhanh

Bắt đầu nhanh

Trang này hiển thị đoạn mã tối thiểu để mở một PDF, trích xuất văn bản của nó và vẽ một trang thành hình ảnh raster bằng cách sử dụng Document, TextAbsorber, và một lớp thiết bị.


Yêu cầu trước

Trước khi chạy các ví dụ, hãy hoàn thành Installation.


Mở PDF, Trích xuất Văn bản và Vẽ một Trang

Mở một tài liệu, in số lượng trang, trích xuất toàn bộ văn bản bằng Aspose::Pdf::Text::TextAbsorber, và vẽ trang 1 thành JPEG ở 150 DPI với JpegDevice:

#include <aspose/pdf/document.hpp>
#include <aspose/pdf/page_collection.hpp>
#include <aspose/pdf/text_absorber.hpp>
#include <aspose/pdf/jpeg_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::JpegDevice jpeg(Aspose::Pdf::Devices::Resolution(150));
    std::ofstream out("page1.jpg", std::ios::binary);
    jpeg.Process(doc.Pages()[1], out);
}

TextAbsorber::Visit chấp nhận toàn bộ Document hoặc một Page duy nhất. {Device}::Process nhận một trang và một luồng đầu ra — cùng một mẫu áp dụng cho BmpDevice và TiffDevice.


Xác định các đoạn văn bản riêng lẻ

Aspose::Pdf::Text::TextFragmentAbsorber trả về các đoạn văn bản có vị trí thay vì một chuỗi phẳng duy nhất, hữu ích khi bạn cần vị trí hoặc số lượng cho từng đoạn:

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

Aspose::Pdf::Document doc("input.pdf");
Aspose::Pdf::Text::TextFragmentAbsorber fragmentAbsorber;
fragmentAbsorber.Visit(doc);
std::cout << "Fragments: " << fragmentAbsorber.TextFragments().Count() << "\n";

Đọc siêu dữ liệu tài liệu

Document::Info() trả về một DocumentInfo với các accessor được định kiểu cho các mục từ điển chuẩn /Info:

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

Aspose::Pdf::Document doc("input.pdf");
auto& info = doc.Info();
std::cout << "Title: " << info.Title() << "\n";
std::cout << "Author: " << info.Author() << "\n";

Các bước tiếp theo

Xem thêm

 Tiếng Việt