User defined java class


#1

How to convert image into pdf format in user defined java class.


#3

Hi Ram,

Please find the below Java code in a user-defined java class.
You can add respective jars in PS/lib folder. It will work.

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.Image;

public boolean processRow(StepMetaInterface smi, StepDataInterface sdi) throws ProcessStudioException {
if (first) {
first = false;

/* TODO: Your code here. (Using info fields)

FieldHelper infoField = get(Fields.Info, "info_field_name");

RowSet infoStream = findInfoRowSet("info_stream_tag");

Object[] infoRow = null;

int infoRowCount = 0;

// Read all rows from info step before calling getRow() method, which returns first row from any
// input rowset. As rowMeta for info and input steps varies getRow() can lead to errors.
while((infoRow = getRowFrom(infoStream)) != null){

  // do something with info data
  infoRowCount++;
}
*/

}

Object[] r = getRow();

if (r == null) {
setOutputDone();
return false;
}

// It is always safest to call createOutputRow() to ensure that your output row’s Object[] is large
// enough to handle any new fields you are creating in this step.
r = createOutputRow(r, data.outputRowMeta.size());

/* TODO: Your code here. (See Sample)

// Get the value from an input field
String foobar = get(Fields.In, “a_fieldname”).getString®;

foobar += “bar”;

// Set a value in a new output field
get(Fields.Out, “output_fieldname”).setValue(r, foobar);

*/
// Send the row on to the next step.

Document document = new Document();
String input = “C://Users//Admin//Documents//imagetopdf.jpeg”; //
String output = “C://Users//Admin//Documents//imagetopdf.pdf”;
try {
FileOutputStream fos = new FileOutputStream(output);
PdfWriter writer = PdfWriter.getInstance(document, fos);
writer.open();
document.open();
document.add(Image.getInstance(input));
document.close();
writer.close();
}
catch (Exception e) {
e.printStackTrace();
}

putRow(data.outputRowMeta, r);

return true;
}