CIB pdf toolbox technical guide (EN)
18. Quick start
18.3. Integration of the CIB pdf toolbox - Java code example
The following examples demonstrate the control of the CIB pdf toolbox under JAVA.
Example for PDF integration from RTF via CIB pdf/joinExample with CIB pdf/merge and CIB pdf/join
JAVA example: Direct printing of a PDF document via CIB runshell and CIB pdf toolbox
Example CIB pdf toolbox direct printing of a PDF document via the Java wrapper (JCoMod)
Example for PDF integration from RTF via CIB pdf/join
If you are using a dynamic RTF document project that also integrates PDF files into the target document, the following procedure is usually provided:
- Via CIB merge, the dynamic document project is first prepared to an RTF result file with the participation of CSV and/or XML data supply.
- The CIB format/pdf component creates a PDF intermediate result file from the RTF result file. This file now contains XMP metadata that provide further instructions for the processing step with the CIB pdf toolbox.
- The CIB pdf toolbox now creates a PDF result file from the intermediate result file
he following JAVA example only describes this last step.
package com.cib.comod.test; import com.cib.comod.jobs.ICibPdfJoinJob; /** • Example implementation for using the JCoMod Wrapper with CIB pdf toolbox • Example: • 1) Takes a PDF intermediate result file and outputs it as PDF result file. • 2) XMP metadata contained internally in the PDF is processed. * */ public boolean doPdfJoinJob(String a_Input, String a_Output, String a_Workspace) { JCibPdfJoinJob t_PdfJob = new JCibPdfJoinJob(); t_PdfJob.initialize(); if (!t_PdfJob.isInitialized()) { System.out.println(„Error initializing the JCibPdfJoinJob“); t_PdfJob.dispose(); // Release job resources return false; } //t_PdfJob.setProperty(ICibPdfJoinJob.PROPERTY_LICENSEKEY, “...”); //t_PdfJob.setProperty(ICibPdfJoinJob.PROPERTY_LICENSECOMPANY, “...”); //PDF intermediate result from CIB format/pdf conversion //must contain the attached PDF file as XMP Metainfo t_PdfJob.setProperty(ICibPdfJoinJob.PROPERTY_INPUTFILE, „input.pdf“); //PDF result document to be generated t_PdfJob.setProperty(ICibPdfJoinJob.PROPERTY_OUTPUTFILE, „output.pdf“); //Execute Job t_PdfJob.execute(); //Error handling int t_Error = ((Integer)t_PdfJob.getProperty(ICibPdfJoinJob.PROPERTY_ERROR)).intValue(); if (t_Error != 0) { // Error while executing the job String t_Errortext = (String) t_PdfJob.getProperty(ICibPdfJoinJob.PROPERTY_ERRORTEXT); System.out.println(„execution error: „+t_Error+“ „+t_Errortext); t_PdfJob.dispose(); // Release job resources return false; } t_PdfJob.dispose(); // Release job resources return true; }
Example with CIB pdf/merge and CIB pdf/join
The following Java code must be inserted into a "PdfToolboxSample" Java class. In addition, the Java JCoMod Wrapper (at least version 2.0.20) is required, which encapsulates the CIB pdf toolbox native library for Java via JNI, thus enabling simple control from Java.
The wrapper consists of these files:
- CoModJobs.jar
- JCoMod.dll
To compile the example, the CoModJobs.jar must be specified in the classpath. At the time of development a JDK 1.4.1 was installed.
The files used in the code sample are matched to the samples included in the scope of delivery, thus ensuring immediate functionality for test purposes.
import java.awt.Color; import java.awt.event.ActionEvent; import javax.swing.AbstractAction; import javax.swing.Action; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.JRadioButton; import javax.swing.JScrollPane; import javax.swing.JTextField; import com.cib.comod.jobs.ICibPdfJoinJob; import com.cib.comod.jobs.ICibPdfMergeJob; import com.cib.comod.jobs.IComodJob; import com.cib.comod.jobs.JCibPdfJoinJob; import com.cib.comod.jobs.JCibPdfMergeJob; public class PdfToolboxSample extends JFrame { JTextField TextPdf1; JTextField TextPdf2; JTextField TextOut; JTextField TextData; JList FieldList; JRadioButton RadioJoin; JRadioButton RadioMerge; public PdfToolboxSample() { makeFrame(); } public void makeFrame() { //Create window setTitle(“PdfToolboxSample”); setSize(400,400); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Create controls RadioMerge = new JRadioButton(new RadioMergeAction()); RadioJoin = new JRadioButton(new RadioJoinAction()); TextPdf1 = new JTextField(“..\\merge xml\\test.pdf”); TextPdf2 = new JTextField(“..\\join\\test1.pdf;..\\join\\functiontest1.pdf;..\\join\\functiontest2.pdf”); TextData = new JTextField(“XML:..\\merge xml\\data.xml;//Testdata//Person”); TextOut = new JTextField(“..\\tempoutput\\Output.pdf”); //Create labels JLabel LabelPdf1=new JLabel(„Pdf file 1:“); JLabel LabelPdf2=new JLabel(“Pdf file 2:”); JLabel LabelData=new JLabel(“Data file:”); JLabel LabelOut=new JLabel(“Output file:”); JLabel LabelFields=new JLabel(“Pdf Fields:”); //set the right dimensions RadioJoin.setSize(70,25); RadioMerge.setSize(70,25); LabelPdf1.setSize(70,25); TextPdf1.setSize(280,25); LabelPdf2.setSize(70,25); TextPdf2.setSize(280,25); LabelData.setSize(50,25); TextData.setSize(280,25); LabelOut.setSize(70,25); TextOut.setSize(280,25); LabelFields.setSize(70,25); //set the right positions RadioJoin.setLocation(100,30); RadioMerge.setLocation(180,30); LabelPdf1.setLocation(20,60); TextPdf1.setLocation(100,60); LabelPdf2.setLocation(20,90); TextPdf2.setLocation(100,90); LabelData.setLocation(20,120); TextData.setLocation(100,120); LabelOut.setLocation(20,150); TextOut.setLocation(100,150); LabelFields.setLocation(20,180); FieldList = new JList(); JScrollPane FieldsPane = new JScrollPane(FieldList); FieldsPane.setVerticalScrollBarPolicy (JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); FieldsPane.setSize(280,100); FieldsPane.setLocation(100,180); //create a button to execute join and merge JButton ButtonExecute = new JButton(new ExecuteAction()); ButtonExecute.setSize(100,25); ButtonExecute.setLocation(100,320); //put everything on the window getContentPane().setLayout(null); getContentPane().add(RadioMerge); getContentPane().add(RadioJoin); getContentPane().add(ButtonExecute); getContentPane().add(LabelPdf1); getContentPane().add(TextPdf1); getContentPane().add(LabelPdf2); getContentPane().add(TextPdf2); getContentPane().add(LabelData); getContentPane().add(TextData); getContentPane().add(LabelOut); getContentPane().add(TextOut); getContentPane().add(LabelFields); getContentPane().add(FieldsPane); //Preset and display RadioJoin.setSelected(true); enableFields(); setVisible(true); } public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); new PdfToolboxSample(); } class ExecuteAction extends AbstractAction { public ExecuteAction() { putValue(Action.NAME, “Execute”);} public void actionPerformed(ActionEvent e) { execute();} } class RadioJoinAction extends AbstractAction { public RadioJoinAction() { putValue(Action.NAME, “Join”);} public void actionPerformed(ActionEvent e) { RadioMerge.setSelected(false); enableFields();} } class RadioMergeAction extends AbstractAction { public RadioMergeAction() { putValue(Action.NAME, “Merge”);} public void actionPerformed(ActionEvent e) { RadioJoin.setSelected(false); enableFields();} } //Plausibility check of the GUI public void enableFields() { if(RadioJoin == null) return; if(RadioJoin.isSelected()) { TextPdf2.setEnabled(true); TextPdf2.setBackground(Color.white); TextData.setEnabled(false); TextData.setBackground(PdfToolboxSample.this.getBackground()); } else { TextPdf2.setEnabled(false); TextPdf2.setBackground(PdfToolboxSample.this.getBackground()); TextData.setEnabled(true); TextData.setBackground(Color.white); } } //execute Job public void execute() { //choose a Job if(RadioJoin.isEnabled()) doJoin(); if(RadioMerge.isEnabled()) doMerge(); } public void doMerge() { //create a JCibPdfMerge-Job JCibPdfMergeJob t_Job = new JCibPdfMergeJob(); // The job must be initialized, otherwise nothing runs if (!t_Job.initialize()) { System.out.println(„Error while initializing the job“); return; } // set Properties t_Job.setProperty(ICibPdfMergeJob.PROPERTY_INPUTFILE, TextPdf1.getText()); t_Job.setProperty(“Data”, TextData.getText()); t_Job.setProperty(ICibPdfMergeJob.PROPERTY_OUTPUTFILE, TextOut.getText()); t_Job.setProperty(“GetFieldInfo”, “1”); // Execute Merge Job t_Job.execute(); //Evaluate FieldInfos String t_FieldNames = (String) t_Job.getProperty(“FieldNames”); //FieldNames sind ;-separiert if (t_FieldNames != null) { String t_FieldNamesArray[] = t_FieldNames.split(“;”); FieldList.setListData(t_FieldNamesArray); } //Error handling System.out.println(“execute() beendet: “); if (((Integer)t_Job.getProperty(IComodJob.PROPERTY_ERROR)).intValue()>0) System.out.println(“Error while executing: “+t_Job.getProperty(IComodJob.PROPERTY_ERROR)+” “+t_Job.getProperty(IComodJob.PROPERTY_ERRORTEXT)); else System.out.println(„Success\n“); System.out.println(„closed Merge Job\n“); } public void doJoin() { //creates a JCibPdfJoin-Job JCibPdfJoinJob t_Job = new JCibPdfJoinJob(); // The job must be initialized, otherwise nothing runs if (!t_Job.initialize()) { System.out.println(„Error initializing the Job“); return; } // assemble the PDFs to be joined as ;-separated list String t_String = TextPdf1.getText(); if (TextPdf2.getText().toString().length()>0) t_String += „;“; t_String += TextPdf2.getText(); // set Properties t_Job.setProperty(ICibPdfJoinJob.PROPERTY_INPUTFILE, t_String); t_Job.setProperty(ICibPdfJoinJob.PROPERTY_OUTPUTFILE, TextOut.getText()); t_Job.setProperty(“GetFieldInfo”, “1”); // execute Join Job t_Job.execute(); //Evaluate FieldInfos String t_FieldNames = (String) t_Job.getProperty(“FieldNames”); //FieldNames are ;-separated if (t_FieldNames != null) { String t_FieldNamesArray[] = t_FieldNames.split(“;”); FieldList.setListData(t_FieldNamesArray); } //Error handling System.out.println(“execute() beendet: “); if (((Integer)t_Job.getProperty(IComodJob.PROPERTY_ERROR)).intValue()>0) System.out.println(“Error while executing: “+t_Job.getProperty(IComodJob.PROPERTY_ERROR)+” “+t_Job.getProperty(IComodJob.PROPERTY_ERRORTEXT)); else System.out.println(„Success\n“); System.out.println(„Join Job terminated\n“); } }
JAVA example: Direct printing of a PDF document via CIB runshell and CIB pdf toolbox
To execute this example two additional classes are required: RunshellProcess.java and Process.java . You can get them from the CIB Support.
import java.io.*; import de.cib.comod.RunshellProcess; public class DirectPrintPdf { public static void main(String[] args) { //Set PDF file e.g. a test.pdf String inputFileName = new String(“..\\Test.pdf”); // Start a new runshell process RunshellProcess cibRunShell = new RunshellProcess(“cibrsh.exe”); //Set printer names e.g. HP LaserJet 4100 Series PS cibRunShell.addArgument(„PrinterName = \\\““ + „HP LaserJet 4100 Series PS“ + „\\\““); //Set output format cibRunShell.addProperty(„OutputFormat“, „FormatPrinter“); //Set input file cibRunShell.addCommand(RunshellProcess.COMMAND_PDFJOIN, inputFileName); try { //Specify path to the libraries and start process runshell.executeAndWait(new File(“.”)); } catch(IOException io) { io.printStackTrace(); } catch(InterruptedException inter) { inter.printStackTrace(); } }
Example CIB pdf toolbox direct printing of a PDF document via the Java wrapper (JCoMod)
import java.io.*; import com.cib.comod.jobs.*; public class DirectPrintPdf { public static void main(String[] args) { JCibPdfJoinJob t_PDFJob = new JCibPdfJoinJob(); t_PDFJob.initialize(); if (!t_PDFJob.isInitialized()) { System.out.println(„Error while initializing the Dll“); return; } //Set PDF file e.g. a test.pdf t_PDFJob.setProperty(ICibPdfJoinJob.PROPERTY_INPUTFILE, “Test.pdf”); //Set printer names e.g. HP LaserJet 4100 Series PS t_PDFJob.setProperty(„PrinterName“, „HP LaserJet 4100 Series PS“ ); //Set output format t_PDFJob.setProperty(ICibPdfJoinJob.PROPERTY_OUTPUTFORMAT, “FormatPrinter”); //Start process t_PDFJob.execute(); int t_ReturnValue =(Integer)t_PDFJob.getProperty(ICibPdfJoinJob.PROPERTY_ERROR)).intValue(); if (t_ReturnValue != 0) { System.out.println(“Error! CIB pdf toolbox returns “ + t_ReturnValue); } t_PDFJob.dispose(); }