あなたは(レポートビューアを開く前に)最初のデフォルトのプリンタ名を取得することができます
System.Drawing.Printing.PrinterSettings settings = new System.Drawing.Printing.PrinterSettings();
defaultPrinterName = settings.PrinterName;
印刷するときは、お使いのプリンタ名がPrintDocument
のPrinterName
プロパティに割り当てます。
LocalReport rep = new LocalReport();
//set your data and parameters here
//...
rep.Refresh();
ExportLandscape(rep);
PrintDocument printDoc = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
ps.PrinterName = defaultPrinterName;
printDoc.PrinterSettings = ps;
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
実際の印刷を処理するためのイベント:
ここで
//this has to declared somewhere at the "top":
private IList<Stream> m_streams;
private int m_currentPageIndex;
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 ExportPortrait(LocalReport report) {
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>8.5in</PageWidth>
<PageHeight>11in</PageHeight>
<MarginTop>0.5in</MarginTop>
<MarginLeft>0.5in</MarginLeft>
<MarginRight>0.5in</MarginRight>
<MarginBottom>0.5in</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;
}
private void ExportLandscape(LocalReport report) {
string deviceInfo =
@"<DeviceInfo>
<OutputFormat>EMF</OutputFormat>
<PageWidth>11in</PageWidth>
<PageHeight>8.5in</PageHeight>
<MarginTop>0.5in</MarginTop>
<MarginLeft>0.5in</MarginLeft>
<MarginRight>0.5in</MarginRight>
<MarginBottom>0.5in</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;
}
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek) {
Stream stream = new MemoryStream();
m_streams.Add(stream);
return stream;
}
thisのようなコードをvb.netに変換する必要があります。