Last active
December 22, 2015 09:49
-
-
Save jpolvora/6454647 to your computer and use it in GitHub Desktop.
Modo de Uso: var report = viewer.RptViewer.LocalReport; JonePolvora.LocalReportExtensions.ImprimeDireto(report);
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.Drawing.Printing; | |
using System.IO; | |
using Microsoft.Reporting.WinForms; | |
// ReSharper disable once CheckNamespace | |
namespace JonePolvora { | |
public static class LocalReportExtensions { | |
/// <summary> | |
/// Recebe um array com diversos relatórios a serem impressos | |
/// </summary> | |
/// <param name="reports"></param> | |
public static void ImprimeDireto(this LocalReport[] reports) { | |
foreach (var localReport in reports) { | |
ImprimeDireto(localReport); | |
} | |
} | |
/// <summary> | |
/// Imprime um relatorio direto à impressora | |
/// </summary> | |
/// <param name="report"></param> | |
public static void ImprimeDireto(this LocalReport report) { | |
var streams = Export(report); | |
var mCurrentPageIndex = 0; | |
var printDoc = new PrintDocument(); | |
if (!printDoc.PrinterSettings.IsValid) { | |
throw new Exception("Error: Impressora padrão não configurada!"); | |
} | |
printDoc.PrintPage += (o, args) => { | |
var pageImage = new Metafile(streams[mCurrentPageIndex]); | |
// margens | |
var adjustedRect = new Rectangle( | |
args.PageBounds.Left - (int)args.PageSettings.HardMarginX, | |
args.PageBounds.Top - (int)args.PageSettings.HardMarginY, | |
args.PageBounds.Width, | |
args.PageBounds.Height); | |
args.Graphics.FillRectangle(Brushes.White, adjustedRect); | |
args.Graphics.DrawImage(pageImage, adjustedRect); | |
mCurrentPageIndex++; | |
args.HasMorePages = (mCurrentPageIndex < streams.Count); | |
}; | |
printDoc.Print(); | |
} | |
// Export the given report as an EMF (Enhanced Metafile) file. | |
private static List<Stream> Export(LocalReport report) { | |
const 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>"; | |
Warning[] warnings; | |
var mStreams = new List<Stream>(); | |
report.Render("Image", deviceInfo, (name, extension, encoding, type, seek) => { | |
Stream stream = new MemoryStream(); | |
mStreams.Add(stream); | |
return stream; | |
}, out warnings); | |
foreach (var stream in mStreams) | |
stream.Position = 0; | |
return mStreams; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment