Java Question Problem with the development of libraries to generate PDFs from CrystalReports

enrfortin

Member
Licensed User
I've been trying to add the functionality of passing parameters to the jCrystalReports library
but once I compile it with SLC it shows me some errors so I have to add some packages


B4X:
    #AdditionalJar: CrystalReportsRuntime.jar
    #AdditionalJar: log4j.jar
    #AdditionalJar: logging.jar

it works but when I run it, it displays other errors.

here I leave the code as I have it modified with a parameter



B4X:
public void LaunchReportWithParameters() {

 
    BasicConfigurator.configure();
    Logger.getRootLogger().setLevel(Level.ERROR);
    try {
      if (REPORT_NAME != "" && USERNAME != "" && SERVER_NAME != "" && CONN_URL != "" && DB_NAME != "" && DB_CLASS_NAME != "") {
        ReportClientDocument reportClientDoc = new ReportClientDocument();
        reportClientDoc.open(REPORT_NAME, 0);
        reportClientDoc.getDatabaseController().logon(getUSERNAME(), getPASSWORD());
        reportClientDoc.getDataDefController().getRecordFilterController().setFormulaText(SELECTION_FORMULA);
         ParameterFieldController parameterFieldController = reportClientDoc.getDataDefController().getParameterFieldController();
        parameterFieldController.setCurrentValue("", "Prefijo", new String ("HN"));
        ConnectionInfo connectionInfo1 = new ConnectionInfo();
        ConnectionInfo connectionInfo2 = new ConnectionInfo();
        DatabaseController dbController = reportClientDoc.getDatabaseController();
        IConnectionInfo iConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);
        Fields pFields = null;
        if (TRUSTED_CONN != "true")
          TRUSTED_CONN = "false";
        PropertyBag pb = new PropertyBag();
        pb.put("JDBC Connection String", "!" + getDB_CLASS_NAME() + "!" + getCONN_URL());
        pb.put("Trusted_Connection", getTRUSTED_CONN());
        pb.put("PreQEServerName", getCONN_URL());
        pb.put("Server Type", "JDBC (JNDI)");
        pb.put("Database DLL", "crdb_jdbc.dll");
        pb.put("Database", getDB_NAME());
        pb.put("Database Class Name", getDB_CLASS_NAME());
        pb.put("Use JDBC", "true");
        pb.put("Database Name", getDB_NAME());
        pb.put("Server Name", getCONN_URL());
        pb.put("Connection URL", getCONN_URL());
        pb.put("Server", getSERVER_NAME());
        connectionInfo2.setAttributes(pb);
        connectionInfo2.setUserName(getUSERNAME());
        connectionInfo2.setPassword(getPASSWORD());
        connectionInfo2.setKind(ConnectionInfoKind.SQL);
        int replaceParams = 4;
        dbController.replaceConnection(iConnectionInfo, (IConnectionInfo)connectionInfo2, pFields, replaceParams);
      } else {
        System.out.println("Some fields were not filled. REPORT_NAME, USERNAME, SERVER_NAME, CONN_URL, DB_NAME and DB_CLASS_NAME are required.");
      }
    } catch (ReportSDKException ex) {
      System.out.println(ex);
    } catch (Exception ex) {
      System.out.println(ex);
    }
  }

I have even tried to replicate the library by just copying the code without modifying it and still I can't run it.

EDIT:

If anyone else is interested, I solved this problem by adding java code to a new class

and making use of this function with "RuntMethod" I am attaching the compiled library for the use of

exporting Crystal Reports reports with parameters

and I also leave here the code of the class in case someone needs to add more parameters or any other thing.

How to use?

create a variable with the instance of CristalReportsB4j

and then call the GenerateOT() function with the following parameters

GenerarOT(REPORT_NAME As String, USERNAME As String, PASSWORD As String, SERVER_NAME As String, _
CONN_URL As String, DB_NAME As String, DB_CLASS_NAME As String, SELECTION_FORMULA As String, PARAM_NAME As String, _
PARAM_VALUE As String, TRUSTED_CONN As String, RUTA) where RUTA is the location where the pdf is to be saved exp.

