At the beginning you have to make sure that the "pdfreactor-wrapper.jar" is available to your project. Simply add the jar to your classpath.
You can create a new PDFreactor instance now. Therefore you should store this instance in a new variable named similar to "pdfReactor". This variable will be used later on to convert your input document to the PDF document. Please refer to the example below.
PDFreactor pdfReactor = new PDFreactor();
Before you can create a PDF document using the PDFreactor Java API you need to create the PDFreactor configuration object. All the information necessary about the resulting PDF is stored in this object.
Configuration config = new Configuration(); config.setDocument("<html><body><p>Hello World</p></body></html>"); config.setTitle("Hello World sample"); config.getViewerPreferences.add(ViewerPreferences.FIT_WINDOW); config.getViewerPreferences.add(ViewerPreferences.PAGE_MODE_USE_THUMBS); Resource userStyleSheet = new Resource(); userStyleSheet.setContent("body {" + "margin: 1cm;" + "}"); config.getUserStyleSheets.add(userStyleSheet);
The example above describes the configuration object of a very simple "Hello World" document with the title "Hello World sample", a margin of 1cm on each side and some ViewerPreferences. It is of course possible to personalize the config object as needed. For further information about possible configuration properties please refer to the PDFreactor Manual.
Only the PDF conversion is left to successfully generate a PDF document using the PDFreactor Java API. Therefore you could add something similar to the code snippet shown below to your Java class file.
try { // Render document and save result to result Result result = pdfReactor.convert(config); if (result != null) { byte[]pdf = result.getDocument(); //Save the pdf at the desired location FileOutputStream fos = new FileOutputStream( new File("MyFirstPDF.pdf")); fos.write(pdf); fos.close(); } } catch (PDFreactorWebserviceException exception) { Result result = exception.getResult(); System.err.println(result.getError(); } catch (Exception e) { }
This code snippet performs a check whether the conversion was successful or not. If the conversion was unsuccessful an appropriate error warning will be displayed. For example a missing document property in the configuration object will trigger such an error message. If the conversion was successful the resulting PDF document will be saved to the location you have specified.