LaTeX to XPS | .NET

Another target format is XPS. An XPS file is physically a ZIP package that contains paginated content of a document, along with the metadata required for proper display by specific viewers (such as Windows XPS Viewer) and printing. All data in a package is represented by files. Some of them are binary and contain resources such as images, fonts, and ICC profiles. Others are XML files in various specific schemas. The latter include files that contain the document data itself. Document data consists of a set of files - each file contains data for an individual page of the document. Such files consist of a single page element and a tree of child elements - Canvas, Path and Glyphs. Canvas is a grouping element that can contain other Canvases, Paths and Glyphs. It serves to control the appearance of all child elements as a group. Path elements are used to define vector graphics paths. And Glyphs elements are used to define textual content. All three elements have properties to define various aspects of appearance.

There is the Aspose.Page library that provides an API for manipulating XPS documents, as well as converting them to PDF and raster image formats.

How to convert LaTeX to XPS

The LaTeX to XPS conversion is just as simple as the conversion to raster image formats, except that the SaveOptions MUST be set to an XpsSaveOptions class instance (by default or explicitly), and the device MUST be changed to an instance of the XpsDevice class.

Simplest conversion example

The example below shows how to convert a LaTeX file to XPS in a few steps:

  1. Create a TexDocument instance.
  2. Load the source .tex file using TexDocument.Load.
  3. (Optional) Configure XpsSaveOptions such as compression settings.
  4. Save the document to XPS by calling document.Save with an XpsDevice.

This code demonstrates the most direct way to produce an XPS file from LaTeX.

 1// Convert LaTeX to XPS - simplest approach
 2
 3// Create conversion options for Object LaTeX format upon Object TeX engine extension.
 4TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
 5
 6// Specify a file system working directory for the output.
 7options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
 8
 9// Initialize the options for saving in XPS format.
10options.SaveOptions = new XpsSaveOptions();
11
12// Run LaTeX to XPS conversion.
13new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new XpsDevice(), options).Run();

An alternative way to write the main output XPS file

There’s another constructor of the XpsDevice class, which lets us get the resulting XPS file in an alternative way.

Alternative conversion example

This snippet demonstrates using the XpsDevice constructor that accepts a Stream. The steps are:

  1. Create a TexDocument and load the LaTeX source.
  2. Create a MemoryStream to hold the XPS output.
  3. Instantiate XpsDevice with the stream.
  4. Save the document to the device, then you can use the stream (e.g., send it over a network or store it in a database).

This approach is useful when you need the XPS data in memory rather than as a file on disk.

 1// Convert LaTeX to XPS - alternative approach with stream
 2
 3// Create the stream to write the XPS file to.
 4using (Stream xpsStream = File.Open(Path.Combine(OutputDir, "hello-world-alt.xps"), FileMode.Create))
 5{
 6    // Create conversion options for Object LaTeX format upon Object TeX engine extension.
 7    TeXOptions options = TeXOptions.ConsoleAppOptions(TeXConfig.ObjectLaTeX);
 8    
 9    // Specify a file system working directory for the output.
10    options.OutputWorkingDirectory = new OutputFileSystemDirectory(OutputDir);
11    
12    // Initialize the options for saving in XPS format.
13    options.SaveOptions = new XpsSaveOptions(); // Default value. Arbitrary assignment.
14    
15    // Run LaTeX to XPS conversion.
16    new TeXJob(Path.Combine(DataDir, "hello-world.ltx"), new XpsDevice(xpsStream), options).Run();
17}

The effect is the same as we get here.

You may also check out the free LaTeX-to-XPS conversion web app built based on Aspose.TeX for .NET API.

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.