Dieses interessante Thema zeigt, wie LaTeX mit C++** in PNG gerendert wird. Wir nennen die LaTeX-Dateien auch als .tex-Dateien und man kann mit einfachen API-Eigenschaften und -Methoden ganz einfach LaTeX in C++ in PNG rendern. Eine gute Sache bei der Verwendung dieses Beispiels ist, dass API-Aufrufe für ihre Ausführung nicht von anderer Anwendungssoftware oder Tools von Drittanbietern abhängig sind.
Schritte zum Rendern von LaTeX in PNG mit C++
- Installieren Sie Aspose.Tex.Cpp mit dem NuGet-Paket-Manager-Tool
- Fügen Sie einen Verweis auf die Namespaces Aspose::TeX, Aspose::TeX::IO und Aspose::TeX::Presentation::Image ein
- Objekt TeXOptions Class instanziieren, um Konfigurationen festzulegen
- Objekt PngSaveOptions Class instanziieren, um LaTeX als PNG-Bild zu speichern
- Initialisieren Sie das ImageDevice für das Rendern
- Verwenden Sie TexJob zum Rendern von LaTeX in ein PNG-Bild
LaTeX-Dokumente sind für wissenschaftliche und Forschungszwecke gedacht, die von technischen Benutzern verwendet werden können und Informationen in Klartextform enthalten. Um LaTeX in ein PNG-Bild in C++ zu exportieren, erstellen wir zuerst eine Instanz der TeXOptions-Klasse zum Festlegen von Konfigurationen mit Eingabeverzeichnis, Ausgabeverzeichnis und Konsolenoptionen. In den nachfolgenden Schritten werden wir PngSaveOptions-Eigenschaften wie die Bildauflösung festlegen. Schließlich werden wir ImageDevice initialisieren und mit TexJob LaTeX mit C++ in ein PNG-Bild rendern.
Code zum Konvertieren von LaTeX in PNG in 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(); | |
} | |
}; |
In diesem Thema haben wir gelernt, wie C++ mithilfe einfacher API-Aufrufe ein PNG-Bild aus LaTeX erstellt. Wenn Sie an anderen Funktionen wie dem Rendern von MPP-Dateien interessiert sind, lesen Sie den Artikel zu wie man MPP in XPS mit C++ konvertiert.