PostScript (PS) acts as both a printing language and a document formatting tool, whereas PDF serves as the standard for document sharing and viewing. Switching of PS to PDF using Java will make sure that the file compatibility issue is resolved because PDF files are accessible on almost every gadget without the need for specific programs. This short tutorial describes how to convert a PS file to PDF using Java with the help of a list of steps and a sample code.
Steps to Convert from PS to PDF using Java
- Set the environment to use Aspose.PDF for Java to change PS to PDF
- Create an instance of the PsLoadOptions class
- Load the source PS file into the Document object
- Pass the PS load options object while loading the PS file
- Save the loaded document as PDF on the disk
These steps explain how to develop a PS file to PDF converter using Java. Declare an object of the PsLoadOptions class object and load the source PS file into the Document class object by applying the settings in the PsLoadOptions. Finally, save the document as a PDF file on the disk.
Code to Convert PostScript to PDF using Java
import com.aspose.pdf.*; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
// Set license for Aspose.PDF | |
License pdfLic = new License(); | |
try { | |
pdfLic.setLicense("license.lic"); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
var options = new PsLoadOptions(); | |
Document document = new Document("Sample.ps", options); | |
document.save("PSToPDF_out.pdf"); | |
} | |
} |
This code has demonstrated how to change PS format to PDF using Java. The PsLoadOptions contains multiple properties and functions that can be used for customization. You may invoke the setFontsFolders() method for setting the folders for the fonts during the conversion.
In this article, we have learned to transform a file from PS to PDF. To convert an MD file to PDF, refer to the article on How to convert MD file to PDF using Java.