
Helpful resources ▼
使用 .NET 转换 PostScript 文件
Contents
[
Hide
Show
]本文以 PS 到 PDF 格式转换为例,讲解如何使用 C# 转换 PS 文件。
Aspose.Page 允许您使用 C# 转换 BMP、JPG、TIFF、PNG 和 PDF 格式的 PS 文件。
C# PS 转 PDF
您可以通过免费在线工具检查 Aspose.Page PS 转 PDF 的质量并查看结果 PostScipt 转 PDF 转换器、PS 查看器 以及其他 用于操作 PS 文件的应用程序
Aspose.Page for .NET PS 转 PDF 转换器允许转换 .NET 平台支持的任何语言编写的 PostScript (PS) 文件:C#、VB、J#。所有转换操作均以与以下示例类似的方式执行。
执行 PS 转 PDF 的步骤:
- 从 PS 文件创建 PsDocument 实例。
- 使用 PdfSaveOptions(或 ImageSaveOptions(如果转换为图像格式))指定 AdditionalFontsFolder 和 SuppressError 布尔值。
- 从先前创建的输出流创建 PdfDevice(或 ImageDevice(如果转换为图像格式)的实例。
- 使用 PDF 保存选项将 PostScript 文档保存为 PDF。
- 如果 SuppressErrors 值为真(默认情况下),则可以看到在 PS 转换为 PDF 期间引发了哪些错误,并将其保存在 Exceptions 列表中。
{{#if_output 'PDF'}} // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion(); // Initialize PsDocument with the name of PostScript file. PsDocument document = new PsDocument(dataDir + "input.ps"); // If you want to convert Postscript file despite of minor errors set this flag bool 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.AdditionalFontsFolders = 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, Aspose.Page.Drawing.Size(595x842)); // or //saveOptions.Size = new Aspose.Page.Drawing.Size(595x842); document.SaveAsPdf(dataDir + "outputPDF_out.pdf", options); //Review errors if (suppressErrors) { foreach (PsConverterException ex in options.Exceptions) { Console.WriteLine(ex.Message); } } {{/if_output}} {{#if_output 'BMP'}} // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion(); // Initialize PsDocument with the name of PostScript file. PsDocument document = new PsDocument(dataDir + "inputForImage.ps"); // If you want to convert Postscript file despite of minor errors set this flag bool suppressErrors = true; //Initialize options object with necessary parameters. ImageSaveOptions options = new ImageSaveOptions(); //Set output image format. options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Bmp; // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" }; // Save PS document as array of image bytes, one bytes array for one page. byte[][] imagesBytes = document.SaveAsImage(options); int i = 0; foreach (byte[] imageBytes in imagesBytes) { string imagePath = Path.GetFullPath(dataDir + "out_image" + i.ToString() + "." + options.ImageFormat.ToString().ToLower()); using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write)) { fs.Write(imageBytes, 0, imageBytes.Length); } i++; } //Review errors if (suppressErrors) { foreach (PsConverterException ex in options.Exceptions) { Console.WriteLine(ex.Message); } } {{/if_output}} {{#if_output 'JPG'}} // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion(); // Initialize PsDocument with the name of PostScript file. PsDocument document = new PsDocument(dataDir + "inputForImage.ps"); // If you want to convert Postscript file despite of minor errors set this flag bool suppressErrors = true; //Initialize options object with necessary parameters. ImageSaveOptions options = new ImageSaveOptions(); //Set output image format. options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Jpeg; // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" }; // Save PS document as array of image bytes, one bytes array for one page. byte[][] imagesBytes = document.SaveAsImage(options); int i = 0; foreach (byte[] imageBytes in imagesBytes) { string imagePath = Path.GetFullPath(dataDir + "out_image" + i.ToString() + "." + options.ImageFormat.ToString().ToLower()); using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write)) { fs.Write(imageBytes, 0, imageBytes.Length); } i++; } //Review errors if (suppressErrors) { foreach (PsConverterException ex in options.Exceptions) { Console.WriteLine(ex.Message); } } {{/if_output}} {{#if_output 'PNG'}} // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion(); // Initialize PsDocument with the name of PostScript file. PsDocument document = new PsDocument(dataDir + "inputForImage.ps"); // If you want to convert Postscript file despite of minor errors set this flag bool suppressErrors = true; //Initialize options object with necessary parameters. ImageSaveOptions options = new ImageSaveOptions(); //Set output image format. options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Png; // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" }; // Save PS document as array of image bytes, one bytes array for one page. byte[][] imagesBytes = document.SaveAsImage(options); int i = 0; foreach (byte[] imageBytes in imagesBytes) { string imagePath = Path.GetFullPath(dataDir + "out_image" + i.ToString() + "." + options.ImageFormat.ToString().ToLower()); using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write)) { fs.Write(imageBytes, 0, imageBytes.Length); } i++; } //Review errors if (suppressErrors) { foreach (PsConverterException ex in options.Exceptions) { Console.WriteLine(ex.Message); } } {{/if_output}} {{#if_output 'TIFF'}} // For complete examples and data files, please go to https://github.com/aspose-page/Aspose.Page-for-.NET // The path to the documents directory. string dataDir = RunExamples.GetDataDir_WorkingWithDocumentConversion(); // Initialize PsDocument with the name of PostScript file. PsDocument document = new PsDocument(dataDir + "inputForImage.ps"); // If you want to convert Postscript file despite of minor errors set this flag bool suppressErrors = true; //Initialize options object with necessary parameters. ImageSaveOptions options = new ImageSaveOptions(); //Set output image format. options.ImageFormat = Aspose.Page.Drawing.Imaging.ImageFormat.Tiff; // If you want to add special folder where fonts are stored. Default fonts folder in OS is always included. options.AdditionalFontsFolders = new string[] { @"{FONT_FOLDER}" }; // Save PS document as array of image bytes, one bytes array for one page. byte[][] imagesBytes = document.SaveAsImage(options); int i = 0; foreach (byte[] imageBytes in imagesBytes) { string imagePath = Path.GetFullPath(dataDir + "out_image" + i.ToString() + "." + options.ImageFormat.ToString().ToLower()); using (FileStream fs = new FileStream(imagePath, FileMode.Create, FileAccess.Write)) { fs.Write(imageBytes, 0, imageBytes.Length); } i++; } //Review errors if (suppressErrors) { foreach (PsConverterException ex in options.Exceptions) { Console.WriteLine(ex.Message); } } {{/if_output}}
我们来看一下 PdfSaveOptions。使用此类,我们可以在将 PS 转换为 PDF 时指定不同的转换参数。
- Size 指定结果文档的页面大小。
- AdditionalFontsFolder 指定字体的存放位置。默认情况下,系统字体文件夹始终包含在内。
- ConvertFontsToTTF 指定处理器将非 TrueType 基础字体(目前为 Type1、Type3 和 Type32)转换为 TrueType (TTF) 字体。如果 PS 文件包含的 Type1、Type3 或 Type32 字体字形总数超过 500 个,则将这些字体转换为 TTF 格式后,转换速度会更快,生成的 PDF 文件体积也会更小。
- SuppressError 控制 PS 转换器在出现非关键错误时的行为。如果该值为 true,则可以在转换后在 Exceptions 字段中查看此类错误列表。默认值为 true。
- Debug 允许将调试信息输出到控制台。默认值为 false。
您也可以在我们的 PS Converter 上在线查看 PS 转换功能。在那里,您可以一次转换多个 PS 文件,并在几秒钟内下载结果。
您可以从 GitHub下载示例和数据文件。