Convert LaTeX to PDF in C#

A TeX file may contain text, symbols, or arithmetic expressions. These are used for typesetting the documents and you can typeset TeX input to PDF, XPS, and several other supported formats. In this article, you will be learning how to convert LaTeX to PDF in C# programmatically. The following sections explain the topic in detail:

LaTeX to PDF API Installation

Aspose.TeX for .NET API has been designed for typesetting TeX files to different file formats like PDF, XPS, or images. You can quickly configure the API by downloading the DLL file from the Downloads section, or you can install it from NuGet with the following installation command:

PM> Install-Package Aspose.TeX
latex to pdf api Installation

Convert LaTeX to PDF in C#

You can convert TeX to PDF file with the steps below:

  1. Create typesetting options for default ObjectTeX format.
  2. Specify console or memory stream as an output terminal.
  3. Set options for rendering into PDF format.
  4. Call TexJob class constructor and save output PDF with PdfDevice class object.

The following code snippet explains how to convert TeX to PDF file programmatically using C#:

String dataDir = @"D:\test\";
// Create typesetting options for default ObjectTeX format.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a file system working directory for input.
options.InputWorkingDirectory = new InputFileSystemDirectory(dataDir);
// Specify a file system working directory for output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(dataDir);
// Specify memory stream as output terminal.
options.TerminalOut = new OutputMemoryTerminal();
// Set options for rendering into PDF format.
options.SaveOptions = new PdfSaveOptions();
// Run typesetting.
new TeXJob("hello-world", new PdfDevice(), options);

The arguments passed to the TexJob class constructor are noteworthy here. The first one refers to the path to the input TeX file, the second is Device type while the third one refers to TexOptions.

Convert TeX ZIP Directory to PDF ZIP Directory Conversion in C#

You may need to convert multiple TeX files to PDF at a time. In such cases, you may convert TeX file in a ZIP archive to PDF document in zipped form. You need to follow the steps below:

  1. Open streams on ZIP archives as the input and output working directory.
  2. Create typesetting options using the TeXOptions class object.
  3. Create and specify saving options with PdfSaveOptions.
  4. Run typesetting with TexJob class constructor.

The code below elaborates on how to convert TeX ZIP archive to PDF ZIP archive programmatically using C#:

String dataDir = @"D:\test\";
// Open a stream on a ZIP archive that will serve as the input working directory.
using (Stream inZipStream = File.Open(Path.Combine(dataDir, "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(dataDir, "terminal-out-to-zip.zip"), FileMode.Create))
{
// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify the job name.
options.JobName = "terminal-output-to-zip";
// Specify a ZIP archive working directory for input.
options.InputWorkingDirectory = new InputZipDirectory(inZipStream, "in");
// Specify a ZIP archive working directory for output.
options.OutputWorkingDirectory = new OutputZipDirectory(outZipStream);
// Specify that the terminal output must be written to a file in the output working directory.
options.TerminalOut = new OutputFileTerminal(options.OutputWorkingDirectory);
// Create and specify saving options.
options.SaveOptions = new PdfSaveOptions();
// Run typesetting.
new TeXJob("hello-world", new PdfDevice(), options);
// Finalize output ZIP archive.
((OutputZipDirectory)options.OutputWorkingDirectory).Finish();
}

LaTex to PDF Converter - Online Tool

This online LaTeX to PDF converter is powered by Aspose.TeX for .NET.

latex to pdf converter

Convert TeX (LaTeX) to XPS File Programmatically in C#

You can convert TeX to XPS file with the following steps:

  1. Create typesetting options on ObjectTeX engine extension.
  2. Specify a file system working directory for input and output.
  3. Convert TeX to XPS by running Typesetting with XpsDevice.

The following code shows how to convert TeX file to XPS programmatically using C#:

String dataDir = @"D:\test\";
// Create typesetting options for default ObjectTeX format on ObjectTeX engine extension.
TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectTeX());
// Specify a file system working directory for input.
options.InputWorkingDirectory = new InputFileSystemDirectory(dataDir);
// Specify a file system working directory for output.
options.OutputWorkingDirectory = new OutputFileSystemDirectory(dataDir);
// Specify console as output terminal.
options.TerminalOut = new OutputConsoleTerminal();
// Run Typesetting
new TeXJob("hello-world", new XpsDevice(), options);

Get Free API License

You can evaluate the API with full access by requesting a Free Temporary License.

Conclusion

In this article, you have explored how to convert LaTeX to PDF in C#. Moreover, you have also learned how to convert a TeX file into a ZIP archive and create an output ZIP directory. You can have a look at several other features by visiting the API Documentation. Please feel free to contact us at the Free Support Forum in case of any concerns.

See Also