通過C++管理 PDF 文件中的表格

如何使用 C++ 庫處理 PDF 中的表格

最常用的表格操作 - C++

如何管理 PDF 文件中的表格使用 C++ 庫

為了使用PDF格式的表格,我們將使用[Aspose.PDF for C++](https://products.aspose.com/pdf/cpp)API,這是一個功能豐富,功能強大且易於使用的文檔操作API,適用於 cpp 平臺。打開 [NuGet](https://www.nuget.org/packages/aspose.pdf) 包管理器,搜索“.PDF”並安裝。您也可以從程式包管理器主控台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

通過C++將表格添加到 PDF


您需要為[Aspose.PDF for C++](https://releases.aspose.com/pdf/cpp) 才能在您的環境中嘗試代碼。

  1. 載入包含文件實例的 PDF。
  2. 通過主頁索引訪問主頁。
  3. 建立表物件。
  4. 設定表格設定(例如設置邊框)。
  5. 填充表。
  6. 將表格添加到頁面。
  7. 儲存檔。

在 PDF 中新增表格 - C++


    String _dataDir("C:\\Samples\\");

    // Load source PDF document
    auto document = MakeObject<Document>(_dataDir + u"AddTable.pdf");

    // Initializes a new instance of the Table
    auto table = MakeObject<Table>();

    // Set the table border color as LightGray
    table->set_Border(MakeObject<Aspose::Pdf::BorderInfo>(
        Aspose::Pdf::BorderSide::All, .5f,
        Aspose::Pdf::Color::get_LightGray()));
    // Set the border for table cells
    table->set_DefaultCellBorder (MakeObject<Aspose::Pdf::BorderInfo>(
        Aspose::Pdf::BorderSide::All, .5f,
        Aspose::Pdf::Color::get_LightGray()));

    // Create a loop to add 10 rows
    for (int row_count = 1; row_count < 10; row_count++)
    {
        // Add row to table
        auto row = table->get_Rows()->Add();
        // Add table cells
        row->get_Cells()->Add(String::Format(u"Column ({0}, 1)", row_count));
        row->get_Cells()->Add(String::Format(u"Column ({0}, 2)", row_count));
        row->get_Cells()->Add(String::Format(u"Column ({0}, 3)", row_count));
    }

    // Add table object to first page of input document
    document->get_Pages()->idx_get(1)->get_Paragraphs()->Add(table);

    // Save updated document containing table object
    document->Save(_dataDir + u"document_with_table_out.pdf");