Java Node

The Java Node allows you to define your own node function with all the flexibility provided by Java programming language. This also give access to all the advanced PDF functions available in Qoppa’s jPDFProcess PDF library making it possible to inspect or manipulate the PDFs as part of the process. You can also import your own additional libraries to be used if needed.

PDF Automation Server Custom Job API

Note: Java code is limited ot the version being used by PDF Automation server. PAS comes packaged with Java 1.8.0_202.

How to use the Java Node

Defining your own custom node function will require Java programming. After placing the node, you will need to use the built in code editor to define the function of the node.

The JavaNode interface allows executing Java™ code inside of a Flow in the PDF Automation Server with process(PASContext, Message).

The Message provides access to the current Payload file content and Flow variables. The PASContext provides access to the current running process.

The Code Editor provides sample implementations of the JavaNode interface.

process(PASContext, Message)

This method is called during the Flow execution in the PDF Automation Server. If no modification needs to be done to the Payload content, the return Message array can just contain the same Message passed to this method. Otherwise, if the implementation will modify the Payload content (ie. create new data to be processed in the Flow), a new Message along with a new Payload needs to be returned instead.

In order to create a new Payload, a File is required. If the File should be deleted when the Flow completes processing of the Message, the File should be created using PASContext.createTempFile(String, String).

File output = pasContext.createTempFile("prefix", ".ext");

 // TODO - write new data to the output

 Payload payload = pasContext.createPayload(output);

 Message newMessage = message.deriveMessage(payload);

 return new Message[]{ newMessage };

Importing Your Own Jar Files

To add additional jar files for your JavaNode implementation, add all required jar files to the PDF Automation Server installation directory in the "userlib" folder.

Note: The installation directory is local to where the server is running not the PAS Manager.

Java Node Sample

Below is a small example showing how to search a PDF for text and delete that page:

/**

 *

 * @author Qoppa Software

 *

 */

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.util.Vector;

import com.qoppa.pas.api.flow.FlowException;

import com.qoppa.pas.api.flow.JavaNode;

import com.qoppa.pas.api.flow.Message;

import com.qoppa.pas.api.flow.PASContext;

import com.qoppa.pas.api.flow.Payload;

import com.qoppa.pdf.PDFException;

import com.qoppa.pdf.TextPosition;

import com.qoppa.pdfProcess.PDFDocument;

public class SearchPDFDeletePages implements JavaNode {

@Override

public Message[] process(PASContext pasContext, Message message) throws FlowException {

try {

InputStream inStream = message.getPayload().getInputStream();

 

// Create a PDF Document

PDFDocument pdfDoc = new PDFDocument(inStream, null);

 

inStream.close();

 

int pageIdx = pdfDoc.getPageCount() - 1;

 

while (pageIdx >= 0) {

// Search the page for specific text

Vector<TextPosition> textPositions = pdfDoc.getPage(pageIdx).findText("search text", false, true);

 

// If search results were found, delete the page

if (textPositions.size() > 0) {

pdfDoc.deletePage(pageIdx);

}

--pageIdx;

}

 

// Create a new Payload with the new PDFDocument

File tempFile = pasContext.createTempFile("del_pages", ".pdf");

FileOutputStream outStream = new FileOutputStream(tempFile);

pdfDoc.saveDocument(outStream);

outStream.close();

Payload payload = pasContext.createPayload(tempFile);

 

// Create a new Message with the Payload

Message returnMessage = message.deriveMessage(payload);

 

return new Message[] { returnMessage };

} catch (IOException e) {

throw new FlowException(e.getMessage(), e);

} catch (PDFException e) {

throw new FlowException(e.getMessage(), e);

}

}

}

 

Need Help? Let Us Create a Custom Job for You!

If you need help with defining your own custom process, we can do the coding for you. Please contact us using the form found on our PDF consulting page.

 

 


Qoppa Software's PDF Automation Server for Windows, Linux, Unix, and macOS

Automate PDF Document Workflows through RESTful Web Services & Folder Watching

Copyright © 2002-Present Qoppa Software. All rights reserved.