0% found this document useful (0 votes)
42 views2 pages

Autoprint

This class contains methods for exporting a report to streams and then printing the report. It creates streams to render pages to, stores them in a list, and implements printing by drawing each stream as an image onto a print page. It handles pagination by tracking the current page index and setting a flag to indicate if more pages remain to be printed.

Uploaded by

Glaire Sy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views2 pages

Autoprint

This class contains methods for exporting a report to streams and then printing the report. It creates streams to render pages to, stores them in a list, and implements printing by drawing each stream as an image onto a print page. It handles pagination by tracking the current page index and setting a flag to indicate if more pages remain to be printed.

Uploaded by

Glaire Sy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

class AutoPrint

{
private int m_currentPageIndex;
private IList<Stream> m_streams;

private Stream CreateStream(string name,


string fileNameExtension, Encoding encoding,
string mimeType, bool willSeek)
{
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
public void Export(LocalReport report)
{
string deviceInfo =

@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0in</MarginTop>
<MarginLeft>0in</MarginLeft>
<MarginRight>0in</MarginRight>
<MarginBottom>0in</MarginBottom>
</DeviceInfo>";

Warning[] warnings;
m_streams = new List<Stream>();
report.Render("Image", deviceInfo, CreateStream,
out warnings);
foreach (Stream stream in m_streams)
stream.Position = 0;
m_currentPageIndex = 0;
//var d = new PrintDocument();
//d.PrintController = new StandardPrintController();
Print();

private void PrintPage(object sender, PrintPageEventArgs ev)


{
Metafile pageImage = new
Metafile(m_streams[m_currentPageIndex]);

// Adjust rectangular area with printer margins.


Rectangle adjustedRect = new Rectangle(
ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX,
ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY,
ev.PageBounds.Width,
ev.PageBounds.Height);

// Draw a white background for the report


ev.Graphics.FillRectangle(Brushes.White, adjustedRect);

// Draw the report content


ev.Graphics.DrawImage(pageImage, adjustedRect);
// Prepare for the next page. Make sure we haven't hit the end.
m_currentPageIndex++;
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
}
private void Print()
{
//PrinterSettings settings = new PrinterSettings(); //set printer
settings
string printerName = Settings.Default.DOCPRINTNAME;

//settings.PrinterName; //use default printer name

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.PrintController = new StandardPrintController();
printDoc.Print();
}
public void Dispose()
{
if (m_streams != null)
{
foreach (Stream stream in m_streams)
stream.Close();
m_streams = null;
}
}

You might also like