通过 PHP 将表格添加到 PDF

使用适用于 PHP via Java 库的 Aspose.PDF 以编程方式将表格插入到 PDF 文档

如何使用 PHP via Java 库在 PDF 文档中添加表格

为了在 PDF 中添加表格,我们将通过 Java API 使用适用于 PHP 的 Aspose.PDF,它是 php-java 中功能丰富、功能强大且易于使用的文档处理工具。在任何地方安装 Tomcat 9.0 版本,添加 aspose.pdf.war,更多细节请查看 GitHub 页面。

通过 PHP 将表格添加到 PDF


你需要 通过 Java 实现 PHP 的 Aspose.PDF 才能在你的环境中试用代码。

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

在 PDF 中添加表格-PHP

// Open document
$document = new Document($inputFile);        
// Initializes a new instance of the Table
$table = new Table();
$colors= new Color();
// Set the table border color as LightGray
$borderSide = new BorderSide();
$borderInfo = new BorderInfo($borderSide->All, 0.5, $colors->getLightGray());
$table->setBorder($borderInfo);
// set the border for table cells
$table->setDefaultCellBorder($borderInfo);
// create a loop to add 10 rows
for ($row_count = 1; $row_count < 10; $row_count++) {
    // add row to table
    $row = $table->getRows()->add();
    // add table cells
    $row->getCells()->add("Column (" . $row_count . ", 1)");
    $row->getCells()->add("Column (" . $row_count . ", 2)");
    $row->getCells()->add("Column (" . $row_count . ", 3)");
}
// Add table object to first page of input document
$document->getPages()->add()->getParagraphs()->add($table);

// Save resulting PDF document.    
$document->save($outputFile);
$document->close();