转换 PS、EPS 和 XPS
适用于 Java 的 PS、EPS 和 XPS 转换器 API 解决方案
每当需要以编程方式转换 PostScript PS 和 Encapsulated PostScript EPS 文件以及 XPS 文档时,Java API 都可以顺利完成并转换多个文件。对于 PS 和 EPS,API 支持 Level 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 文件另存为图像,图像保存选项为字节数组的数组。输入文件的一页对应一个字节数组。
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);