PNG
JPG
BMP
TIFF
PDF
如何通过云 PHP SDK 替换 PDF 中的表格
要在 PDF 中替换表格,我们将使用 Aspose.PDF Cloud PHP SDK 这个云 SDK 帮助 PHP 程序员使用 PHP 编程语言通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个帐户并获取您的应用程序信息。 一旦您拥有 App SID 和密钥,就可以开始使用 Aspose.PDF Cloud PHP SDK。
包管理器控制台命令
composer install
通过云 PHP 替换 PDF 中表格的步骤
Aspose.PDF Cloud 开发人员可以轻松地在几行代码中加载和替换 PDF 中的表格。
- 上传一个 PDF 文档
- 下载修改后的 PDF 文档
- 初始化一个表格
- 替换 PDF 中的现有表格
使用 PHP 替换 PDF 中的表格
private function _create_rest_api() {
$credentials = json_decode(file_get_contents("../../../../Credentials/credentials.json"), true);
$configAuth = new Configuration();
$configAuth->setAppKey($credentials['key']);
$configAuth->setAppSid($credentials['id']);
$this->pdfApi = new PdfApi(null, $configAuth);
}
public function __construct($config) {
$this->configParams = $config;
$this->_create_rest_api();
}
public function uploadDocument() {
$fileNamePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['PDF_DOCUMENT_NAME'];
$pdfFileData = file_get_contents($fileNamePath);
$this->pdfApi->uploadFile($this->configParams['PDF_DOCUMENT_NAME'], $pdfFileData);
}
public function downloadResult() {
$changedPdfData = $this->pdfApi->downloadFile($this->configParams['PDF_DOCUMENT_NAME']);
$filePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['LOCAL_RESULT_DOCUMENT_NAME'];
file_put_contents($filePath, $changedPdfData);
echo "Downloaded: " . $filePath . "\n";
}
private function _init_table() {
$numOfCols = 5;
$numOfRows = 5;
$headerTextState = [
"font" => "Arial Bold",
"fontSize" => 11,
"foregroundColor" => ["a" => 0xFF, "r" => 0xFF, "g" => 0xFF, "b" => 0xFF],
"fontStyle" => "Bold"
];
$commonTextState = [
"font" => "Arial Bold",
"fontSize" => 11,
"foregroundColor" => ["a" => 0xFF, "r" => 0x70, "g" => 0x70, "b" => 0x70]
];
$table = new \Aspose\PDF\Model\Table();
$table->setRows([]);
$table->setColumnWidths(str_repeat(" 70", $numOfCols));
$borderTableBorder = new \Aspose\PDF\Model\GraphInfo();
$borderTableBorder->setColor(["a" => 0xFF, "r" => 0x00, "g" => 0xFF, "b" => 0x00]);
$borderTableBorder->setLineWidth(0.5);
$table->setDefaultCellBorder([
"top" => $borderTableBorder,
"right" => $borderTableBorder,
"bottom" => $borderTableBorder,
"left" => $borderTableBorder,
"roundedBorderRadius" => 0
]);
$table->setLeft(150);
$table->setTop(250);
for ($rowIndex = 0; $rowIndex < $numOfRows; $rowIndex++) {
$row = new \Aspose\PDF\Model\Row();
$row->setCells([]);
for ($colIndex = 0; $colIndex < $numOfCols; $colIndex++) {
$cell = new \Aspose\PDF\Model\Cell();
$cell->setDefaultCellTextState($commonTextState);
if ($rowIndex == 0) {
$cell->setBackgroundColor(["a" => 0xFF, "r" => 0x80, "g" => 0x80, "b" => 0x80]);
$cell->setDefaultCellTextState($headerTextState);
} else {
$cell->setBackgroundColor(["a" => 0xFF, "r" => 0xFF, "g" => 0xFF, "b" => 0xFF]);
}
$textRect = new \Aspose\PDF\Model\TextRect();
$textRect->setText($rowIndex == 0 ? "header #" . $colIndex : "value #(" . $rowIndex . "," . $colIndex . ")");
$cell->setParagraphs([$textRect]);
$row->getCells()[] = $cell;
}
$table->getRows()[] = $row;
}
return $table;
}
public function replaceTable() {
$newTable = $this->_init_table();
$resultTabs = $this->pdfApi->putTable($this->configParams['PDF_DOCUMENT_NAME'], $this->configParams['TABLE_ID'], [$newTable]);
if ($resultTabs->getCode() == 200) {
echo "New table successfully replaced.\n";
var_dump($newTable);
} else {
echo "Unexpected error: can't replace table !!!\n";
}
}
function main() {
global $configParams;
$pdfTables = new PdfTables($configParams);
try {
$pdfTables->uploadDocument();
$pdfTables->replaceTable();
$pdfTables->downloadResult();
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
在 PDF 中处理表格
表格为系统地呈现数据提供了结构化格式,使读者更容易理解和分析信息。它们还增强了文档的视觉吸引力,增加了专业性和组织性。在处理数字或比较数据时,表格通过将相关信息分组为易于阅读的格式来提高清晰度。此外,表格可以结合实时或动态生成的内容,例如来自数据库或分析仪表板的数据。 使用 Aspose.PDF Cloud PHP SDK 替换 PDF 文档中的表格。
使用我们的 PHP 库,您可以:
- 添加 PDF 文档的文本或图像格式的页眉和页脚。
- 向 PDF 文档添加表格和印章(文本或图像)。
- 将多个 PDF 文档附加到现有文件。
- 处理 PDF 附件、注释和表单字段。
- 对 PDF 文档应用加密或解密并设置密码。
- 从页面或整个 PDF 文档中删除所有印章和表格。
- 按 ID 删除 PDF 文档中特定的印章或表格。
- 替换 PDF 页面或整个文档中的单个或多个文本实例。
- 广泛支持将 PDF 文档转换为各种其他文件格式。
- 提取 PDF 文件的各个元素并优化 PDF 文档。
- 您可以尝试我们的免费应用在线测试功能。