موارد کمی وجود دارد که نیاز به دستکاری اسناد غیر از PDF در حالی است که داده های تجزیه در قالب های PDF در دسترس باشد. بنابراین، برای چنین برنامه هایی، دو سناریو وجود خواهد داشت: یا آنها عملکرد تجزیه PDF را در راه حل خود اضافه می کنند یا قابلیت تبدیل PDF را برای دستکاری داده ها به عنوان فرمت های پشتیبانی شده اضافه می کنند. برای سناریوی دوم برای تبدیل PDF به Word، اکسل، HTML، تصاویر یا هر قالب مورد نیاز، پیادهسازی کد PHP Reader و مبدل برای PDF در کد مبتنی بر جاوا ساده است. ما در اینجا چند مورد بحث می کنیم تا برنامه نویسان بتوانند این قطعات کد تبدیل را با توجه به نیاز خود تغییر دهند.
تبدیل 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 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 برای کتابخانه PHP از تمام تبدیل PDF به Word پشتیبانی می کند. در صورتی که ما فقط اسناد مایکروسافت ورد را بدون هیچ تنظیمات خاصی تبدیل می کنیم، فقط فایل PDF را با استفاده از روش Save از کلاس Document بارگذاری می کنیم و با مسیر سند Word خروجی و SaveFormat به عنوان پارامتر استفاده خواهیم کرد. برای موارد خاصی که نیاز به افزایش فاصله خطوط، وضوح تصویر و تنظیمات بیشتر وجود دارد، API دارای کلاس DocSaveOptions است که تمام این تنظیمات را نشان می دهد.
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 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] تخصصی (https://apireference.aspose.com/pdf/php-java/aspose.pdf/saveformat) شمارش برای ذخیره PDF در فرمت های خروجی خاص مایکروسافت اکسل XLS XLSX موجود است. علاوه بر این، کتابخانه PHP/Java PDF همچنین دارای یک [کلاس ExcelSaveOptions] خاص است (https://apireference.aspose.com/pdf/php-java/aspose.pdf/excelsaveoptions) که نه تنها صرفه جویی در قالب های اکسل را انجام می دهد بلکه توابع و خواص مختلفی را برای تنظیم ویژگی های مختلف مانند فرمت دقیق خروجی، به حداقل رساندن تعداد برگه ها و موارد دیگر فراهم می کند.
تبدیل 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 به اسلایدهای ارائه پاورپوینت با متن یا تصاویر قابل انتخاب با رندر اسلاید به عنوان تصاویر پشتیبانی می کند. الگوی ذخیره فرمت سند قابل حمل در پاورپوینت تقریباً یکسان است، بارگذاری فایل با استفاده از کلاس Document و سپس فراخوانی روش Save با مسیر فایل خروجی و SaveFormat به عنوان پارامترها. در صورت رندر با گزینه های ارائه ویژه، برنامه نویسان می توانند از کلاس PPTXSaveOptions با هر گزینه رندر خاص مربوطه استفاده کنند. فراخوانی روش ذخیره و انتقال گزینه ها به عنوان پارامتر.
تبدیل 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 به فرمت های دیگر برای موارد عمومی است، مانند بارگذاری سند منبع و فراخوانی روش Save با مسیر فایل HTML خروجی و SaveFormat.Html به عنوان پارامترها. در صورت ذخیره با منابع جاسازی شده، یک کلاس 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);
}
تبدیل صفحات PDF به تصاویر از جمله PNG، JPEG، TIFF، BMP و غیره در برنامه های مبتنی بر جاوا با استفاده از قطعات کد ذکر شده در زیر آسان است. توسعه دهندگان می توانند پس از بارگذاری فایل، صفحات PDF را حلقه دهند و Page by Page را به فرمت تصویر مورد نیاز تبدیل کنند. توسعه دهندگان می توانند رزولوشن افقی و عمودی تصاویر را با استفاده از [رزولوشن کلاس] تنظیم کنند (https://apireference.aspose.com/pdf/php-java/aspose.pdf.devices/resolution)