使用 Java 将 PostScript 文件合并为 PDF
您可以通过免费在线工具 PostScipt Merger
Aspose.Page for Java PS Merger 允许在任何支持 Java 虚拟机的操作系统上将 PostScript (PS) 文件合并为 PDF 文档。
执行 PS 到 PDF 合并需要执行以下几个步骤:
- 从第一个 PostSctipt 文件创建 PsDocument 实例。
- 创建一个 PostSctipt 文件数组,用于将文件与第一个文件合并。
- 使用 PdfSaveOptions 指定 AdditionalFontsFolder 和 SuppressError 布尔值。
- 将 PS 文件与创建的文档合并,并使用 PDF 保存选项将其保存为 PDF。
- 如果 SuppressErrors 值为 true(默认情况下),则可以查看将 EPS 文件合并为 PDF 文档时引发的错误,并将其保存在 Exceptions 列表中。
以下代码片段展示了如何在 Java 中将 PS 文件合并为 PDF 文档:
1// For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-Java
2
3 // The path to the documents directory.
4 String dataDir = Utils.getDataDir();
5
6 // Initialize PS document from PostScript file.
7 PsDocument document = new PsDocument(dataDir + "input.ps");
8
9 // Create an array of PostScript files that will be merged with the first one
10 String[] filesForMerge = new String[] { dataDir + "input2.ps", dataDir + "input3.ps" };
11
12 // If you want to merge PostScript file despite of minor errors set this flag
13 boolean suppressErrors = true;
14
15 //Initialize options object with necessary parameters.
16 PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
17 // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
18 //options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});
19 // Default page size is 595x842 and it is not mandatory to set it in PdfSaveOptions
20 // But if you need to specify size use following line
21 // PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new Dimension(595, 842));
22
23 // Merge files with initialized PsDocument and save it as PDF
24 document.mergeToPdf(dataDir + "mergePStoPDF.pdf", filesForMerge, options);
25
26 //Review errors
27 if (suppressErrors) {
28 for (Exception ex : options.getExceptions()) {
29 System.out.println(ex.getMessage());
30 }
31 }
我们来看一下
PdfSaveOptions。使用此类,我们可以在将 PS 文件合并为 PDF 时指定不同的转换参数。
- AdditionalFontsFolder 指定字体的保存位置。默认情况下,始终包含系统字体文件夹。
- SuppressError 控制出现非严重错误时 PS 到 PDF 合并的操作。如果值为 true,则可以在合并后在 Exceptions 字段中查看此类错误列表。默认值为 true。
- Debug 允许将调试信息输出到控制台。默认值为 false。