LaTeX to XPS | Java

Another target format is XPS. An XPS file is physically a ZIP package that contains paginated content of a document, as well as the metadata required for proper display by specific viewers (such as Windows XPS Viewer) and printing. All data in a package is organized as files in directories. Some of them 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 is 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. Its purpose is control over 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 text content. All three elements have properties to define various aspects of appearence.

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 conversion to XPS is just as simple as the conversion to raster image formats, except that in addition to the SaveOptions, we have to use an instance of the XpsDevice class.

Set inputFile to the full path of a self-contained LaTeX file, such as hello-world.ltx, and outputDirectory to an existing output folder. The default output is hello-world.xps for this input. Import the classes from com.aspose.tex and com.aspose.tex.rendering used in the snippet. The calling method must handle or declare exceptions. Apply a license as described in the licensing guide to avoid evaluation restrictions. The example selects noninteractive processing and closes its output resources.

 1// Convert LaTeX to XPS using the default output filename.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 4OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 5options.setOutputWorkingDirectory(output);
 6options.setSaveOptions(new XpsSaveOptions());
 7options.setInteraction(Interaction.NonstopMode);
 8
 9XpsDevice device = new XpsDevice();
10TeXJobResult result;
11try {
12    result = new TeXJob(inputFile, device, options).run();
13} finally {
14    device.dispose();
15    output.close();
16}

An alternative way to write the main output XPS file

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

Use the same inputFile and outputDirectory values as above. Set xpsFile to a java.nio.file.Path, for example Paths.get(outputDirectory, "any-name.xps"). In addition to the Aspose.TeX classes, import java.io.OutputStream, java.nio.file.Files, java.nio.file.Path, and java.nio.file.Paths. The stream controls the XPS destination; auxiliary files still go to outputDirectory.

 1// Convert LaTeX to XPS using an explicitly selected output stream.
 2// inputFile is the full input path; outputDirectory is an existing output folder.
 3// xpsFile is the java.nio.file.Path of the XPS file to create.
 4TeXJobResult result;
 5try (OutputStream xpsStream = Files.newOutputStream(xpsFile)) {
 6    TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
 7    OutputFileSystemDirectory output = new OutputFileSystemDirectory(outputDirectory);
 8    options.setOutputWorkingDirectory(output);
 9    options.setSaveOptions(new XpsSaveOptions());
10    options.setInteraction(Interaction.NonstopMode);
11
12    XpsDevice device = new XpsDevice(xpsStream);
13    try {
14        result = new TeXJob(inputFile, device, options).run();
15    } finally {
16        device.dispose();
17        output.close();
18    }
19}

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. Here is the Java version page.