Add Table to PDF via C++

Insert table to PDF document programmatically using Aspose.PDF for C++ Library

How to adding Tables in PDF document Using C++ Library

In order to add table in PDF, we’ll use Aspose.PDF for C++ API which is a feature-rich, powerful and easy to use document manipulation API for cpp platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF.Cpp

Add Table to PDF via C++


You need Aspose.PDF for C++ to try the code in your environment.

  1. Load the PDF with an instance of Document.
  2. Access the Page via its index.
  3. Create Table object.
  4. Set table setting (e.g. set the borders).
  5. Populate table.
  6. Add the table to a page.
  7. Save the file.

Add Table in 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");