这个有趣的主题提供了有关如何使用 C++** 将 LaTeX 渲染为 PNG 的演示。我们也将 LaTeX 文件称为 .tex 文件,并且可以使用简单的 API 属性和方法轻松地在 C++ 中将 LaTeX 渲染为 PNG。使用此示例的一个好处是 API 调用的执行不依赖于任何其他应用程序软件或第三方工具。
使用 C++ 将 LaTeX 渲染为 PNG 的步骤
- 使用 NuGet 包管理器工具安装 Aspose.Tex.Cpp
- 包括对 Aspose::TeX、Aspose::TeX::IO 和 Aspose::TeX::Presentation::Image 命名空间的引用
- 实例化 TeXOptions Class 对象以设置配置
- 实例化 PngSaveOptions Class 对象以将 LaTeX 保存为 PNG 图像
- 初始化 ImageDevice 以进行渲染
- 使用 TexJob 将 LaTeX 渲染为 PNG 图像
LaTeX 文档用于科学和研究目的,可供技术用户使用,并包含纯文本形式的信息。为了将 LaTeX 导出为 C++ 中的 PNG 图像,我们将首先创建 TeXOptions 类 的实例,用于设置涉及输入目录、输出目录和控制台选项的配置。在后续步骤中,我们将设置 PngSaveOptions 属性,例如图像分辨率。最后,我们将初始化 ImageDevice 并使用 TexJob render LaTeX 使用 C++ 到 PNG 图像。
在 C++ 中将 LaTeX 转换为 PNG 的代码
#pragma once | |
#include <system/text/encoding.h> | |
#include <system/io/text_writer.h> | |
#include <system/io/memory_stream.h> | |
#include <system/array.h> | |
#include <Aspose.TeX.Cpp/TeXOptions.h> | |
#include <Aspose.TeX.Cpp/TeXConfig.h> | |
#include <Aspose.TeX.Cpp/TeXJob.h> | |
#include <Aspose.TeX.Cpp/Presentation/SaveOptions.h> | |
#include <Aspose.TeX.Cpp/Presentation/Image/PngSaveOptions.h> | |
#include <Aspose.TeX.Cpp/Presentation/Image/ImageDevice.h> | |
#include <Aspose.TeX.Cpp/IO/OutputFileSystemDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/OutputConsoleTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/IOutputWorkingDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/IOutputTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/InputFileSystemDirectory.h> | |
#include <Aspose.TeX.Cpp/IO/InputConsoleTerminal.h> | |
#include <Aspose.TeX.Cpp/IO/IInputTerminal.h> | |
#include <cstdint> | |
#include <Aspose.TeX.Cpp/License.h> | |
using namespace Aspose::TeX; | |
using namespace Aspose::TeX::IO; | |
using namespace Aspose::TeX::Presentation::Image; | |
class TexToPngConverter{ | |
public: | |
static void TexToPNGRendering() | |
{ | |
// Initialize license object | |
System::SharedPtr<License> TexLicense = System::MakeObject<License>(); | |
// Applying license for rendering PNG | |
TexLicense->SetLicense(u"Aspose.Total.NET.lic"); | |
// Instantiate TeXOptions object for configuring settings | |
System::SharedPtr<TeXOptions> RenderingOptions = TeXOptions::ConsoleAppOptions(TeXConfig::ObjectTeX()); | |
// Specify the job name. | |
RenderingOptions->set_JobName(u"stream-image-out"); | |
// Specify the input file working directory. | |
RenderingOptions->set_InputWorkingDirectory(System::MakeObject<InputFileSystemDirectory>(u"")); | |
// Specify the output file working directory. | |
RenderingOptions->set_OutputWorkingDirectory(System::MakeObject<OutputFileSystemDirectory>(u"")); | |
// Setting the console input terminal | |
RenderingOptions->set_TerminalIn(System::MakeObject<InputConsoleTerminal>()); | |
// Setting the console output terminal | |
RenderingOptions->set_TerminalOut(System::MakeObject<OutputConsoleTerminal>()); | |
// Creating and specifying PngSaveOptions | |
System::SharedPtr<PngSaveOptions> ImageOptions = System::MakeObject<PngSaveOptions>(); | |
ImageOptions->set_Resolution(300); | |
RenderingOptions->set_SaveOptions(ImageOptions); | |
// Instantiating ImageDevice for rendering | |
System::SharedPtr<ImageDevice> ImageDevise = System::MakeObject<ImageDevice>(); | |
// Running TexJob for rendering to PNG | |
TeXJob::TeXJob(System::MakeObject<System::IO::MemoryStream>(System::Text::Encoding::get_ASCII()-> | |
GetBytes(u"\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt")), | |
ImageDevise, RenderingOptions); | |
// During executoion, when the console prompts the input, | |
// Step 1: Type "ABC", press Enter | |
// Step 2: Type "\end" and press Enter again | |
// In order to view the ouptut and to look write. | |
RenderingOptions->get_TerminalOut()->get_Writer()->WriteLine(); | |
// Alternatively, you can also get the images in form of array of byte arrays | |
// The 0th index belong to first page. | |
System::ArrayPtr<System::ArrayPtr<uint8_t>> result = ImageDevise->get_Result(); | |
} | |
}; |
在本主题中,我们学习了 C++ 如何使用简单的 API 调用从 LaTeX 创建 PNG 图像。如果您有兴趣了解渲染 MPP 文件等其他功能,请参阅 如何使用 C++ 将 MPP 转换为 XPS 上的文章。