通过 C++ 在 PDF 文档中添加表格

如何使用 C++ 库在 PDF 中添加表格

如何使用 C++ 库在 PDF 文档中添加表格

为了在 PDF 中添加表格,我们将使用 Aspose.PDF for C++ API,这是一款功能丰富、功能强大且易于使用的适用于 cpp 平台的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以从软件包管理器控制台使用以下命令。

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

通过 C++ 将表格添加到 PDF


你需要 Aspose.PDF for C++ 才能在你的环境中试用这些代码。

1。使用 “文档” 实例加载 PDF。 1。通过其索引访问该页面。 1。创建表对象。 1。设置表格设置(例如设置边框)。 1。填充表。 1。将表格添加到页面中。 1。保存该文件。

在 PDF 中添加表格-C++。


    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");