B4X:
Dim cr As CrystalReportsB4J
    cr.Initialize
    cr.GenerarOT("C:\Users\Macbook\Desktop\LIBRERIAS EDITANDO\jCrystalReports\ReportesHN\rptOrdenTrabajo.rpt", "YourUser","SecretPassword","HN-VSR-SQL01", "jdbc:jtds:sqlserver://192.168.1.92:1433/DTKDBHN","DTKDBHN","net.sourceforge.jtds.jdbc.Driver","937690","Prefijo","HN","false", "DetektorOT-"&"937690"&".pdf")

Remember that you need to have both JcrystalReports libraries or create a class with the following code
1719493670332.png


B4X:
Sub Class_Globals


End Sub

Public Sub GenerarOT(REPORT_NAME As String, USERNAME As String, PASSWORD As String, SERVER_NAME As String, _
    CONN_URL As String,  DB_NAME As String, DB_CLASS_NAME As String,  SELECTION_FORMULA As String, PARAM_NAME As String, _
    PARAM_VALUE As String, TRUSTED_CONN As String, RUTA)
  
    Dim reportFormat As JavaObject
    reportFormat.InitializeStatic("com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat")
    Dim pdfFormat As Object = reportFormat.GetField("PDF")
  
  
    Me.as(JavaObject).RunMethod("ExportReport", Array(pdfFormat, RUTA ,REPORT_NAME,USERNAME,PASSWORD,SERVER_NAME,CONN_URL,DB_NAME,DB_CLASS_NAME,"{OrdenTrabajo.OrdenTrabajoID}="&SELECTION_FORMULA,PARAM_NAME,PARAM_VALUE, TRUSTED_CONN))
End Sub


Public Sub Initialize
End Sub

