package com.example;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import com.aspose.tex.MathRendererOptions;
import com.aspose.tex.PngMathRenderer;
import com.aspose.tex.PngMathRendererOptions;
import com.aspose.tex.Size2D;

public class Main {
	// Latex Mathematical Expressions using Latex to Image API
	public static void main(String[] args) throws IOException {
		// Create an instance of the MathRendererOptions class and assign an object of the PngMathRendererOptions class.   
		MathRendererOptions options = new PngMathRendererOptions();
		// Specify the preamble by calling the setPreamble method.
		options.setPreamble("\\usepackage{amsmath}\r\n"
			+ "\\usepackage{amsfonts}\r\n"
			+ "\\usepackage{amssymb}\r\n"
			+ "\\usepackage{color}");
		
		// Specify the foreground color by invoking the setTextColor method.  
		options.setTextColor(Color.BLACK);
		// Call the setScale method to define the scaling factor 300%.
		options.setScale(3000);
		// Invoke the setBackgroundColor method to set the background color.
		options.setBackgroundColor(Color.YELLOW);
		// The setLogStream method will used to define the output stream for the log file.
		options.setLogStream(new ByteArrayOutputStream());
		// Call the showTerminal method to specify whether to show the terminal output on the console or not.
		options.showTerminal(true);
		// Instantiate an instance of the Size2D.Float class in which the dimensions of the resulting image will be written.
		Size2D size = new Size2D.Float();
		// Create the output stream for the formula image by initializing an object of the FileOutputStream class.
		final OutputStream stream = new FileOutputStream("/math-formula.png");
		try {
			// Call the render method to fire up the rendering.
			new PngMathRenderer().render("\\begin{equation*}\r\n" +
				"e^x = x^{\\color{red}0} + x^{\\color{red}1} "
				+ "\\frac{x^{\\color{red}2}}{2} "
				+ "\\frac{x^{\\color{red}3}}{6} "
				+ "\\cdots = \\sum_{n\\geq 0} "
				+ "\\frac{x^{\\color{red}n}}{n!}\r\n" 
				+ "\\end{equation*}", stream, options, size);
		} finally {
		  if (stream != null)
			stream.close();
		}
	}
}