在少数情况下,需要操作 PDF 以外的文档,同时以 PDF 格式提供解析数据。因此,对于此类应用程序,将有两种情况:要么在其解决方案中添加 PDF 解析功能,要么添加 PDF 转换功能以将数据作为支持的格式进行处理。对于**将 PDF 转换为 Word 、Excel、HTML、图像或任何所需格式的第二种场景,在基于 Java 的代码中实现 PDF 代码的 PHP 阅读器和转换器非常简单。我们在这里讨论一些案例,以便程序员可以根据他们的要求修改这些转换代码片段。
PDF 到微软 Word 的转换
// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");
// Import the necessary classes from the Aspose.PDF for Java library
use com\aspose\pdf\License;
use com\aspose\pdf\Document;
use com\aspose\pdf\DocSaveOptions;
use com\aspose\pdf\DocSaveOptions_DocFormat;
use com\aspose\pdf\DocSaveOptions_RecognitionMode;
// Set the license file for Aspose.PDF for Java
$license = "Aspose.PDF.PHPviaJava.lic";
$licenceObject = new License();
$licenceObject->setLicense($license);
// Set the input and output file paths
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'result-pdf-to-docx.docx';
// Load the PDF document
$document = new Document($inputFile);
// Create the save options for converting to DOCX format
$saveOption = new DocSaveOptions();
$saveOption->setMode(DocSaveOptions_RecognitionMode::$EnhancedFlow);
$saveOption->setFormat(DocSaveOptions_DocFormat::$DocX);
// Save the document in DOCX format
$document->save($outputFile, $saveOption);
Aspose.PDF for PHP 库支持所有 PDF 到 Word 的转换。如果我们只是在没有任何特殊设置的情况下转换微软的Word文档,则只需使用文档类中的保存方法加载PDF文件,然后使用输出Word文档路径和SaveFormat作为参数即可。对于需要增强线距、图像分辨率和更多设置的特殊情况,API 提供了 docSaveOptions 类来公开所有这些设置。
将 PDF 另存为 Excel 文件
// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");
// Import the necessary classes from the Aspose.PDF for Java library
use com\aspose\pdf\Document;
use com\aspose\pdf\ExcelSaveOptions;
use com\aspose\pdf\ExcelSaveOptions_ExcelFormat;
use com\aspose\pdf\License;
// Set the path to the Aspose.PDF license file
$license = "Aspose.PDF.PHPviaJava.lic";
// Create a new License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense($license);
// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
// Set the path to the output Excel file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'sample.xlsx';
// Create a new Document object and load the input PDF file
$document = new Document($inputFile);
// Create a new ExcelSaveOptions object
$saveOption = new ExcelSaveOptions();
// Set the output format to XLSX
$saveOption->setFormat(ExcelSaveOptions_ExcelFormat::$XLSX);
// Save the document as an Excel file using the specified save options
$document->save($outputFile, $saveOption);
专用 SaveFormat.Excel 枚举可用于将 PDF 保存为特定的微软 Excel XLS XLS XLSX 输出格式。此外,PHP/Java PDF 库还有一个特殊的 ExcelSaveOptions 类,它不仅可以保存为 Excel 格式,还提供不同的功能和属性来设置不同的属性,例如精确的输出格式、最小化工作表数量等。
将 PDF 转换为幻灯片演示文稿
// Include the required Java and Aspose.PDF for PHP libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");
// Import the necessary classes from the Aspose.PDF for PHP library
use com\aspose\pdf\Document;
use com\aspose\pdf\PptxSaveOptions;
use com\aspose\pdf\License;
// Set the path to the Aspose.PDF license file
$license = "Aspose.PDF.PHPviaJava.lic";
// Create a new License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense($license);
// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
// Set the path to the output PPTX file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . "results" . DIRECTORY_SEPARATOR . 'sample.pptx';
// Load the input PDF document
$document = new Document($inputFile);
// Create an instance of PptxSaveOptions
$saveOption = new PptxSaveOptions();
// Save the PDF document as a PPTX file
$document->save($outputFile, $saveOption);
PHP API 支持通过将幻灯片呈现为图像,将 PDF 页面转换为带有可选文本或图像的 PowerPoint 演示幻灯片。将便携式文档格式保存到 PowerPoint 的模式几乎相同,使用文档类加载文件,然后使用输出文件路径和 SaveFormat 作为参数调用保存方法。如果使用特殊的演示选项进行渲染,程序员可以将 pptxSaveOptions 类 与任何相关的特定渲染选项一起使用。调用 save 方法并将选项作为参数传递。
PDF 到 HTML 的转换
// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");
// Import the necessary classes from the Aspose.PDF library
use com\aspose\pdf\Document;
use com\aspose\pdf\HtmlSaveOptions;
use com\aspose\pdf\License;
// Set the path to the license file
$licensePath = "Aspose.PDF.PHPviaJava.lic";
// Create a new License object and set the license using the provided file path
$license = new License();
$license->setLicense($licensePath);
// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
// Set the path to the output HTML file
$outputFile = $dataDir . DIRECTORY_SEPARATOR . 'pdf-to-html.html';
// Create a new Document object and load the input PDF file
$document = new Document($inputFile);
// Create a new HtmlSaveOptions object for saving the document as HTML
$saveOption = new HtmlSaveOptions();
// Save the document as HTML using the specified save options
$document->save($outputFile, $saveOption);
PDF 解析库支持将 PDF 作为整个 HTML 以及包括图像在内的嵌入式资源保存。对于泛型案例,转换过程与 PDF 转换为其他格式相同,例如加载源文档和使用输出 HTML 文件路径和 SaveFormat.Html 作为参数调用 Save 方法。如果使用嵌入式资源进行保存,则有一个 HtmlSaveOptions 类 具有多个选项,例如在转换期间将图像保存到特定文件夹,将生成的HTML拆分为多个页面等等。
将 PDF 转换为图像
// Include the required libraries
require_once ("java/Java.inc");
require_once ("lib/aspose.pdf.php");
// Import the necessary classes from the Aspose.PDF for PHP via Java library
use com\aspose\pdf\Document;
use com\aspose\pdf\devices_Resolution;
use com\aspose\pdf\devices_JpegDevice;
use com\aspose\pdf\License;
// Create a License object and set the license file
$licenceObject = new License();
$licenceObject->setLicense("Aspose.PDF.PHPviaJava.lic");
// Set the path to the input PDF file
$dataDir = getcwd() . DIRECTORY_SEPARATOR . "samples";
$inputFile = $dataDir . DIRECTORY_SEPARATOR . "sample.pdf";
// Set the path and template for the output JPEG files
$imageFileNameTemplate = $dataDir . DIRECTORY_SEPARATOR . 'pdf-to-jpeg-';
// Open the target document
$document = new Document($inputFile);
$pages = $document->getPages();
$count = $pages->size();
// Create a Resolution object with a resolution of 300 dpi
$resolution = new devices_Resolution(300);
// Create a JpegDevice object with the specified resolution
$imageDevice = new devices_JpegDevice($resolution);
// Loop through each page of the document
for ($pageCount = 1; $pageCount <= $document->getPages()->size(); $pageCount++) {
// Convert a particular page and save the image to a file
$imageFileName = $imageFileNameTemplate . $pageCount . '.jpg';
$page = $document->getPages()->get_Item($pageCount);
$imageDevice->process($page, $imageFileName);
}
在基于Java的应用程序中,使用下面列出的代码片段可以轻松地将PDF页面转换为包括PNG、JPEG、TIFF、BMP等在内的图像。开发人员可以在加载文件后循环浏览 PDF 页面,并将逐页转换为所需的图像格式。开发人员可以使用 分辨率等级 设置图像的水平和垂直分辨率