import com.crystaldecisions.sdk.occa.report.lib.*;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ITable;
import com.crystaldecisions.sdk.occa.report.data.Tables;
import com.crystaldecisions.sdk.occa.report.exportoptions.*;
//Java imports.
import java.io.*;
import org.apache.log4j.BasicConfigurator;
public class JRCExportReport {
static final String REPORT_NAME = "C:\\ProveCR2013\\st_rpt.rpt";
//static final String REPORT_NAME = "C:\\ProveCR2013\\JRCExportReport.rpt";
static final String EXPORT_FILE = "C:\\ProveCR2013\\myExportedReport.pdf";
public static void main(String[] args) {
PropertyBag propertyBag = null;
IConnectionInfo connectionInfo = null;
// Declare variables to hold ConnectionInfo values.
// Below is the list of values required to switch to use a JDBC/JNDI
// connection
String TRUSTED_CONNECTION = "false";
String SERVER_TYPE = "JDBC (JNDI)";
String USE_JDBC = "true";
String DATABASE_DLL = "crdb_jdbc.dll";
// String JNDI_OPTIONAL_NAME = jndiName;
String CONNECTION_URL = "jdbc:odbc:nomeDBAccess";
String DATABASE_CLASS_NAME = "sun.jdbc.odbc.JdbcOdbcDriver";
String DB_USER_NAME = "user";
String DB_PASSWORD = "password";
ITable origTable = null;
ITable newTable = null;
try {
BasicConfigurator.configure();
//Open report.
ReportClientDocument reportClientDoc = new ReportClientDocument();
reportClientDoc.open(REPORT_NAME, 0);
reportClientDoc.getDatabaseController().logon("user", "password");
// Change connection information properties
connectionInfo = newTable.getConnectionInfo ();
// Set new table connection property attributes
propertyBag = new PropertyBag ();
// Overwrite any existing properties with updated values
propertyBag.put ("Trusted_Connection", TRUSTED_CONNECTION);
propertyBag.put ("Server Type", SERVER_TYPE);
propertyBag.put ("Use JDBC", USE_JDBC);
propertyBag.put ("Database DLL", DATABASE_DLL);
//propertyBag.put ("JNDIOptionalName", JNDI_OPTIONAL_NAME);
propertyBag.put ("Connection URL", CONNECTION_URL);
propertyBag.put ("Database Class Name", DATABASE_CLASS_NAME);
// propertyBag.put("Server Name", SERVER_NAME); //Optional property
// propertyBag.put("Connection String", CONNECTION_STRING); //Optional property
// propertyBag.put("Database Name", DATABASE_NAME); //Optional property
// propertyBag.put("URI", URI); //Optional property
connectionInfo.setAttributes (propertyBag);
// Set database username and password
// NOTE: Even if the username and password properties do not change
// when switching databases, the
// database password is *not* saved in the report and must be set at
// runtime if the database is secured.
connectionInfo.setUserName (DB_USER_NAME);
connectionInfo.setPassword (DB_PASSWORD);
// Update the table information
reportClientDoc.getDatabaseController ().setTableLocation (origTable, newTable);
//NOTE: If parameters or database login credentials are required, they need to be set before.
//calling the export() method of the PrintOutputController.
//Export report and obtain an input stream that can be written to disk.
//See the Java Reporting Component Developer's Guide for more information on the supported export format enumerations
//possible with the JRC.
ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream)reportClientDoc.getPrintOutputController().export(ReportExportFormat.PDF);
//Release report.
reportClientDoc.close();
//Use the Java I/O libraries to write the exported content to the file system.
byte byteArray[] = new byte[byteArrayInputStream.available()];
//Create a new file that will contain the exported result.
File file = new File(EXPORT_FILE);
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(byteArrayInputStream.available());
int x = byteArrayInputStream.read(byteArray, 0, byteArrayInputStream.available());
byteArrayOutputStream.write(byteArray, 0, x);
byteArrayOutputStream.writeTo(fileOutputStream);
//Close streams.
byteArrayInputStream.close();
byteArrayOutputStream.close();
fileOutputStream.close();
System.out.println("Successfully exported report to " + EXPORT_FILE);
}
catch(ReportSDKException ex) {
ex.printStackTrace();
}
catch(Exception ex) {
ex.printStackTrace();
}
}
}