乳胶到图像| .NET的Aspose.TeX
Aspose.TeX for .NET允许我们将乳胶文件转换为许多栅格图像格式。
如何将乳胶转换为png
让我们详细了解C#中的代码,提供最简单的方法将乳胶转换为PNG格式。
1// Convert LaTeX to PNG - simplest approach
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in PNG format.
10PngSaveOptions pngOptions = new PngSaveOptions();
11// Set image resolution to 300 DPI.
12pngOptions.Resolution = 300;
13options.SaveOptions = pngOptions;
14
15// Run LaTeX to PNG conversion.
16new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new ImageDevice(), options).Run();因此,我们需要做的第一件事(有时不是第一件)是创建 Texoptions类的实例。这样做的唯一静态方法是 consoleappoptions(),因此不要被其名称的含义所困扰。该方法将 TexConfig类的 Objectlatex实例进行,该类完全适合转换 latex file。该配置告诉对象Tex引擎加载对象乳胶格式并准备好接受乳胶文件。对象乳胶格式实际上只是 乳胶格式,只是它使用 Object tex特定的原始图来设置页面指标。
所需的第一个选项是 outputWorkingDirectory,该选项定义了将编写Tex输出的空间或区域。 此处是有关.net 的 aspose.tex中输出目录概念的详细信息。在此示例中,我们使用 outputeFilesystemDirectory类,该类使我们能够将输出写入指定的目录或文件夹。
第二个选项是 SaveOptions类实例,它将控制 对象模型对目标格式的转换。由于我们将乳胶转换为png,因此是 pngsaveoptions类实例,它使我们可以指定输出图像的分辨率。
然后,我们需要创建一个 Texjob类的实例。要转换存储在文件系统中的乳胶文件,我们使用 this版本的构造函数。我们需要指定文件的完整路径。否则,发动机将在当前目录(即 CurrentDirectory)中寻找它,并且很可能找不到它。然而, .tex扩展可能会省略。引擎将自动添加它。构造函数的第二个参数是 设备类实例。由于我们将乳胶转换为png,因此这是一个 Imagedevice类实例,这对于所有受支持的图像格式都是常见的。作为最后一个论点,我们通过了最近准备的转换选项。
现在剩下要做的就是 运行工作。
无论运行是否成功,我们都会看到的第一个结果将是终端输出。如果跑步成功,它将看起来像这样:
1This is ObjectTeX, Version 3.1415926-1.0 (Aspose.TeX 21.8)
2entering extended mode
3
4(<input_directory>\hello-world.ltx
5LaTeX2e <2011/06/27>
6(article.cls
7Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
8(size10.clo))
9No file hello-world.aux.
10[1]
11(<output_directory>\hello-world.aux) )
12Output written on hello-world.png (1 page).
13Transcript written on hello-world.log.我们将在我们指定为输出目录的文件夹中找到引擎劳动力的其他“水果”。这些将是成绩单文件,在这里是!,主要输出png映像文件。
编写主要输出PNG文件的另一种方法
还有另一种将图像数据作为字节数组数组的方法,第二维中的每个数组代表单独的页面的图像数据。
替代转换示例
以下 C# 代码片段演示了另一种获取 PNG 输出的方法,即将图像数据写入内存流,而不是直接写入文件系统。当您需要在内存中保存生成的 PNG 字节进行进一步处理(例如通过网络发送、存储到数据库中或在保存前执行其他转换)时,此方法非常有用。
1// Convert LaTeX to PNG - alternative approach with manual page-by-page saving
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in PNG format.
10PngSaveOptions pngSaveOptions = new PngSaveOptions();
11// Disable automatic image writing - we'll write them manually.
12pngSaveOptions.DeviceWritesImages = false;
13options.SaveOptions = pngSaveOptions;
14
15// Create the image device.
16ImageDevice device = new ImageDevice();
17
18// Run LaTeX to PNG conversion.
19new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), device, options).Run();
20
21// Save pages file by file (useful for multi-page documents).
22for (int i = 0; i < device.Result.Length; i++)
23{
24 using (Stream fs = File.Open(Path.Combine(OutputDir, $"page-{i + 1}.png"), FileMode.Create))
25 fs.Write(device.Result[i], 0, device.Result[i].Length);
26}*“ Page-n.png” *文件将写入我们指定的任何路径。与 PDF输出不同,他们将复制写入输出目录的输出PNG文件。
关于输入选项
如果我们的主要输入文件需要依赖项,例如,这些依赖项不包含在基本乳胶系统和支持的软件包中,我们必须设置
requientInputInputDirectory选项选项的类似方式,我们设置了
outputworkworkingDirectory选项并将依赖项放入该目录中。依赖项可以在子目录中任意组织。如果我们有自己的文件要在排版过程中包括,例如外部图形文件,我们还必须使用通往收集这些文件的位置的路径来设置
InputWorkingDirectory。我们还可以将主输入文件放置在输入目录中的某个位置,并在run()方法中指定相对路径(如果主输入文件在root中,则根本没有指定路径)。
此处是有关.net 的aspose.tex中输入目录概念的详细信息,并提供了实现。
讨论了其他TEX的工作选择 此处。
您还可以查看基于 Aspose.TeX for .NET API 构建的免费 LaTeX 到 PNG 转换 web 应用程序。
下面,我们讨论乳胶转换为其他受支持的光栅图像格式,而没有深入细节,因为实际上没有细节。唯一的区别是转换选项中 SaveOptions属性的类型。
如何将乳胶转换为JPG
1// Convert LaTeX to JPEG
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in JPEG format.
10options.SaveOptions = new JpegSaveOptions();
11
12// Run LaTeX to JPEG conversion.
13new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new ImageDevice(), options).Run();您还可以查看基于 Aspose.TeX for .NET API 构建的免费 LaTeX 到 JPG 转换 web 应用程序。
如何将乳胶转换为tiff
1// Convert LaTeX to TIFF
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in TIFF format.
10options.SaveOptions = new TiffSaveOptions();
11
12// Run LaTeX to TIFF conversion.
13new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new ImageDevice(), options).Run();您还可以查看基于 Aspose.TeX for .NET API 构建的免费 LaTeX 到 TIFF 转换 网页应用。
如何将乳胶转换为BMP
1// Convert LaTeX to BMP
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in BMP format.
10options.SaveOptions = new BmpSaveOptions();
11
12// Run LaTeX to BMP conversion.
13new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new ImageDevice(), options).Run();您还可以查看基于 Aspose.TeX for .NET API 构建的免费 LaTeX 到 BMP 转换 web 应用程序。
如何将乳胶转换为SVG
另一种支持的目标格式是 svg。 **可扩展的向量图形(SVG)**是一种基于XML的矢量图像格式,用于定义二维图形,支持交互性和动画。
SVG图像以矢量图形格式定义,并存储在XML文本文件中。 svg可以将图像缩放而不会丢失质量,并且svg可以搜索,索引,脚本脚本和压缩。可以使用文本编辑器或矢量图形编辑器创建和编辑XML文本文件,并由最使用的Web浏览器渲染。
有 Aspose.SVG库,它提供了与svg相关的各种功能。这些包括创建,加载,编辑和转换SVG文档。
乳胶到SVG转换也与 转换为栅格图像格式一样简单,只是必须将 saveOptions设置为 svgsaveoptions类实例,并且必须将设备更改为 svgdevice类的实例。
1// Convert LaTeX to SVG - simplest approach
2
3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
5
6// Specify a file system working directory for the output.
7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
8
9// Initialize the options for saving in SVG format.
10options.SaveOptions = new SvgSaveOptions();
11
12// Run LaTeX to SVG conversion.
13new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new SvgDevice(), options).Run();您还可以查看基于 Aspose.TeX for .NET API 构建的免费 LaTeX 到 SVG 转换 web 应用程序。