#if java
import java.util.*;
import anywheresoftware.b4a.AbsObjectWrapper;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.Author;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import com.crystaldecisions.sdk.occa.report.application.DatabaseController;
import com.crystaldecisions.sdk.occa.report.application.ReportClientDocument;
import com.crystaldecisions.sdk.occa.report.application.ParameterFieldController;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfo;
import com.crystaldecisions.sdk.occa.report.data.ConnectionInfoKind;
import com.crystaldecisions.sdk.occa.report.data.Fields;
import com.crystaldecisions.sdk.occa.report.data.IConnectionInfo;
import com.crystaldecisions.sdk.occa.report.document.PaperSize;
import com.crystaldecisions.sdk.occa.report.document.PaperSource;
import com.crystaldecisions.sdk.occa.report.document.PrintReportOptions;
import com.crystaldecisions.sdk.occa.report.document.PrinterDuplex;
import com.crystaldecisions.sdk.occa.report.exportoptions.ReportExportFormat;
import com.crystaldecisions.sdk.occa.report.lib.PropertyBag;
import com.crystaldecisions.sdk.occa.report.lib.ReportSDKException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
 public PaperSize PAPER_10X14 = PaperSize.paper10x14;
 
  public PaperSize PAPER_11X17 = PaperSize.paper11x17;
 
  public PaperSize PAPER_A3 = PaperSize.paperA3;
 
  public PaperSize PAPER_A4 = PaperSize.paperA4;
 
  public PaperSize PAPER_A4_SMALL = PaperSize.paperA4Small;
 
  public PaperSize PAPER_A5 = PaperSize.paperA5;
 
  public PaperSize PAPER_B4 = PaperSize.paperB4;
 
  public PaperSize PAPER_B5 = PaperSize.paperB5;
 
  public PaperSize PAPER_C_SHEET = PaperSize.paperCsheet;
 
  public PaperSize PAPER_D_SHEET = PaperSize.paperDsheet;
 
  public PaperSize PAPER_E_SHEET = PaperSize.paperEsheet;
 
  public PaperSize PAPER_ENVELOPE_9 = PaperSize.paperEnvelope9;
 
  public PaperSize PAPER_ENVELOPE_10 = PaperSize.paperEnvelope10;
 
  public PaperSize PAPER_ENVELOPE_11 = PaperSize.paperEnvelope11;
 
  public PaperSize PAPER_ENVELOPE_12 = PaperSize.paperEnvelope12;
 
  public PaperSize PAPER_ENVELOPE_14 = PaperSize.paperEnvelope14;
 
  public PaperSize PAPER_ENVELOPE_B4 = PaperSize.paperEnvelopeB4;
 
  public PaperSize PAPER_ENVELOPE_B5 = PaperSize.paperEnvelopeB5;
 
  public PaperSize PAPER_ENVELOPE_B6 = PaperSize.paperEnvelopeB6;
 
  public PaperSize PAPER_ENVELOPE_C3 = PaperSize.paperEnvelopeC3;
 
  public PaperSize PAPER_ENVELOPE_C4 = PaperSize.paperEnvelopeC4;
 
  public PaperSize PAPER_ENVELOPE_C5 = PaperSize.paperEnvelopeC5;
 
  public PaperSize PAPER_ENVELOPE_C6 = PaperSize.paperEnvelopeC6;
 
  public PaperSize PAPER_ENVELOPE_C65 = PaperSize.paperEnvelopeC65;
 
  public PaperSize PAPER_ENVELOPE_DL = PaperSize.paperEnvelopeDL;
 
  public PaperSize PAPER_ENVELOPE_ITALY = PaperSize.paperEnvelopeItaly;
 
  public PaperSize PAPER_ENVELOPE_MONARCH = PaperSize.paperEnvelopeMonarch;
 
  public PaperSize PAPER_ENVELOPE_PERSONAL = PaperSize.paperEnvelopePersonal;
 
  public PaperSize PAPER_EXECUTIVE = PaperSize.paperExecutive;
 
  public PaperSize PAPER_FANFOLD_LEGAL_GERMAN = PaperSize.paperFanfoldLegalGerman;
 
  public PaperSize PAPER_FANFOLD_STD_GERMAN = PaperSize.paperFanfoldStdGerman;
 
  public PaperSize PAPER_FANFOLD_US = PaperSize.paperFanfoldUS;
 
  public PaperSize PAPER_FOLIO = PaperSize.paperFolio;
 
  public PaperSize PAPER_LEDGER = PaperSize.paperLedger;
 
  public PaperSize PAPER_LEGAL = PaperSize.paperLegal;
 
  public PaperSize PAPER_LETTER = PaperSize.paperLetter;
 
  public PaperSize PAPER_LETTER_SMALL = PaperSize.paperLetterSmall;
 
  public PaperSize PAPER_NOTE = PaperSize.paperNote;
 
  public PaperSize PAPER_QUARTO = PaperSize.paperQuarto;
 
  public PaperSize PAPER_STATEMENT = PaperSize.paperStatement;
 
  public PaperSize PAPER_TABLOID = PaperSize.paperTabloid;
 
  public ReportExportFormat format_PDF = ReportExportFormat.PDF;
 
  public ReportExportFormat format_CHARACTER_SEPARATED_VALUES = ReportExportFormat.characterSeparatedValues;
 
  public ReportExportFormat format_EDITABLE_RTF = ReportExportFormat.editableRTF;
 
  public ReportExportFormat format_MSEXCEL = ReportExportFormat.MSExcel;
 
  public ReportExportFormat format_MSWORD = ReportExportFormat.MSWord;
 
  public ReportExportFormat format_RTF = ReportExportFormat.RTF;
 
  public ReportExportFormat format_TAB_SEPARATED_TEXT = ReportExportFormat.tabSeparatedText;
 
  public ReportExportFormat format_TEXT = ReportExportFormat.text;
 
  public ReportExportFormat format_XML = ReportExportFormat.XML;
 
  public ReportExportFormat format_RECORD_TO_MSEXCEL = ReportExportFormat.recordToMSExcel;
 
  private PrintService[] printServices = null;
 
  private PrintService printService = null;

    // Agrega los métodos getters y setters si es necesario

