在本分步操作指南中,您将学习如何在 C# 中将 LaTeX 渲染为 PNG。本教程中的代码片段可以通过几个步骤轻松地将 C# 中的 Latex 渲染为 png。
在 C# 中将 LaTeX 渲染为 PNG 的步骤
- 从 NuGet.org 安装 Aspose.TeX for .NET 包
- 包括 Aspose.TeX、Aspose.TeX.IO 和 Aspose.TeX.Presentation.Image 命名空间
- 使用 SetLicense 方法将许可证应用到 Aspose.TeX for .NET
- 使用 TeXConfig 设置创建 TeXOptions 对象
- 设置输入输出工作目录读取和保存文件
- 将 TerminalOutput 选项设置为 OutputFileTerminal
- 创建 PngSaveOptions 对象并设置属性
- 创建要传递给 Typeset 方法的 ImageDevice 对象
- 使用 Typesetting 方法运行排版操作
上述步骤将有助于在 C# 中轻松地将 TeX 转换为 PNG。输入 TeX file 将从选项中指定的工作目录中读取,输出 PNG 文件将放置在输出工作目录中。
在 C# 中将 LaTeX 渲染为 PNG 的代码
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
//Add reference to Aspose.TeX for .NET API | |
//Use following namespaces to render Latex file to PNG format | |
using Aspose.TeX; | |
using Aspose.TeX.IO; | |
using Aspose.TeX.Presentation.Image; | |
namespace RenderLatexToPNG | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//Set Aspose license before rendering latex file to PNG format | |
Aspose.TeX.License AsposeTeXLicense = new Aspose.TeX.License(); | |
AsposeTeXLicense.SetLicense(@"c:\asposelicense\license.lic"); | |
//Create TeXOptions object with ObjectTex config settings | |
TeXOptions TeXFormatOptions = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX()); | |
//Set input and output working directory | |
TeXFormatOptions.InputWorkingDirectory = new InputFileSystemDirectory(@"c:\samples"); | |
TeXFormatOptions.OutputWorkingDirectory = new OutputFileSystemDirectory(@"c:\samples"); | |
//Set output terminal option to file terminal to save to a file | |
TeXFormatOptions.TerminalOut = new OutputFileTerminal(TeXFormatOptions.OutputWorkingDirectory); | |
//PNG save options | |
PngSaveOptions pngSaveOptions = new PngSaveOptions(); | |
pngSaveOptions.Resolution = 300; | |
TeXFormatOptions.SaveOptions = pngSaveOptions; | |
//Create an ImageDevice object | |
ImageDevice imageDevice = new ImageDevice(); | |
//Run typesetting. | |
TeX.Typeset("customtex", imageDevice, TeXFormatOptions); | |
} | |
} | |
} |
上面显示的代码片段清楚地说明了如何通过几个步骤在 C# 中将 TeX 渲染为 PNG。由于我们将输出保存到文件系统,因此我们指定了文件终端作为输出终端,而不是控制台输出终端。
使用本教程,您可以轻松地为 C# 和 .NET 应用程序构建自己的 LaTeX 渲染器。