Saturday, February 25, 2012

Problem with printing report directly to printer

Hello,

I am trying to get a SSRS 2005 report to print from my Visual Studio 2005 C++ application without using the ReportViewer to preview it first. What I have done is created a dll that I call into when I want to access a certain report and print it. While searching around on the internet I found an MSDN article about printing a report without previewing and it had an example in C# code. So I used that as a guide for my C++ code but I am still having problems with rendering the report properly so it can be printed. When I try to render a report using the "Image" format, my streamid string is empty but the byte array that the render routine returns is not. Here is the code I am using, what could be the problem here?

Note: m_Streams is define elsewhere as

array<String^>^ m_Streams = gcnew array<String^>(10);

void Print::Export(LocalReport^ report)

{

array<Warning^>^ Warn = gcnew array<Warning^>(10);

String^ deviceinfo =

"<DeviceInfo>" +

" <OutputFormat>EMF</OutputFormat>" +

" <PageWidth>8.5in</PageWidth>" +

" <PageHeight>11in</PageHeight>" +

" <MarginTop>0.25in</MarginTop>" +

" <MarginLeft>0.25in</MarginLeft>" +

" <MarginRight>0.25in</MarginRight>" +

" <MarginBottom>0.25in</MarginBottom>" +

"</DeviceInfo>";

String^ mimeType;

String^ enc;

String^ FileExt;

array<Byte>^ bytes;

bytes = report->Render("Image",deviceinfo, mimeType, enc, FileExt, m_Streams,Warn); // m_Streams has a length of

return; // 0 after the Render

}

void Print:: PrintPage(System:: Object^ sender, System:: Drawing:: Printing:: PrintPageEventArgs^ ev)

{

Metafile^ pageImage = gcnew Metafile(m_Streams[m_CurrentPage]);

ev->Graphics->DrawImage(pageImage, ev->PageBounds);

m_CurrentPage++;

ev->HasMorePages = (m_CurrentPage < m_Streams->Length);

return;

}

void Print:Stick out tonguerintRpt()

{

String^ printerName = "Default";

if (m_Streams->Length < 0)

return;

PrintDocument^ printDoc = gcnew PrintDocument();

if (!printDoc->PrinterSettings->IsValid) {

return;

}

printDoc->PrintPage += gcnew PrintPageEventHandler(this, &Print:: PrintPage);

printDoc->Print();

return;

}

void Print::Run()

{

LocalReport^ report = gcnew LocalReport();

DataSet^ ds = gcnew DataSet();

LoadData(ds);

report->ReportPath = "c:\\bmi\\bulrpt\\Report1.rdlc";

ReportDataSource^ RDS = gcnew ReportDataSource();

RDS->Name = "DataSet1_Subject";

RDS->Value = ds->Tables["Subject"];

report->DataSources->Add(RDS);

Export(report);

PrintRpt();

return;

}

DataTable^ Print:: LoadData(DataSet^ ds)

{

System:: String ^ConnStr = "SELECT * FROM Subject";

SqlConnection^ conn = gcnew SqlConnection("Data Source=JOE-PC\\BMIMSDESERVER; Initial Catalog=stx52013;Integrated Security = TRUE");

SqlCommand^ command = gcnew SqlCommand(ConnStr, conn);

SqlDataAdapter^ adapt = gcnew SqlDataAdapter(command);

adapt->TableMappings->Add("Table", "Subject");

conn->Open();

adapt->Fill(ds);

return ds->Tables["Subject"];

}

I had the same problem with that render method and simply looped until the byte lenth was zero and that worked. There is another example out there that used the second overload of the method that works pretty much as is. In that one you pass a call back event handler.

|||

I've tried to do

bytes = report->Render("Image",deviceinfo, &Print::CreateStream,Warn);

as well but when I compile that I get the error -

error C3374: can't take address of 'Print::CreateStream' unless creating delegate instance

What does this mean?

No comments:

Post a Comment