转换 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 对象并设置所需的转换选项。
- 使用输出文件路径和选项对象调用 PsDocument.saveAsPdf()。
将 PS EPS 转换为 PDF 的 Java 代码
// Convert PS file to PDF document.
// Learn more: https://docs.aspose.com/page/java/convert-ps/
// Initialize PsDocument with the name of PostScript file.
PsDocument document = new PsDocument(getDataDir() + "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);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.setAdditionalFontsFolders(new String[] { "{FONT_FOLDER}" });
// Default page size is 595x842 and it is not mandatory to set it in PdfSaveOptions
// But if you need to specify sizeuse following line
//PdfSaveOptions options = new PdfSaveOptions(suppressErrorsnew, java.awt.Dimension(595x842));
// or
//saveOptions.setSize(new java.awt.Dimension(595x842));
//If PostScript file contains some graphics written as Type3 font glyphs, but it is not actual glyphs
//the resulting PDF may be incorrect because of default value of options.ConvertFontsToTTF = true
//Try to set this
//options.setConvertFontsToTTF(false);
// Save document as PDF
document.saveAsPdf(getOutputDir() + "outputPDF_out.pdf", options);
//Review errors
if (suppressErrors) {
for (Exception ex : options.getExceptions()) {
System.out.println(ex.getMessage());
}
}通过 Java 将 PostScript 转换为图像。
对于任何 EPS/PS PostScript 到图像转换器应用程序,以下 Java 代码都能很好地运行,请执行以下步骤:
- 使用 PsDocument 类加载 PS 或 EPS 文件。
- 为目标图像格式创建 ImageSaveOptions 对象。
- 使用选项对象、输出文件夹和文件名调用 PsDocument.saveAsImage()。
PostScript 转换为图像的 Java 代码
// Convert PS file to PNG image.
// Learn more: https://docs.aspose.com/page/java/convert-ps/
//Outut file
String outputFileName = "output_image.png";
// Initialize PsDocument with the name of PostScript file.
PsDocument document = new PsDocument(getDataDir() + "inputForImage.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();
//Set output image format.
options.setImageFormat(ImageFormat.PNG);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
options.setAdditionalFontsFolders(new String[] { "{FONT_FOLDER}" });
// Save PS document to an image.
document.saveAsImage(options, getOutputDir(), getFileNameWithoutExtension(outputFileName));
//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 。
- 使用选项对象、输出文件夹和文件名调用 XpsDocument.saveAsImage()。
XPS 转换为图像的 Java 代码
// Convert XPS file to PNG image.
// Learn more: https://docs.aspose.com/page/java/convert-xps/
//Outut file
String outputFileName = "XPStoPNG_out.png";
// Load XPS document form the XPS file
XpsDocument document = new XpsDocument(getDataDir() + "input.xps");
// Initialize options object with necessary parameters.
PngSaveOptions options = new PngSaveOptions();
options.setSmoothingMode(SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save XPS document to the PNG image files.
document.saveAsImage(options, getOutputDir(), outputFileName);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 类加载 XPS 文件。
- 创建 PdfSaveOptions 对象。
- 使用输出文件路径和选项对象调用 XpsDocument.saveAsPdf()。
XPS 转换为 PDF 的 Java 代码
// Convert XPS file to PDF document.
// Learn more: https://docs.aspose.com/page/java/convert-xps/
// Load XPS document form the XPS file
XpsDocument document = new XpsDocument(getDataDir() + "input.xps");
// Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(PdfImageCompression.Jpeg);
options.setTextCompression(PdfTextCompression.Flate);
options.setPageNumbers(new int[] { 1, 2, 6 });
// Save document as PDF
document.saveAsPdf(getOutputDir() + "XPStoPDF_out.pdf", options);