Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active August 12, 2024 10:42
Show Gist options
  • Save aspose-com-gists/76c7e5770ac8b3f6d409f6ec60f02030 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/76c7e5770ac8b3f6d409f6ec60f02030 to your computer and use it in GitHub Desktop.
Gists for Aspose.TeX for .NET
// Create conversion options instance.
...
// Force the TeX engine to output the specified date in the title.
options.DateTime = new System.DateTime(2022, 12, 18);
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Initialize the options for saving in PDF format.
options.SaveOptions = new PdfSaveOptions();
// Enable the shell command execution.
options.ShellMode = ShellMode.ShellRestricted;
// Run LaTeX to PDF conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "embedded-base64-image.tex"), new PdfDevice(), options).Run();
// Create conversion options instance.
...
// Set to true to make the engine skip missing packages (when your file references one) without errors.
options.IgnoreMissingPackages = true;
// Create conversion options instance.
...
// Specify the console as the input terminal.
options.TerminalIn = new InputConsoleTerminal(); // Default. Arbitrary assignment.
// Create conversion options instance.
...
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Create conversion options instance.
...
// Create some device.
...
// Run LaTeX to XPS conversion.
new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(@"\documentclass{article} \begin{document} Hello, World! \end{document}")),
device, options).Run();
// Open the stream for the ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "zip-in.zip"), FileMode.Open))
{
// Create conversion options instance.
...
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
}
// Create conversion options instance.
...
// Set the interaction mode.
options.Interaction = Interaction.NonstopMode;
// Create conversion options instance.
...
// Set the job name.
options.JobName = "my-job-name";
// Create conversion options instance.
...
// Initialize the options for saving in BMP format.
options.SaveOptions = new BmpSaveOptions();
// Create conversion options instance.
...
// Initialize the options for saving in JPEG format.
options.SaveOptions = new JpegSaveOptions();
// Create the stream to write the PDF file to.
using (Stream pdfStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "any-name.pdf"), FileMode.Create))
{
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
...
// Run LaTeX to PDF conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new PdfDevice(pdfStream), options).Run();
}
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Initialize the options for saving in PDF format.
options.SaveOptions = new PdfSaveOptions();
// Run LaTeX to PDF conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new PdfDevice(), options).Run();
// Create conversion options instance.
...
// Initialize the options for saving in PNG format.
PngSaveOptions pngSaveOptions = new PngSaveOptions();
pngSaveOptions.DeviceWritesImages = false;
options.SaveOptions = pngSaveOptions;
// Create the image device.
ImageDevice device = new ImageDevice();
// Run LaTeX to PNG conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), device, options).Run();
// Save pages file by file.
for (int i = 0; i < device.Result.Length; i++)
{
using (Stream fs = File.Open(Path.Combine(RunExamples.OutputDirectory, $"page-{i + 1}.png"), FileMode.Create))
fs.Write(device.Result[i], 0, device.Result[i].Length);
}
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Initialize the options for saving in PNG format.
options.SaveOptions = new PngSaveOptions();
// Run LaTeX to PNG conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new ImageDevice(), options).Run();
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Initialize the options for saving in SVG format.
options.SaveOptions = new SvgSaveOptions();
// Run LaTeX to SVG conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new PdfDevice(), options).Run();
// Create conversion options instance.
...
// Initialize the options for saving in TIFF format.
options.SaveOptions = new TiffSaveOptions();
// Create the stream to write the XPS file to.
using (Stream xpsStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "any-name.xps"), FileMode.Create))
{
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
...
// Initialize the options for saving in XPS format.
options.SaveOptions = new XpsSaveOptions(); // Default value. Arbitrary assignment.
// Run LaTeX to XPS conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "hello-world.ltx"), new XpsDevice(xpsStream), options).Run();
}
// Create conversion options instance.
...
// Initialize the options for saving in XPS format.
options.SaveOptions = new XpsSaveOptions(); // Default value. Arbitrary assignment.
// Run LaTeX to XPS conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "sample.ltx"), new XpsDevice(), options).Run();
// Create conversion options instance.
...
// Run LaTeX to XPS conversion.
new TeXJob(new XpsDevice(), options).Run();
// Create conversion options instance.
...
// Set to true to make the engine not construct ligatures where normally it would.
options.NoLigatures = true;
// Create conversion options instance.
...
// Specify the console as the input terminal.
options.TerminalOut = new OutputConsoleTerminal(); // Default value. Arbitrary assignment.
// Create conversion options instance.
...
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Create conversion options instance.
...
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Open the stream for the ZIP archive that will serve as the output working directory.
using (Stream outZipStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "zip-pdf-out.zip"), FileMode.Create))
{
// Create conversion options instance.
...
// Specify a ZIP archive working directory for the output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
}
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true if you want math formulas to be converted to raster images.
options.SaveOptions.RasterizeFormulas = true;
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true if you want included graphics (if it contains vector elements) to be converted to raster images.
options.SaveOptions.RasterizeIncludedGraphics = true;
// Create conversion options instance.
...
// Ask the engine to repeat the job.
options.Repeat = true;
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify a file system working directory for the required input.
// The directory containing packages may be located anywhere.
options.RequiredInputDirectory = new InputFileSystemDirectory(Path.Combine(RunExamples.InputDirectory, "packages"));
// Initialize the options for saving in PNG format.
options.SaveOptions = new PngSaveOptions();
// Run LaTeX to PNG conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "required-input-fs.tex"), new ImageDevice(), options).Run();
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Initialize the options for saving in PNG format.
options.SaveOptions = new PngSaveOptions();
// Create a file stream for the ZIP archive containing the required package.
// The ZIP archive may be located anywhere.
using (Stream zipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "packages\\pgfplots.zip"), FileMode.Open))
{
// Specify a ZIP working directory for the required input.
options.RequiredInputDirectory = new InputZipDirectory(zipStream, "");
// Run LaTeX to PNG conversion.
new TeXJob(Path.Combine(RunExamples.InputDirectory, "required-input-zip.tex"), new ImageDevice(), options).Run();
}
// This is an implementation of IInputWorkingDirectory that is suitable for the TeX job's RequiredInputDirectory option
// in case required input contains fonts provided by external packages.
// The class additionally implements IFileCollector, which provides access to file collections by extension.
// This is necessary to load external font maps, which are files (outside TeX syntax) that map TeX's
// internal font names to file names of physical fonts.
public class RequiredInputDirectory : IInputWorkingDirectory, IFileCollector
{
private Dictionary<string, Dictionary<string, string>> _fileNames =
new Dictionary<string, Dictionary<string, string>>();
public RequiredInputDirectory()
{
}
// This method should preliminarily be called for each file entry that is supposed to be located inside
// the required input directory. Inside is an example of how the dictionary of file names could be organized
// for easy collection of file names by extension.
// Here fileName is a full file name. This can be a file path on a file system, a URL, or whatever else (theoretically).
public void StoreFileName(string fileName)
{
string extension = Path.GetExtension(fileName);
string name = Path.GetFileNameWithoutExtension(fileName);
Dictionary<string, string> files;
if (!_fileNames.TryGetValue(extension, out files))
_fileNames.Add(extension, files = new Dictionary<string, string>());
files[name] = fileName;
}
// The IInputWorkingDirectory implementation.
public NamedStream GetFile(string fileName, bool searchSubdirectories = false)
{
return new NamedStream(null, fileName); // Here we actually return a stream for the file requested by its name.
}
// Here is how we gather file collections by extension.
public string[] GetFileNamesByExtension(string extension, string path = null)
{
Dictionary<string, string> files;
if (!_fileNames.TryGetValue(extension, out files))
return new string[0];
return new List<string>(files.Values).ToArray();
}
public void Dispose()
{
_fileNames.Clear();
}
}
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true to make the device subset fonts used in the document.
options.SaveOptions.SubsetFonts = true;
// Create TeX engine options for no format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectIniTeX);
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Run format creation.
TeXJob.CreateFormat("customtex", options);
// For further output to look fine.
options.TerminalOut.Writer.WriteLine();
public class PackageGuesser : IGuessPackageCallback
{
private Dictionary<string, string> _map = new Dictionary<string, string>();
public PackageGuesser()
{
_map.Add("lhead", "fancyhdr"); // Defines the mapping between the \lhead command and the fancyhdr package.
}
public string GuessPackage(string commandName, bool isEnvironment)
{
string packageName;
if (!isEnvironment)
{
_map.TryGetValue(commandName, out packageName);
return packageName ?? ""; // It's better to return an empty string to avoid consequent calls for the same command name.
}
// Some code for environments
// ...
return "";
}
}
// Create repair options.
LaTeXRepairerOptions options = new LaTeXRepairerOptions();
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify a file system working directory for the required input.
// The directory containing packages may be located anywhere.
options.RequiredInputDirectory = new InputFileSystemDirectory(Path.Combine(RunExamples.InputDirectory, "packages"));
// Specify the callback class to externally guess packages required for undefined commands or environments.
options.GuessPackageCallback = new PackageGuesser();
// Run the repair process.
new Features.LaTeXRepairer(Path.Combine(RunExamples.InputDirectory, "invalid-latex.tex"), options).Run();
// Create rendering options setting the image resolution to 150 dpi.
PngFigureRendererOptions options = new PngFigureRendererOptions();
options.Resolution = 150;
// Specify the preamble.
options.Preamble = "\\usepackage{pict2e}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the figure image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.png"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new PngFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
\begin{picture}(6,5)
\thicklines
\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
\end{picture}", stream, options);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
}
// Create rendering options setting the image resolution to 150 dpi.
PngMathRendererOptions options = new PngMathRendererOptions();
options.Resolution = 150;
// Specify the preamble.
options.Preamble = @"\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the foreground color.
options.TextColor = System.Drawing.Color.Black;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the formula image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.png"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new PngMathRenderer().Render(@"\begin{equation*}
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!}
\end{equation*}", stream, options);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
}
// Create rendering options.
FigureRendererOptions options = new SvgFigureRendererOptions();
// Specify the preamble.
options.Preamble = "\\usepackage{pict2e}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the figure image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(RunExamples.OutputDirectory, "text-and-formula.svg"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new SvgFigureRenderer().Render(@"\setlength{\unitlength}{0.8cm}
\begin{picture}(6,5)
\thicklines
\put(1,0.5){\line(2,1){3}} \put(4,2){\line(-2,1){2}} \put(2,3){\line(-2,-5){1}} \put(0.7,0.3){$A$} \put(4.05,1.9){$B$} \put(1.7,2.95){$C$}
\put(3.1,2.5){$a$} \put(1.3,1.7){$b$} \put(2.5,1.05){$c$} \put(0.3,4){$F=\sqrt{s(s-a)(s-b)(s-c)}$} \put(3.5,0.4){$\displaystyle s:=\frac{a+b+c}{2}$}
\end{picture}", stream, options);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
}
// Create rendering options.
MathRendererOptions options = new SvgMathRendererOptions();
// Specify the preamble.
options.Preamble = @"\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}";
// Specify the scaling factor 300%.
options.Scale = 3000;
// Specify the foreground color.
options.TextColor = System.Drawing.Color.Black;
// Specify the background color.
options.BackgroundColor = System.Drawing.Color.White;
// Specify the output stream for the log file.
options.LogStream = new System.IO.MemoryStream();
// Specify whether to show the terminal output on the console or not.
options.ShowTerminal = true;
// Create the output stream for the formula image.
using (System.IO.Stream stream = System.IO.File.Open(
System.IO.Path.Combine(RunExamples.OutputDirectory, "math-formula.svg"), System.IO.FileMode.Create))
{
// Run rendering.
System.Drawing.SizeF size = new SvgMathRenderer().Render(@"\begin{equation*}
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!}
\end{equation*}", stream, options);
// Show other results.
System.Console.Out.WriteLine(options.ErrorReport);
System.Console.Out.WriteLine();
System.Console.Out.WriteLine("Size: " + size); // Dimensions of the resulting image.
}
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify the console as output terminal.
options.TerminalOut = new OutputConsoleTerminal(); // Default value. Arbitrary assignment.
// Specify a memory terminal as output terminal, if you don't want the terminal output to be written to the console.
// options.TerminalOut = new OutputMemoryTerminal();
// Run the job.
TeXJob job = new TeXJob("hello-world", new XpsDevice(), options);
job.Run();
// For further output to look fine.
options.TerminalOut.Writer.WriteLine(); // The same as Console.Out.WriteLine();
// Initialize license object.
License license = new License();
// Set license.
license.SetLicense("D:\\Aspose.Total.NET.lic");
Console.WriteLine("License set successfully.");
// Initialize license object.
License license = new License();
// Load license in FileStream.
FileStream myStream = new FileStream("D://Aspose.Total.NET.lic", FileMode.Open);
// Set license.
license.SetLicense(myStream);
Console.WriteLine("License set successfully.");
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name. Otherwise, the first argument of the TeXJob constructor will be taken as a job name.
options.JobName = "overridden-job-name";
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Run the job.
TeXJob job = new TeXJob("hello-world", new XpsDevice(), options);
job.Run();
// Open a stream on a ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "zip-in.zip"), FileMode.Open))
// Open a stream on a ZIP archive that will serve as the output working directory.
using (Stream outZipStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "terminal-out-to-zip.zip"), FileMode.Create))
{
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name.
options.JobName = "terminal-output-to-zip";
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
// Specify a ZIP archive working directory for the output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Define the saving options.
options.SaveOptions = new PdfSaveOptions();
// Run the job.
new TeXJob("hello-world", new PdfDevice(), options).Run();
// Finalize output ZIP archive.
((OutputZipDirectory)options.OutputWorkingDirectory).Finish();
}
// Set metered public and private keys.
new Aspose.TeX.Metered().SetMeteredKey(
"<type public key here>",
"<type private key here>");
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name.
options.JobName = "stream-in-image-out";
// Specify a file system working directory for input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify the console as the input terminal.
options.TerminalIn = new InputConsoleTerminal(); // Default value. Arbitrary assignment.
// Specify the console as the output terminal.
options.TerminalOut = new OutputConsoleTerminal(); // Default value. Arbitrary assignment.
// Define the saving options.
PngSaveOptions pngSaveOptions = new PngSaveOptions();
pngSaveOptions.Resolution = 300;
options.SaveOptions = pngSaveOptions;
// Create the image device.
ImageDevice device = new ImageDevice();
// Run the job.
TeXJob job = new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(
"\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt")),
device, options);
job.Run();
// When the console prompts the input, type "ABC", press Enter, then type "\end" and press Enter again.
// For further output to look fine.
options.TerminalOut.Writer.WriteLine();
// You can alternatively get images in form of array of byte arrays.
// The first index for the page number (0-based, of course).
byte[][] result = device.Result;
// Open a stream on a ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "zip-in.zip"), FileMode.Open))
// Open a stream on a ZIP archive that will serve as the output working directory.
using (Stream outZipStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "typeset-pdf-to-external-stream.zip"), FileMode.Create))
{
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name.
options.JobName = "typeset-pdf-to-external-stream"; // does NOT define the name of the output PDF.
// Specify a ZIP archive working directory for the input.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
// Specify a ZIP archive working directory for the output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Define the saving options.
options.SaveOptions = new PdfSaveOptions();
// Open a stream to write the output PDF to.
// 1) A file somewhere on a local file system.
using (Stream stream = File.Open(Path.Combine(RunExamples.OutputDirectory, "file-name.pdf"), FileMode.Create)) // writing PDF somewhere else
// 2) A file in the output ZIP. A weird feature that extends flexibilty :)
//using (Stream stream = options.OutputWorkingDirectory.GetFile("file-name.pdf", out string fullName)) // writing PDF to the same ZIP
new TeXJob("hello-world", new PdfDevice(stream), options).Run();
// Finalize output ZIP archive.
((OutputZipDirectory)options.OutputWorkingDirectory).Finish();
}
// Create the file system input working directory.
// Create the format provider using the file system input working directory.
// We use the project output directory as our custom format file is supposed to be located there.
using (FormatProvider formatProvider =
new FormatProvider(new InputFileSystemDirectory(RunExamples.OutputDirectory), "customtex"))
{
// Create conversion options for a custom format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX(formatProvider));
options.JobName = "typeset-with-custom-format";
// Specify the input working directory. This is not required here as we are providing the main input as a stream.
// But it is required when the main input has dependencies (e.g. images).
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Run the job.
new TeXJob(new MemoryStream(Encoding.ASCII.GetBytes(
"Congratulations! You have successfully typeset this text with your own TeX format!\\end")),
new XpsDevice(), options).Run();
// For further output to look fine.
options.TerminalOut.Writer.WriteLine();
}
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a job name.
options.JobName = "external-file-stream";
// Specify a file system working directory for the input.
options.InputWorkingDirectory = new InputFileSystemDirectory(RunExamples.InputDirectory);
// Specify a file system working directory for the output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(RunExamples.OutputDirectory);
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Open the stream to write typeset XPS document. The file name is not necessarily the same as the job name.
using (Stream stream = File.Open(Path.Combine(RunExamples.OutputDirectory, options.JobName + ".xps"), FileMode.Create))
// Run the job.
new TeXJob("hello-world", new XpsDevice(stream), options).Run();
// Open the stream on the ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "zip-in.zip"), FileMode.Open))
// Open the stream on the ZIP archive that will serve as the output working directory.
using (Stream outZipStream = File.Open(Path.Combine(RunExamples.OutputDirectory, "zip-pdf-out.zip"), FileMode.Create))
{
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
// Specify a ZIP archive working directory for the output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
// Specify the console as the output terminal.
options.TerminalOut = new OutputConsoleTerminal(); // Default value. Arbitrary assignment.
// Define the saving options.
options.SaveOptions = new PdfSaveOptions();
// Run the job.
TeXJob job = new TeXJob("hello-world", new PdfDevice(), options);
job.Run();
// For further output to look fine.
options.TerminalOut.Writer.WriteLine();
// Finalize output ZIP archive.
((OutputZipDirectory)options.OutputWorkingDirectory).Finish();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment