转换 PS、EPS 和 XPS
适用于 .NET 的 PS、EPS 和 XPS 转换器 API 解决方案。
每当需要以编程方式转换 PostScript PS 和 Encapsulated PostScript EPS 文件以及 XPS 文档时,.NET API 可以顺利完成并转换多个文件。对于 PS 和 EPS,API 支持 1-3 级 PostScript 运算符和大多数 EPS 标题注释,以及转换具有最大一致性的 PostScript 文档,除了少数字体情况和 API 处理诸如 Time New Roman 之类的字体。
此外,对于 XPS 文件转换,API 可以添加或删除页面,处理画布、路径和字形元素,创建矢量图形、文本字符串,转换 XPS 大纲项等等。
此处的 .NET API 解决方案允许您以编程方式转换诸如 PS、EPS 和 XPS 等 PDL 格式的文件,但您可能会发现查看和尝试在这些本机 API 上开发的跨平台很有用。
通过 C# .NET 将 PostScript 转换为 PDF。
要通过 .NET API 将 PostScript PS 和 Encapsulated PostScript EPS 文件转换为 PDF,您需要执行以下步骤:
- 使用 PsDocument Class 加载 PS 或 EPS 文件。
- 使用 PdfSaveOptions Class 设置 PDF 保存。
- 使用 FileStream Class 输出 PDF 文件。
- PdfDevice Class 通过使用输出 PDF 文件流对象进行初始化。
- 调用 PsDocument.Save 进行 PDF 转换。
PS EPS 到 PDF 转换的 C# 代码
// The path to the documents directory. | |
string dataDir = "definedDirectoryPath"; | |
// 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 (Exception ex in options.Exceptions) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} |
通过 C# .NET 将 PostScript 转换为图像。
对于任何 EPS/PS PostScript 到图像转换器应用程序,以下 C# 代码运行良好,因此请执行以下步骤:
- 使用具有输入文件流作为参数的 PsDocument 类加载文档。
- 创建 ImageSaveOptions Class 对象并使用所需的设置对其进行初始化。
- 将每个输入文件页面保存为图像 PNG、JPG、TIFF、BMP 等。
PostScript 到图像转换的 C# 代码
// The path to the documents directory. | |
string dataDir = "definedDirectoryPath"; | |
// 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); | |
//Save images bytes arrays as image files. | |
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++; | |
} |
FAQ
1. 我可以用这个 API 解决方案转换 Postscript 吗?
Aspose.Page 具有允许您在线或以编程方式将 PS、XPS 和 EPS 文件转换为其他格式的功能。如果您需要立即在线转换您的文件,您可以使用 页面描述语言格式文件转换器 跨平台应用程序。
2. 转换器支持哪些页面描述语言?
此转换功能支持具有 .ps、.eps 和 .xps 扩展名的文件。 PDF 和 SVG 等著名的 PDL 在 Aspose.products 中表示为单独的解决方案
3. 功能是免费的吗?
跨平台转换器 是免费的,对于 API 解决方案,您可以获得免费试用版,然后在需要时购买产品。
通过 C# .NET 将 XPS 转换为图像 JPG、PNG、BMP。
.NET API 还支持XPS 转换为图片BMP、JPG、PNG、TIFF 等,并提供XpsDocument 类进行XPS 操作。要将 XPS 转换为图像,请执行以下步骤:
- 从流中加载 XPS 文件。
- 初始化相关的图像保存选项,例如 XPS to JPG 它是 JpegSaveOptions 对于 XPS 到 PNG,它的 PngSaveOptions 。这是所有 XPS 到图像的列表 保存选项 。
- 定义渲染的相关设置,如 SmoothingMode、Resolution 和 PageNumbers 等。最后遍历文档分区以将它们保存到图像中。
XPS 到图像转换的 C# 代码
// The path to the documents directory. | |
string dataDir = "definedDirectoryPath"; | |
//Outut file | |
string outputFileName = dataDir + "XPStoImage_out.jpeg"; | |
// Load XPS document form the XPS file | |
XpsDocument document = new XpsDocument(dataDir + "input.xps", new XpsLoadOptions()); | |
// Initialize options object with necessary parameters. | |
JpegSaveOptions options = new JpegSaveOptions() | |
{ | |
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality, | |
Resolution = 300, | |
PageNumbers = new int[] { 1, 2, 6 } | |
}; | |
// Save XPS document to the images byte arrays. The first dimension is for inner documents | |
/// and the second one is for pages within inner documents. | |
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 | |
using (Stream imageStream = System.IO.File.Open(Path.GetDirectoryName(outputFileName) + Path.DirectorySeparatorChar + | |
Path.GetFileNameWithoutExtension(outputFileName) + "_" + (i + 1) + "_" + (j + 1) + | |
Path.GetExtension(outputFileName), System.IO.FileMode.Create, System.IO.FileAccess.Write)) | |
// Write image | |
imageStream.Write(imagesBytes[i][j], 0, imagesBytes[i][j].Length); | |
} | |
} |