Skip to Content
0
Former Member
Feb 04, 2009 at 11:43 AM

Export Report to PDF

32 Views

Hi,

I'm very new to this fourm and on a steep learning curve.

I'm querying the RAS for a report and using the reportAppFactory object and reportClientDocument.OpenDocument to open the report. I'm setting database logon to tables and any subreports. Finally I set the parameter value and then want to export the report to a bytearray in a PDF format. This last step is where I'm having problems. On the PrintOutputController.Export call I keep getting an error, "Failed to open report".

Am I going about this the wrong way? Can anyone point me in the right direction?

Many Thanks.

CrystalDecisions.CrystalReports.Engine.ReportDocument rptDocument = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
SessionMgr sessionMgr = new SessionMgr();
EnterpriseSession enterpriseSession = sessionMgr.LogonWithToken(sToken);
EnterpriseService enterpriseService = enterpriseSession.GetService("InfoStore");
InfoStore infoStore = new InfoStore(enterpriseService);

querystring = "Select SI_CUID, SI_DESCRIPTION From CI_INFOOBJECTS Where SI_PROGID='CrystalEnterprise.Report' And SI_NAME = '" + sReportName.ToString() + "'";

InfoObjects infoObjects = infoStore.Query(querystring);
InfoObject infoObject = infoObjects[1];

EnterpriseService tempService = enterpriseSession.GetService("", "RASReportFactory");
ReportAppFactory reportAppFactory;
reportAppFactory = (ReportAppFactory)tempService.Interface;

ReportClientDocument reportClientDocument = new ReportClientDocumentClass();
reportClientDocument = reportAppFactory.OpenDocument(infoObject.ID, 0);


CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo cnInfo = (CrystalDecisions.ReportAppServer.DataDefModel.ConnectionInfo)reportClientDocument.DatabaseController.GetConnectionInfos(null)[0].Clone(true);


PropertyBag propertyBag = new PropertyBagClass();
                
propertyBag.EnsureCapacity(5);
propertyBag["Database DLL"] = "crdb_odbc.dll";
propertyBag["QE_DatabaseType"] = "ODBC (RDO)";
propertyBag["QE_ServerDescription"] = "ODBC - Xtreme Sample Database 10";
propertyBag["QE_SQLDB"] = true;
propertyBag["Server Name"] = sDbDSN;
                
cnInfo.Attributes = propertyBag;
cnInfo.UserName = sDbUsername;
cnInfo.Password = sDbPassword;
cnInfo.Kind = CrConnectionInfoKindEnum.crConnectionInfoKindSQL;


foreach (CrystalDecisions.ReportAppServer.DataDefModel.Table oTable in reportClientDocument.Database.Tables)
                {
                    oTable.ConnectionInfo = cnInfo;
                }

foreach (String tempSubReportName in reportClientDocument.SubreportController.QuerySubreportNames())
                {
CrystalDecisions.ReportAppServer.DataDefModel.Database database = reportClientDocument.SubreportController.GetSubreportDatabase(tempSubReportName);

                    foreach (CrystalDecisions.ReportAppServer.DataDefModel.Table table_old in database.Tables)
                    {
CrystalDecisions.ReportAppServer.DataDefModel.Table table_new = new CrystalDecisions.ReportAppServer.DataDefModel.TableClass();

table_new.Name = table_old.Name;
table_new.ConnectionInfo = cnInfo;

reportClientDocument.SubreportController.SetTableLocation(tempSubReportName, table_old, table_new);
                    }
                }



ParameterFieldDiscreteValue paramValue = new ParameterFieldDiscreteValue();
                paramValue.Value = sReportParameter;

                Values val = new Values();
                val.Add(paramValue);

                reportClientDocument.DataDefController.ParameterFieldController.SetCurrentValue(sReportName, reportClientDocument.DataDefinition.ParameterFields[0].Name, paramValue);

                PrintOutputController rasPrintOutputController = reportClientDocument.PrintOutputController;



                if (reportClientDocument.IsOpen)
                {
                    ByteArray tempByteArray = rasPrintOutputController.Export(CrReportExportFormatEnum.crReportExportFormatPDF, 1);
                    Byte[] byteStreamOutput = tempByteArray.ByteArray;

                    reportClientDocument.Close();
                    GC.Collect();

                    Response.Clear();
                    Response.AddHeader("content-disposition", "inline;filename=untitled.pdf");
                    Response.ContentType = "application/pdf";
                    Response.BinaryWrite(byteStreamOutput);
                    Response.End();
                }