Ada beberapa kasus ketika ada kebutuhan untuk memanipulasi dokumen selain PDF sementara data penguraian tersedia dalam format PDF. Jadi, untuk aplikasi semacam itu, akan ada dua skenario: apakah mereka menambahkan fungsionalitas penguraian PDF dalam solusi mereka atau menambahkan fungsionalitas konversi PDF untuk memanipulasi data sebagai format yang didukung. Untuk skenario kedua untuk mengonversi PDF ke Word, Excel, HTML, Gambar atau format yang diperlukan, mengimplementasikan kode Pembaca dan konverter PHP untuk PDF dalam kode berbasis Java itu sederhana. Kami membahas beberapa kasus di sini sehingga programmer dapat memodifikasi cuplikan kode konversi ini sesuai dengan kebutuhan mereka.
Konversi PDF ke 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);
Aspose.PDF untuk perpustakaan PHP mendukung semua konversi PDF ke Word. Jika kita hanya mengonversi dokumen Microsoft Word tanpa pengaturan khusus, kita cukup memuat file PDF menggunakan metode Simpan dari kelas Dokumen dan akan menggunakan dengan output jalur dokumen Word dan SaveFormat sebagai parameter. Untuk kasus khusus di mana ada kebutuhan untuk meningkatkan jarak garis, resolusi gambar, dan pengaturan lainnya, API memiliki kelas DocSaveOptions yang memperlihatkan semua pengaturan tersebut.
Simpan PDF sebagai File 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);
Khusus SaveFormat.excel Pencacatan tersedia untuk menyimpan PDF ke format output Microsoft Excel XLS XLSX tertentu. Selain itu, PHP/Java PDF Library juga memiliki [kelas ExcelSaveOptions] khusus (https://apireference.aspose.com/pdf/php-java/aspose.pdf/excelsaveoptions) yang tidak hanya menangani penyimpanan ke format Excel tetapi juga menyediakan fungsi dan properti yang berbeda untuk mengatur atribut yang berbeda seperti format output yang tepat, meminimalkan jumlah lembar kerja dan banyak lagi.
Konversi PDF ke Presentasi PowerPoint
// 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 mendukung konversi halaman PDF ke Slide Presentasi PowerPoint dengan teks atau gambar yang dapat dipilih dengan merender slide sebagai gambar. Pola menyimpan Format Dokumen Portabel ke PowerPoint hampir sama, Memuat file menggunakan kelas Dokumen dan kemudian memanggil metode Save dengan jalur file output dan saveFormat sebagai parameter. Dalam hal rendering dengan opsi presentasi khusus, Pemrogram dapat menggunakan kelas PPTXSaveOptions dengan opsi rendering spesifik yang relevan. Memanggil metode simpan dan meneruskan opsi sebagai parameter.
PDF ke HTML Konversi
// 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 Parsing Library mendukung penyimpanan PDF ke HTML secara keseluruhan serta dengan sumber daya tertanam termasuk gambar. Prosedur konversi sama dengan PDF ke format lain untuk kasus umum, seperti memuat dokumen sumber dan memanggil metode Simpan dengan jalur file HTML keluaran dan SaveFormat.Html sebagai parameter. Dalam hal menyimpan dengan sumber daya tertanam, ada kelas HTMLSaveOptions yang memiliki beberapa opsi seperti menyimpan gambar ke folder tertentu selama konversi, membagi HTML yang dihasilkan menjadi beberapa halaman dan banyak lagi.
Konversi PDF ke Gambar
// 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);
}
Mengkonversi halaman PDF menjadi gambar termasuk PNG, JPEG, TIFF, BMP dll mudah dalam aplikasi berbasis Java menggunakan cuplikan kode yang tercantum di bawah ini. Pengembang dapat mengulang halaman PDF setelah memuat file dan mengonversi Halaman demi Halaman ke format gambar yang diperlukan. Pengembang dapat mengatur resolusi horizontal dan vertikal gambar menggunakan Kelas resolusi