این موضوع جالب نشان می دهد که چگونه می توان LaTeX را با استفاده از C++** به PNG تبدیل کرد. ما همچنین فایلهای LaTeX را بهعنوان فایلهای .tex مینامیم و به راحتی میتوان با ویژگیها و روشهای ساده API، لاتک را به PNG در C++** رندر کرد. یک چیز خوب در مورد استفاده از این مثال این است که فراخوانی های API برای اجرای آنها به هیچ نرم افزار کاربردی دیگر یا ابزار شخص ثالث وابسته نیستند.
مراحل رندر LaTeX به PNG با استفاده از C++
- با استفاده از ابزار مدیریت بسته 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 به تصویر PNG در C++، ابتدا نمونه ای از کلاس TeXOptions را برای تنظیم تنظیمات شامل دایرکتوری ورودی، دایرکتوری خروجی و گزینه های کنسول ایجاد می کنیم. در مراحل بعدی، ویژگی های PngSaveOptions را مانند وضوح تصویر تنظیم می کنیم. در نهایت، ImageDevice را مقداردهی اولیه می کنیم و با استفاده از TexJob تصویر LaTeX را با استفاده از C++ به PNG ارائه می کنیم.
کد برای تبدیل LaTeX به PNG در C++
#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، تصویر PNG را از LaTeX ایجاد می کند. اگر علاقه مند به یادگیری ویژگی های دیگری مانند رندر فایل های MPP هستید، به مقاله در نحوه تبدیل MPP به XPS با استفاده از ++C مراجعه کنید.