วิธี Render LaTeX เป็น PNG ใน C#

ในคำแนะนำทีละขั้นตอนนี้ คุณจะได้เรียนรู้วิธีการแสดงผล LaTeX เป็น PNG ใน C# ข้อมูลโค้ดในบทช่วยสอนนี้ทำให้การเรนเดอร์ลาเท็กซ์เป็น png ใน C# เป็นเรื่องง่ายในไม่กี่ขั้นตอน

ขั้นตอนใน Render LaTeX เป็น PNG ใน C#

  1. ติดตั้งแพ็คเกจ Aspose.TeX for .NET จาก NuGet.org
  2. รวมเนมสเปซ Aspose.TeX, Aspose.TeX.IO และ Aspose.TeX.Presentation.Image
  3. ใช้ใบอนุญาตกับ Aspose.TeX สำหรับ .NET โดยใช้วิธี SetLicense
  4. สร้างวัตถุ TeXOptions ด้วยการตั้งค่า TeXConfig
  5. ตั้งค่าไดเร็กทอรีการทำงานอินพุตและเอาต์พุตเพื่ออ่านและบันทึกไฟล์
  6. ตั้งค่าตัวเลือก TerminalOutput เป็น OutputFileTerminal
  7. สร้างวัตถุ PngSaveOptions และตั้งค่าคุณสมบัติ
  8. สร้าง ImageDevice object ที่จะส่งผ่านไปยัง Typeset method
  9. เรียกใช้การดำเนินการเรียงพิมพ์โดยใช้วิธีการเรียงพิมพ์

ขั้นตอนข้างต้นจะช่วยแปลง TeX เป็น PNG ใน C# ได้อย่างง่ายดาย อินพุต TeX file จะถูกอ่านจากไดเร็กทอรีการทำงานที่ระบุในตัวเลือก และไฟล์ PNG เอาต์พุตจะถูกวางในไดเร็กทอรีการทำงานเอาต์พุต

รหัสสำหรับ Render LaTeX เป็น PNG ใน C#

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);
}
}
}

ข้อมูลโค้ดที่แสดงด้านบนทำให้เห็นวิธีการเรนเดอร์ TeX เป็น PNG ใน C# อย่างชัดเจนในไม่กี่ขั้นตอน เนื่องจากเรากำลังบันทึกเอาต์พุตไปยังระบบไฟล์ ดังนั้นแทนที่จะใช้เทอร์มินัลเอาต์พุตของคอนโซล เราได้ระบุเทอร์มินัลไฟล์เป็นเทอร์มินัลเอาต์พุตของเรา

เมื่อใช้บทช่วยสอนนี้ คุณสามารถสร้าง LaTeX renderer ของคุณเองสำหรับแอปพลิเคชัน C# และ .NET ได้อย่างง่ายดาย

 ไทย