public void ExportReport(ReportExportFormat repExpFrmt, String exportFileName, String REPORT_NAME, String USERNAME, String PASSWORD, String SERVER_NAME, String CONN_URL, String DB_NAME, String DB_CLASS_NAME, String SELECTION_FORMULA, String PARAM_NAME, String PARAM_VALUE, String TRUSTED_CONN) {
    BasicConfigurator.configure();
    Logger.getRootLogger().setLevel(Level.ERROR);
    try {
        if (!REPORT_NAME.isEmpty() && !USERNAME.isEmpty() && !SERVER_NAME.isEmpty() && !CONN_URL.isEmpty() && !DB_NAME.isEmpty() && !DB_CLASS_NAME.isEmpty()) {
            ReportClientDocument reportClientDoc = new ReportClientDocument();
            reportClientDoc.open(REPORT_NAME, 0);

            // Logon to the database
            reportClientDoc.getDatabaseController().logon(USERNAME, PASSWORD);
            reportClientDoc.getDataDefController().getRecordFilterController().setFormulaText(SELECTION_FORMULA);
            ConnectionInfo connectionInfo2 = new ConnectionInfo();
            DatabaseController dbController = reportClientDoc.getDatabaseController();
            IConnectionInfo iConnectionInfo = dbController.getConnectionInfos(null).getConnectionInfo(0);
            Fields pFields = null;
            BA.Log("En la funcion que genera el pdf");
            PropertyBag pb = new PropertyBag();
            pb.put("JDBC Connection String", "!" + DB_CLASS_NAME + "!" + CONN_URL);
            pb.put("Trusted_Connection", TRUSTED_CONN);
            pb.put("PreQEServerName", CONN_URL);
            pb.put("Server Type", "JDBC (JNDI)");
            pb.put("Database DLL", "crdb_jdbc.dll");
            pb.put("Database", DB_NAME);
            pb.put("Database Class Name", DB_CLASS_NAME);
            pb.put("Use JDBC", "true");
            pb.put("Database Name", DB_NAME);
            pb.put("Server Name", CONN_URL);
            pb.put("Connection URL", CONN_URL);
            pb.put("Server", SERVER_NAME);

            connectionInfo2.setAttributes(pb);
            connectionInfo2.setUserName(USERNAME);
            connectionInfo2.setPassword(PASSWORD);
            connectionInfo2.setKind(ConnectionInfoKind.SQL);

            dbController.replaceConnection(iConnectionInfo, connectionInfo2, pFields, 4);

            // Set the parameter values
            ParameterFieldController paramFieldController = reportClientDoc.getDataDefController().getParameterFieldController();
            paramFieldController.setCurrentValue("", PARAM_NAME, PARAM_VALUE);

            ByteArrayInputStream byteArrayInputStream = (ByteArrayInputStream) reportClientDoc.getPrintOutputController().export(repExpFrmt);
            reportClientDoc.close();
            byte[] byteArray = new byte[byteArrayInputStream.available()];
            File file = new File(exportFileName);
            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);
            byteArrayInputStream.close();
            byteArrayOutputStream.close();
            fileOutputStream.close();
        } else {
            System.out.println("Some fields were not filled. REPORT_NAME, USERNAME, SERVER_NAME, CONN_URL, DB_NAME and DB_CLASS_NAME are required.");
        }
    } catch (ReportSDKException ex) {
        System.out.println(ex);
    } catch (Exception ex) {
        System.out.println(ex);
    }
}



#End If
 

Attachments

  • CrystalReportsB4j.jar
    6.7 KB · Views: 88
  • CrystalReportsB4j.xml
    3.5 KB · Views: 87
Last edited:
Top