In wenigen Fällen müssen andere Dokumente als PDF bearbeitet werden, während die Analysedaten in PDF-Formaten verfügbar sind. Für solche Anwendungen gibt es also zwei Szenarien: Entweder fügen sie ihrer Lösung die Funktionalität der PDF-Analyse hinzu oder sie fügen die PDF-Konvertierungsfunktion hinzu, um Daten als unterstützte Formate zu bearbeiten. Für das zweite Szenario zur Konvertierung von PDF in Word, Excel, HTML, Bilder oder ein beliebiges erforderliches Format ist die Implementierung des PHP-Readers und -Konverters für PDF-Code in Java-basiertem Code einfach. Wir besprechen hier einige Fälle, damit Programmierer diese Konvertierungscodeschnipsel an ihre Anforderungen anpassen können.
Konvertierung von PDF in Microsoft 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);
Die Bibliothek Aspose.PDF for PHP unterstützt alle Konvertierungen von PDF in Word. Falls wir nur Microsoft Word-Dokumente ohne spezielle Einstellungen konvertieren, laden wir die PDF-Datei einfach mit der Save-Methode aus der Document-Klasse und verwenden den Word-Dokumentpfad und SaveFormat als Parameter für die Ausgabe. Für die Sonderfälle, in denen der Zeilenabstand, die Bildauflösung und weitere Einstellungen verbessert werden müssen, verfügt die API über die DocSaveOptions-Klasse, die all diese Einstellungen verfügbar macht.
PDF als Excel-Dateien speichern
// 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);
Spezialisierte SaveFormat.excel Enumeration verfügbar, um PDF in bestimmten Microsoft Excel XLS XLSX-Ausgabeformaten zu speichern. Darüber hinaus verfügtPHP/Java PDF Library auch über eine spezielle ExcelSaveOptions-Klasse, die sich nicht nur mit dem Speichern in Excel-Formaten befasst, sondern auch verschiedene Funktionen und Eigenschaften zum Festlegen verschiedener Attribute wie exaktes Ausgabeformat, Minimierung der Anzahl von Arbeitsblättern und mehr bietet.
PDF in PowerPoint-Präsentationen konvertieren
// 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);
Die PHP-API unterstützt die Konvertierung von PDF-Seiten in PowerPoint-Präsentationsfolien mit auswählbarem Text oder Bildern, indem Folien als Bilder gerendert werden. Das Muster beim Speichern des portablen Dokumentformats in PowerPoint ist fast dasselbe. Die Datei wird mithilfe der Document-Klasse geladen und dann die Save-Methode mit dem Ausgabedateipfad und SaveFormat als Parametern aufgerufen. Beim Rendern mit speziellen Präsentationsoptionen können Programmierer die PPTXSaveOptions-Klasse mit allen relevanten spezifischen Renderoptionen verwenden. Aufrufen der Speichermethode und Übergabe der Optionen als Parameter.
Konvertierung von PDF zu 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);
Die PDF Parsing Library unterstützt das Speichern von PDF-Dateien als Ganzes sowie mit eingebetteten Ressourcen, einschließlich Bildern. Das Verfahren zur Konvertierung in andere Formate ist dasselbe wie bei der Konvertierung von PDF in andere Formate, z. B. beim Laden des Quelldokuments und dem Aufrufen der Save-Methode mit dem Pfad der Ausgabe-HTML-Datei und SaveFormat.Html als Parameter. Beim Speichern mit eingebetteten Ressourcen gibt es eine HtmlSaveOptions-Klasse mit mehreren Optionen, z. B. das Speichern von Bildern in einem bestimmten Ordner während der Konvertierung, das Aufteilen des resultierenden HTML-Codes in mehrere Seiten und mehr.
PDF in Bilder umwandeln
// 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);
}
Das Konvertieren von PDF-Seiten in Bilder wie PNG, JPEG, TIFF, BMP usw. ist in Java-basierten Anwendungen mithilfe der unten aufgeführten Codefragmente einfach. Entwickler können nach dem Laden der Datei PDF-Seiten durchgehen und Seite für Seite in das erforderliche Bildformat konvertieren. Entwickler können die horizontale und vertikale Auflösung von Bildern mithilfe von Auflösungsklasse festlegen