翻譯|使用教程|編輯:李顯亮|2021-02-19 10:03:35.307|閱讀 289 次
概述:在某些情況下,必須將PDF文件轉換為其他文件格式。對于這種情況,本文介紹了如何將PDF文件轉換為流行的圖像格式。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
由于PDF格式具有跨平臺支持,因此它統治著數字文檔的世界。但是,在某些情況下,必須將PDF文件轉換為其他文件格式。對于這種情況,本文介紹了如何將PDF文件轉換為流行的圖像格式。特別是,將學習如何使用Java將PDF轉換為PNG,JPEG,BMP和TIFF格式。
在本文中,我們將使用Aspose.PDF for Java,該工具旨在創建新文件以及處理現有PDF文件。該API可將PDF文件高保真地轉換為各種文檔和圖像格式。感興趣的朋友可點擊下方按鈕下載最新版。
在PDF到PNG的轉換中,PDF文件的每一頁都轉換為單獨的PNG圖像。因此,可以根據情況轉換PDF的單個頁面,所有頁面或特定頁面。以下是使用Java將PDF文件轉換為PNG的步驟。
以下代碼示例顯示了如何使用Java將PDF轉換為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文件中的頁面轉換為JPEG圖像的步驟。
以下代碼示例顯示了如何使用Java將PDF頁面轉換為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中執行PDF到BMP轉換的步驟。
以下代碼示例顯示了如何在Java中執行PDF到BMP的轉換。
// 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文件轉換為TIFF,而無需循環瀏覽每個頁面。另一方面,您也可以指定要轉換為TIFF的PDF頁面范圍。以下是使用Java將PDF文件轉換為TIFF的步驟。
以下代碼示例顯示了如何使用Java將PDF文件轉換為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的全部完整功能,可聯系在線客服獲取30天臨時授權體驗。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn