From: JM on
I have modified the example in the MSDN, Walkthrough: Printing a Local Report
without Preview, to generate the rendered output as a PDF file. When I try
to print the rendered file a get an exception of "A Generic errir occured in
GDI+" on the Metafile pageImage = new... statement in the PrintPage event
handler. I have looked to see if there was way PDF files were handled
differently then the Image files and could not find any answers.

Either my implementation of the PrintPage handler is incorrect that is I
need to used another class other then the MetaFile class or I have another
problem which I do not see. The output file from the Render seems correct
because I can view it with Adobe Reader. I just cannot get the example to
print it.

I have included the source for the methods from the example below so you can
see my changes.

-Jim

private void Export( LocalReport report )
{
string deviceInfo =
"<DeviceInfo>" +
" <OutputFormat>PDF</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>";
Warning[ ] warnings;
try
{
m_streams = new List<Stream>( );
report.Render( "PDF", deviceInfo, CreateStream, out warnings );
foreach( Stream stream in m_streams )
stream.Position = 0;
}
catch( Exception ex )
{
string msg = ex.Message;
}
}
// Handler for PrintPageEvents
private void PrintPage( object sender, PrintPageEventArgs ev )
{
try
{
Metafile pageImage = new
Metafile( m_streams[ m_currentPageIndex ] ); // <-- exception
is here
ev.Graphics.DrawImage( pageImage, ev.PageBounds );
m_currentPageIndex++;
ev.HasMorePages = ( m_currentPageIndex < m_streams.Count );
}
catch( Exception ex )
{
string msg = ex.Message;
throw;
}
}

private void Print( )
{
const string printerName = "HP LaserJet 2100 PCL6";
if( m_streams == null || m_streams.Count == 0 )
return;
PrintDocument printDoc = new PrintDocument( );
printDoc.PrinterSettings.PrinterName = printerName;
if( !printDoc.PrinterSettings.IsValid )
{
string msg = String.Format(
"Can't find printer \"{0}\".", printerName );
MessageBox.Show( msg, "Print Error" );
return;
}
printDoc.PrintPage += new PrintPageEventHandler( PrintPage );
printDoc.Print( );
}