PS、EPS、XPSの変換
Java用PS、EPS、XPSコンバーターAPIソリューション
PostScript PSおよびEncapsulated PostScript EPSファイル、ならびにXPSドキュメントをプログラムで変換する必要がある場合、Java APIはスムーズに複数のファイルを変換できます。PSおよびEPSの場合、APIはレベル1-3のPostScript演算子とほとんどのEPSヘッダーコメントをサポートし、数種類のフォントケースを除いて最大限の適合性を持ってPostScriptドキュメントを変換します。APIは、Times New Romanのようなフォントを処理します。
さらに、XPSファイルの変換では、APIはページの追加や削除、キャンバス、パス、グリフ要素の処理、ベクターグラフィックスの形状やテキスト文字列の作成、XPSアウトライン項目の変換などを行うことができます。
ここでのJava用APIソリューションを使用すると、PS、EPS、XPSなどのPDL形式のファイルをプログラムで変換できますが、これらのネイティブAPIで開発されたクロスプラットフォームツールを見て試してみるのも役立つでしょう。
JavaによるPostScriptからPDFへの変換
Java APIを介してPostScript PSおよびEncapsulated PostScript EPSファイルをPDFに変換するには、次の手順を実行する必要があります。
- PsDocumentクラス を使用してPSまたはEPSファイルをロードします。
- PdfSaveOptionsクラス を使用してPDF保存オプションを設定します。
- 出力PDFファイルには FileStreamクラス を使用します。
- FileOutputStreamオブジェクトをパラメータとして持つ PdfDeviceクラス を使用します。
- PsDocument.Save を呼び出して、ファイルをPDFに保存します。
PS EPSをPDFに変換するJavaコード
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");
// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;
//Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// Default page size is 595x842 and it is not mandatory to set it in PdfDevice
// But if you need to specify size and image format use following line
// PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new Dimension(595, 842));
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
// Save PS document to PDF file
document.saveAsPdf(dataDir + "PStoPDF.pdf", options);
//Review errors
if (suppressErrors) {
for (Exception ex : pdfOptions.getExceptions()) {
System.out.println(ex.getMessage());
}
}JavaによるPostScriptから画像への変換
EPS/PS PostScriptから画像へのコンバーターアプリケーションの場合、次のJavaコードが有効に機能するため、次の手順を実行します。
- PSソースファイルで入力ストリームを初期化します。
- 作成されたPS入力ストリームをパラメータとして PsDocument オブジェクトを作成します。
- ImageSaveOptions を使用して、AdditionalFontsFolderやSuppressErrorなどを指定します。
- 必要に応じて ImageDevice オブジェクトを使用して、画像の種類とサイズを指定します。
- PS/EPSファイルを画像として保存し、画像保存オプションをバイト配列の配列として指定します。入力ファイルの1ページに対して1つのバイト配列となります。
PostScriptから画像への変換用Javaコード
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");
// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;
// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// Default image format is PNG and it is not mandatory to set it in ImageSaveOptions.
// But if you need to specify another format use following line
// options.setImageFormat(ImageFormat.JPEG);
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
// But if you need to specify size and image format use constructor with parameters
// ImageSaveOptions options = new ImageSaveOptions(new Dimension(595, 842), ImageFormat.JPEG, suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
// options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
// Save PS document as images bytes arrays, one bytes array for one page of the document.
byte[][] imagesBytes = document.saveAsImage(options);
int i = 0;
for (byte [] imageBytes : imagesBytes) {
String imagePath = dataDir + "PSToImage" + i + "." + options.getImageFormat().toString().toLowerCase();
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());
}
}JavaによるXPSから画像(JPG、PNG、BMP)への変換
Java APIは、ページレイアウトの表現に使用されるXPS形式を処理します。どのようなシナリオにおいても、プログラムでXPSを画像(BMP、JPG、PNG、TIFF)に変換する必要がある場合は、次のコードをJavaアプリケーション内に簡単に統合できます。
- XpsDocumentクラス を使用してXPSドキュメントをロードします。
- 画像の追加設定には、 PngSaveOptions 、 JpegSaveOptions 、 BmpSaveOptions 、 TiffSaveOptions などの関連する画像オプションクラスを使用します。
- image device クラスのインスタンスを作成します。
- XpsDocument.save を呼び出して、変換されたJPEG画像をImageDeviceオブジェクトに保存し、ImageDeviceを使用して画像をJPGとして保存します。
XPSから画像への変換用Javaコード
// The path to the documents directory.
String pathDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PngSaveOptions options = new com.aspose.xps.rendering.PngSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document as images bytes array. One bytes array for one page of every parttion in the document
byte [][][] imagesBytes = document.saveAsImage(options);
// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.length; i++) {
// Iterate through partition pages
for (int j = 0; j < imagesBytes[i].length; j++) {
// Initialize image output stream
FileOutputStream imageStream = new FileOutputStream(dataDir + "XPStoPNG" + "_" + (i + 1) + "_" + (j + 1) + ".png");
// Write image
imageStream.write(imagesBytes[i][j], 0, imagesBytes[i][j].length);
imageStream.close();
}
}FAQ
1. この API ソリューションで Postscript を変換できますか?
Aspose.Page には、PS、XPS、および EPS ファイルをオンラインまたはプログラムで他の形式に変換できる機能があります。ファイルをオンラインで即座に変換する必要がある場合は、 ページ記述言語形式のファイル コンバーター クロスプラットフォーム アプリケーションを使用できます。
2. コンバーターでサポートされているページ記述言語は何ですか?
この変換機能は、拡張子が .ps、.eps、および .xps のファイルをサポートします。 PDF や SVG などの有名な PDL は、Aspose.products では個別のソリューションとして表されます。
3. 機能は無料ですか?
クロスプラットフォーム コンバーター は無料です。API ソリューションの場合、無料の試用版を取得してから、必要に応じて製品を購入できます。
JavaによるXPSからPDFへの変換
プログラムでXPSをPDFドキュメントに変換するプロセスはシンプルで、入力ファイルと出力ファイルの間で忠実度の高い結果が得られます。
- XpsDocumentクラスを使用してファイルをロードします。 PdfSaveOptionsクラス オブジェクトを初期化します。
- レンダリング用のPdfDeviceオブジェクトを作成し、最後に構成されたPDFドキュメントを保存します。
XPSからPDFへの変換用Javaコード
// Load input XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters
PdfSaveOptions options = new PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(PdfImageCompression.Jpeg);
options.setTextCompression(PdfTextCompression.Flate);
// Save XPS document to output PDF file
document.saveAsPdf(dataDir + "XPStoPDF.pdf", options);