
讓我們繼續探索 Aspose.Page for Java 提供的功能。因為我們已經知道需要一個第三方應用程序來查看/處理 PostScript 文件。然而,將它們轉換為圖像文件格式肯定會消除這種麻煩。因此,這篇博客文章將逐步介紹如何使用 Aspose.Page for Java 將 PS 轉換為 PNG。這涉及幾行 Java 源代碼來以編程方式開發 PS 到 PNG 的轉換器。現在,讓我們跳到安裝部分,然後編寫一個代碼示例來將 PostScript 轉換為 PNG。
本文涉及以下要點:
Java PostScript SDK 安裝
所以,你可以 下載 這個 JAR 文件或使用下面提到的 Maven 配置。此外,這裡提供了完整的安裝和使用說明 這裡。
<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://releases.aspose.com/java/repo/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-page</artifactId>
<version>23.12</version>
</dependency>
</dependencies>
在 Java 中將 PostScript 轉換為 PNG
好吧,夠理論了,讓我們看看一些代碼。但在繼續之前,請確保你有一個源 PostScript 文件來測試功能。
以下步驟顯示如何以編程方式將 PS 轉換為 PNG:
- 首先,將 ImageFormat 設置為 PNG,並加載源 PostScript (PS) 文件。
- 使用 PostScript 的輸入流創建 PsDocument 類的實例。
- 如果你想在存在輕微錯誤的情況下轉換 Postscript 文件,可以設置 suppressErrors 的值。
- 使用 suppressErrors 參數初始化 ImageSaveOptions 類的新實例。
- 實例化 ImageDevice 類的對象。
- 調用 save 方法將 PNG 文件保存到磁碟。
- 調用 getImagesBytes 方法以獲取結果圖像的字節。
- 現在,通過使用輸出圖像路徑初始化 FileOutputStream 類的實例來創建輸出流。
public class Main { | |
// Convert PostScript to PNG in Java - PS to PNG Converter | |
public static void main(String[] args) throws Exception { | |
// Create an instance of the PsDocument class from the PostScript file. | |
PsDocument document = new PsDocument("input.ps"); | |
// If you want to convert Postscript file despite of minor errors set this flag | |
boolean suppressErrors = true; | |
// Initialize a new instance of ImageSaveOptions class with suppressErrors parameter. | |
ImageSaveOptions options = new ImageSaveOptions(suppressErrors, ImageFormat.PNG); | |
// Save PS as PNG images bytes array, one byte array for one page of input document. | |
byte[][] imagesBytes = document.saveAsImage(options, options); | |
int i = 0; | |
for (byte [] imageBytes : imagesBytes) { | |
String imagePath = "PSToImage" + i + "." + imageFormat.toString().toLowerCase(); | |
// Create output stream by initializing an instance of the FileOutputStream class with the output image path. | |
FileOutputStream fs = new FileOutputStream(imagePath); | |
try { | |
fs.write(imageBytes, 0, imageBytes.length); | |
} catch (IOException ex) { | |
System.out.println(ex.getMessage()); | |
} finally { | |
fs.close(); | |
} | |
i++; | |
} | |
//Review errors | |
if (suppressErrors) { | |
for (Exception ex : options.getExceptions()) { | |
System.out.println(ex.getMessage()); | |
} | |
} | |
} | |
} |
輸出:

PS 到 PNG 轉換器 - 在線應用
到目前為止,你已經看到這個 Java PostScript SDK 如何提供編程轉換。現在,這個庫的另一個酷功能是,它還提供了一個 在線 工具來將 PostScript 文件轉換為圖像文件格式。最重要的是,這個 PS 到 PNG 轉換器是免費的,你可以通過在任何網頁瀏覽器中打開它來使用。

獲取免費授權
你可以獲得免費的 臨時授權,以便在沒有評估限制的情況下試用這個 Java PostScript SDK。
總結
我們在這裡結束這個指南。為了開始,請訪問 文檔、API 參考和 GitHub 倉庫。我們已經介紹了如何使用 Aspose.Page for Java 將 PostScript 轉換為 PNG。此外,這篇博客文章旨在介紹這個 Java PostScript SDK 作為解決方案,我們希望它能幫助你為你的業務應用程序開發 PS 到 PNG 的轉換器。最後,請與 aspose.com 保持聯繫,以獲取最新消息。
隨時與我們聯繫
你可以在我們的 論壇 上告訴我們你的問題或查詢。