Convert PS, EPS, and XPS

PS, EPS, and XPS converter API Solution for Java

 

Whenever there is need to convert PostScript PS and Encapsulated PostScript EPS Files as well as XPS documents programatically, Java API can do it smoothly and converts multiples files. For PS and EPS, API supports Levels 1-3 PostScript operators and the most of the EPS header comments as well as transforms PostScript documents having maximum conformity with an exception of few fonts cases and API deals such fonts as Time New Roman.

Moreover, for XPS file transformation, API can add or remove pages, deals with canvases, paths, and glyph elements, create vector graphics shapes, and text strings, convert XPS outline items, and more.

The API solution for Java here lets you convert files of such PDL formats as PS, EPS, and XPS programmatically, but you may find useful to see and try cross-platform developed on these native APIs.

PostScript to PDF Conversion via Java.

To convert PostScript PS and Encapsulated PostScript EPS files to PDF via Java API you need to take the next steps:

  1. Load PS or EPS file using PsDocument Class .
  2. Set the PDF saving options using PdfSaveOptions Class .
  3. Use FileStream Class for output PDF file.
  4. Use PdfDevice Class having FileOutputStream object as parameter.
  5. Call the PsDocument.Save for saving into PDF the file.
Java Code to convert PS EPS to PDF
// The path to the documents directory.
String dataDir = Utils.getDataDir();
// Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");

// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;

//Initialize options object with necessary parameters.
PdfSaveOptions options = new PdfSaveOptions(suppressErrors);
// Default page size is 595x842 and it is not mandatory to set it in PdfDevice
// But if you need to specify size and image format use following line
// PdfSaveOptions options = new PdfSaveOptions(suppressErrors, new Dimension(595, 842));
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
//options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});

// Save PS document to PDF file
document.saveAsPdf(dataDir + "PStoPDF.pdf", options);

//Review errors
if (suppressErrors) {
    for (Exception ex : pdfOptions.getExceptions()) {
            System.out.println(ex.getMessage());
        }
}
 

PostScript to Images Conversion via Java.

For any EPS/PS PostScript to image converter application, the following Java code works well, so take the next steps:

  1. Initialize input stream with PS source file.
  2. Create PsDocument object with created PS input stream as a parameter
  3. Use ImageSaveOptions to specify AdditionalFontsFolder and SuppressError etc.
  4. Use ImageDevice object for specifying an image type and size if needed.
  5. Save PS/EPS file as and image with an image save options as an array of arrays of bytes. One array of bytes for one page of an input file.
Java Code for PostScript to Images Conversion
// The path to the documents directory.
String dataDir = Utils.getDataDir();
 // Initialize PS document with PostScript file
PsDocument document = new PsDocument(dataDir + "input.ps");

// If you want to convert Postscript file despite of minor errors set this flag
boolean suppressErrors = true;

// Initialize options object with necessary parameters.
ImageSaveOptions options = new ImageSaveOptions(suppressErrors);
// Default image format is PNG and it is not mandatory to set it in ImageSaveOptions.
// But if you need to specify another format use following line 
// options.setImageFormat(ImageFormat.JPEG);
// Default image size is 595x842 and it is not mandatory to set it in ImageDevice
// But if you need to specify size and image format use constructor with parameters
// ImageSaveOptions options = new ImageSaveOptions(new Dimension(595, 842), ImageFormat.JPEG, suppressErrors);
// If you want to add special folder where fonts are stored. Default fonts folder in OS is always included.
// options.setAdditionalFontsFolders(new String [] {"FONTS_FOLDER"});        

// Save PS document as images bytes arrays, one bytes array for one page of the document.
byte[][] imagesBytes = document.saveAsImage(options);

int i = 0;

for (byte [] imageBytes : imagesBytes) {
    String imagePath = dataDir + "PSToImage" + i + "." + options.getImageFormat().toString().toLowerCase();
    FileOutputStream fs = new FileOutputStream(imagePath);

    try {
        fs.write(imageBytes, 0, imageBytes.length);
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    } finally {
        fs.close();
    }
    i++;
}

//Review errors
if (suppressErrors) {
    for (Exception ex : options.getExceptions()) {
        System.out.println(ex.getMessage());
    }
}
 

Convert XPS to Images JPG, PNG, BMP via Java.

Java API deals XPS format that is used for representing page layout. In any scenario, whenever there is a need to convert XPS to images BMP, JPG, PNG, and TIFF programmatically, the following code can easily be integrated within the Java application.

  1. Use XpsDocument class to load the XPS document.
  2. Use the relevant image option class such as PngSaveOptions , JpegSaveOptions , BmpSaveOptions , TiffSaveOptions for image additional settings.
  3. Create the image device Class instance.
  4. Call the XpsDocument.save to save the converted JPEG image into ImageDevice object and then use ImageDevice for saving the image as a JPG.
Java Code for XPS to Image Conversion
// The path to the documents directory.
String pathDir = Utils.getDataDir();
// Load XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");
// Initialize options object with necessary parameters.
com.aspose.xps.rendering.PngSaveOptions options = new com.aspose.xps.rendering.PngSaveOptions();
options.setSmoothingMode(com.aspose.xps.rendering.SmoothingMode.HighQuality);
options.setResolution(300);
options.setPageNumbers(new int[] { 1, 2, 6 });

// Save XPS document as images bytes array. One bytes array for one page of every parttion in the document
byte [][][] imagesBytes = document.saveAsImage(options);

// Iterate through document partitions (fixed documents, in XPS terms)
for (int i = 0; i < imagesBytes.length; i++) {
    // Iterate through partition pages
    for (int j = 0; j < imagesBytes[i].length; j++) {
        // Initialize image output stream
        FileOutputStream imageStream = new FileOutputStream(dataDir + "XPStoPNG" + "_" + (i + 1) + "_" + (j + 1) + ".png");
        // Write image
        imageStream.write(imagesBytes[i][j], 0, imagesBytes[i][j].length);

        imageStream.close();
    }
}



FAQ

1. Can I convert Postscript with this API solution?

Aspose.Page has functionality that allows you to convert PS, XPS, and EPS files to other formats online or programmatically. If you need to transform your files instantly online you may like to use Page Description Language format files Converter cross-platform application.

2. What Page description Languages are supported by the converter?

This conversion functionality supports files with .ps, .eps, and .xps extensions. Such famous PDLs as PDF and SVG are represented as separate solutions in Aspose.products

3. Is the functionality free?

The cross-platform converters are free, when for the API solution you can get a free Trial and then buy the product if needed.

 

Convert XPS to PDF via Java.

The process of converting XPS to PDF documents programmatically is simple having high fidelity results among the input and output files.

  1. Load the file using XpsDocument class. Initialize PdfSaveOptions class object.
  2. Create PdfDevice object for rendering and lastly save the output PDF document.
Java Code for XPS to PDF Conversion
// Load input XPS document
XpsDocument document = new XpsDocument(dataDir + "input.xps");

// Initialize options object with necessary parameters
PdfSaveOptions options = new PdfSaveOptions();
options.setJpegQualityLevel(100);
options.setImageCompression(PdfImageCompression.Jpeg);
options.setTextCompression(PdfTextCompression.Flate);

// Save XPS document to output PDF file
document.saveAsPdf(dataDir + "XPStoPDF.pdf", options);
 
  

Support and Learning Resources