on 11-26-2015 4:15 PM
Dear All
Currently I'm upgrading the Crystal Report to Crystal Server 2013 and creating a .NET page to download the report in PDF format. I followed the sample code in http://scn.sap.com/docs/DOC-28646 , and everything is fine, I can logon to the crystal report server, get the report, set the report datasource in runtime, and view the report in CrystalReportViewer. However, when I try to export the report to PDF by calling "PrintOutputController.Export(pdfFormat)", it shows "Attempted to read or write protected memory. This is often an indication that other memory is corrupt". It looks like something wrong with the PrintOutputcontroller as the exception goes out when I comment the function call of PrintOutputController. I tried to use the export function of CrystalReportviewer but nothing happened as well.
I tried to use ReportDocument, load the .rpt file, convert to ReportClientDocument and export to PDF and it works! So I guess it shouldn't be the problem of the report. Also I use the Administrator account to logon the server so it is unlikely the permission issue.
Can anyone please help? Many thanks!
# The server is windows server 2008 64bit.
What SDK are you using?
Versions?
Without see your code when setting the export options you need to specify the disk options as well.
Did you build in x86 or x64 mode?
That error can mean a lot of things, permissions, access denied.
Wrap your code in a Try/Catch block and catch the exception and possibly the inner exception to see if it gives you any more details.
Don
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Don, thank you for your reply. I'm using BI .NET SDK and RAS SDK both in version 14. Here is my code.
SessionMgr sessionMgr = new SessionMgr();
EnterpriseSession enterpriseSession = sessionMgr.Logon("username", "password", "servername, "secEnterprise");
EnterpriseService enterpriseService = enterpriseSession.GetService("InfoStore");
InfoStore infoStore = new InfoStore(enterpriseService);
String reportName = "testReport";
String query = "Select SI_ID From CI_INFOOBJECTS Where SI_NAME = '" + reportName + "' AND SI_Instance=0";
InfoObjects infoObjects = infoStore.Query(query);
InfoObject infoObject = infoObjects[1];
enterpriseService = null;
enterpriseService = enterpriseSession.GetService("", "RASReportFactory");
ReportAppFactory reportAppFactory = (ReportAppFactory)enterpriseService.Interface;
ExportOptions exportOptions = new ExportOptions();
exportOptions.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;
exportOptions.FormatOptions = CrReportExportFormatEnum.crReportExportFormatPDF;
PDFExportFormatOptions PDFexportOptions = new PDFExportFormatOptions();
PDFexportOptions.StartPageNumber = 1;
PDFexportOptions.EndPageNumber = 2;
PDFexportOptions.CreateBookmarksFromGroupTree = true;
exportOptions.FormatOptions = PDFexportOptions;
// the problem comes here
ByteArray byteArray = doc.PrintOutputController.Export(rReportExportFormatPDF)
// if i use CrystalReportViewer.ReportSource = doc, it works fine
I wrapped the code in try catch but it still showing the "System.AccessViolationException". I'm using the default setting in Crystal Server 2013, is there particular setting I have to change to make it work?
Hi Jin,
Problem is you are not using the correct export API. What you are doing is attempting to use the POC to stream, not going to work that way.
Try this, I save it to disk but important part is the CR's ExportToStream() API:
System.IO.Stream oStream;
byte[] byteArray = null;
oStream = rpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
MemoryStream ms = new MemoryStream();
FileStream file = new FileStream(@"c:\reports\formulas.pdf", FileMode.Create, FileAccess.Write);
ms.WriteTo(file);
file.Close();
ms.Close();
byteArray = new byte[oStream.Length];
oStream.Read(byteArray, 0, Convert.ToInt32(oStream.Length - 1));
// this is used to verify the file so I saved it to disk
System.IO.File.Create(diskOpts.DiskFileName, Convert.ToInt32(oByte.Length)).Close();
System.IO.File.OpenWrite(diskOpts.DiskFileName).Write(oByte, 0, Convert.ToInt32(oByte.Length));
System.IO.File.SetAttributes(diskOpts.DiskFileName, System.IO.FileAttributes.Directory);
oStream.Close();
GC.Collect();
MessageBox.Show("Export to Stream complete", "RAS", MessageBoxButtons.OK, MessageBoxIcon.Information);
If you want to save it to disk and using the POC you can do it this way:
if (ExportTypeSelected == "crReportExportFormatPDF")
#region PDF
{
// This works do not alter
// this gets the report name and sets the export name to be the same less the extension
string outputFileName = "";
string MyRptName = rpt.FileName.ToString();
outputFileName = MyRptName.Substring(9, rpt.FileName.Length - 9);
outputFileName = outputFileName.Substring(0, (outputFileName.Length - 3)) + "pdf";
try
{
if (File.Exists(outputFileName))
{
File.Delete(outputFileName);
}
CrystalDecisions.ReportAppServer.ReportDefModel.PDFExportFormatOptions RasPDFExpOpts = new PDFExportFormatOptions();
try
{
RasPDFExpOpts = rptClientDoc.get_SavedExportOptions(CrReportExportFormatEnum.crReportExportFormatPDF);
}
catch (Exception ex)
{
btnSQLStatement.Text = "ERROR: " + ex.Message;
//return;
}
// Set them now:
//RasPDFExpOpts.CreateBookmarksFromGroupTree = false;
//RasPDFExpOpts.EndPageNumber = 1;
//RasPDFExpOpts.StartPageNumber = 1;
// Save the udpated info
if (RasPDFExpOpts != null )
rptClientDoc.set_SavedExportOptions(CrReportExportFormatEnum.crReportExportFormatPDF, RasPDFExpOpts);
CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions exportOpts1 = new CrystalDecisions.ReportAppServer.ReportDefModel.ExportOptions();
exportOpts1.ExportFormatType = CrReportExportFormatEnum.crReportExportFormatPDF;
exportOpts1.FormatOptions = RasPDFExpOpts;
// And Export
rptClientDoc.PrintOutputController.ExportEx(exportOpts1).Save(outputFileName, true);
MessageBox.Show("Export to PDF Completed", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
btnSQLStatement.Text = "ERROR: " + ex.Message;
return;
}
// This works do not alter
}
#endregion PDF
Just needs permission to write to disk and then you can use OS API's to stream the PDF file if you want to. Once complete then delete the file.
Don
User | Count |
---|---|
87 | |
10 | |
10 | |
10 | |
7 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.