PNG
JPG
BMP
TIFF
PDF
如何通过 Cloud PHP SDK 向 PDF 添加链接
要在 PDF 中添加链接,我们将使用 Aspose.PDF Cloud PHP SDK 这个 Cloud SDK 帮助 PHP 程序员通过 Aspose.PDF REST API 开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个帐户并获取您的应用程序信息。一旦您拥有 App SID 和密钥,就可以使用 Aspose.PDF Cloud PHP SDK。
包管理器控制台命令
composer install
通过 Cloud 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() {
$pdfFilePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['PDF_DOCUMENT_NAME'];
$pdfFileData = file_get_contents($pdfFilePath);
$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";
}
public function appendLinkOnPage() {
$linkColor = new Color(['a' => 255, 'r' => 0, 'g' => 255, 'b' => 0]);
$linkRectangle = new Rectangle();
$linkRectangle->setLLX($this->configParams['LINK_POS_LLX']);
$linkRectangle->setLLY($this->configParams['LINK_POS_LLY']);
$linkRectangle->setURX($this->configParams['LINK_POS_URX']);
$linkRectangle->setURY($this->configParams['LINK_POS_URY']);
$linkItem = new Link(['rel' => "self"]);
$newLink = new LinkAnnotation();
$newLink->setLinks([$linkItem]);
$newLink->setActionType(LinkActionType::GO_TO_URI_ACTION);
$newLink->setAction($this->configParams['NEW_LINK_ACTION']);
$newLink->setHighlighting(LinkHighlightingMode::INVERT);
$newLink->setColor($linkColor);
$newLink->setRect($linkRectangle);
$addResponse = $this->pdfApi->postPageLinkAnnotations($this->configParams['PDF_DOCUMENT_NAME'], $this->configParams['PAGE_NUMBER'], [$newLink]);
if ($addResponse->getCode() == 200) {
echo "Append link successful!\n";
return true;
}
}
function main() {
global $configParams;
try {
$pdfLinks = new PdfLinks($configParams);
$pdfLinks->uploadDocument();
$pdfLinks->appendLinkOnPage();
$pdfLinks->downloadResult();
} catch (\Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
}
在 PDF 中处理链接
向 PDF 添加链接可以增强可用性、交互性和可访问性。无论是用于导航、营销还是交叉引用,超链接都可以提高文档的有效性,使用户更容易找到相关内容并采取行动。 使用 Aspose.PDF Cloud PHP SDK 将链接添加到 PDF 文档中。
使用我们的 PHP 库,您可以:
- 以文本或图像格式添加 PDF 文档的页眉和页脚。
- 向 PDF 文档添加表格和印章(文本或图像)。
- 将多个 PDF 文档附加到现有文件。
- 处理 PDF 附件、注释和表单字段。
- 对 PDF 文档进行加密或解密并设置密码。
- 从页面或整个 PDF 文档中删除所有印章和表格。
- 按 ID 从 PDF 文档中删除特定的印章或表格。
- 替换 PDF 页面或整个文档中的单个或多个文本实例。
- 对 PDF 文档进行广泛的支持以转换为各种其他文件格式。
- 提取 PDF 文件的各种元素并优化 PDF 文档。
- 您可以试用我们的免费应用在线测试功能。