翻譯|使用教程|編輯:李顯亮|2021-02-19 10:03:35.307|閱讀 289 次
概述:在某些情況下,必須將PDF文件轉(zhuǎn)換為其他文件格式。對于這種情況,本文介紹了如何將PDF文件轉(zhuǎn)換為流行的圖像格式。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
由于PDF格式具有跨平臺支持,因此它統(tǒng)治著數(shù)字文檔的世界。但是,在某些情況下,必須將PDF文件轉(zhuǎn)換為其他文件格式。對于這種情況,本文介紹了如何將PDF文件轉(zhuǎn)換為流行的圖像格式。特別是,將學(xué)習(xí)如何使用Java將PDF轉(zhuǎn)換為PNG,JPEG,BMP和TIFF格式。
在本文中,我們將使用Aspose.PDF for Java,該工具旨在創(chuàng)建新文件以及處理現(xiàn)有PDF文件。該API可將PDF文件高保真地轉(zhuǎn)換為各種文檔和圖像格式。感興趣的朋友可點(diǎn)擊下方按鈕下載最新版。
點(diǎn)擊下載最新版Aspose.PDF for Java
在PDF到PNG的轉(zhuǎn)換中,PDF文件的每一頁都轉(zhuǎn)換為單獨(dú)的PNG圖像。因此,可以根據(jù)情況轉(zhuǎn)換PDF的單個(gè)頁面,所有頁面或特定頁面。以下是使用Java將PDF文件轉(zhuǎn)換為PNG的步驟。
以下代碼示例顯示了如何使用Java將PDF轉(zhuǎn)換為PNG。
// Open document Document pdfDocument = new Document("input.pdf"); // Loop through all the pages of PDF file for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".png"); // Create Resolution object Resolution resolution = new Resolution(300); // Create PngDevice object with particular resolution PngDevice pngDevice = new PngDevice(resolution); // Convert a particular page and save the image to stream pngDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }
以下是使用Java將PDF文件中的頁面轉(zhuǎn)換為JPEG圖像的步驟。
以下代碼示例顯示了如何使用Java將PDF頁面轉(zhuǎn)換為JPEG圖像。
// Open document Document pdfDocument = new Document("input.pdf"); // Loop through all the pages of PDF file for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".jpg"); // Create Resolution object Resolution resolution = new Resolution(300); // Create JpegDevice object where second argument indicates the quality of resultant image JpegDevice jpegDevice = new JpegDevice(resolution, 100); // Convert a particular page and save the image to stream jpegDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }
以下是在Java中執(zhí)行PDF到BMP轉(zhuǎn)換的步驟。
以下代碼示例顯示了如何在Java中執(zhí)行PDF到BMP的轉(zhuǎn)換。
// Open document Document pdfDocument = new Document("input.pdf"); // Loop through all the pages of PDF file for (int pageCount = 1; pageCount <= pdfDocument.getPages().size(); pageCount++) { // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image" + pageCount + ".bmp"); // Create Resolution object Resolution resolution = new Resolution(300); // Create BmpDevice object with particular resolution BmpDevice bmpDevice = new BmpDevice(resolution); // Convert a particular page and save the image to stream bmpDevice.process(pdfDocument.getPages().get_Item(pageCount), imageStream); // Close the stream imageStream.close(); }
與上述光柵圖像格式相反,TIFF是多頁圖像格式。因此,可以一次將PDF文件轉(zhuǎn)換為TIFF,而無需循環(huán)瀏覽每個(gè)頁面。另一方面,您也可以指定要轉(zhuǎn)換為TIFF的PDF頁面范圍。以下是使用Java將PDF文件轉(zhuǎn)換為TIFF的步驟。
以下代碼示例顯示了如何使用Java將PDF文件轉(zhuǎn)換為TIFF圖像。
// Open document Document pdfDocument = new Document("input.pdf"); // Create stream object to save the output image java.io.OutputStream imageStream = new java.io.FileOutputStream("Converted_Image.tiff"); // Create Resolution object Resolution resolution = new Resolution(300); // instantiate TiffSettings object TiffSettings tiffSettings = new TiffSettings(); // set the compression of resultant TIFF image tiffSettings.setCompression(CompressionType.CCITT4); // set the color depth for resultant image tiffSettings.setDepth(ColorDepth.Format8bpp); // skip blank pages while rendering PDF to TIFF tiffSettings.setSkipBlankPages(true); // Create TiffDevice object with particular resolution TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings); // Convert a particular page (Page 1) and save the image to stream tiffDevice.process(pdfDocument, 1, 1, imageStream); // Close the stream imageStream.close();
如果你想試用Aspose的全部完整功能,可聯(lián)系在線客服獲取30天臨時(shí)授權(quán)體驗(yàn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn