SSRS PDF, PDF , , . :
public void PrintPreviewSSRSReport(string reportName)
{
try
{
string reportPath = string.Empty;
string historyID = null;
string deviceInfo = null;
string extension = null;
string encoding = null;
string mimeType = null;
string[] streamIDs = null;
string format = "PDF";
Byte[] result;
ReportExecution2005.Warning[] warnings = null;
ExecutionInfo execInfo = new ExecutionInfo();
TrustedUserHeader trusteduserHeader = new TrustedUserHeader();
ExecutionHeader execHeader = new ExecutionHeader();
ServerInfoHeader serviceInfo = new ServerInfoHeader();
MHTools.ReportExecution2005.ReportParameter[] _parameters = null;
ParameterValue[] _ParameterValue = null;
reportPath = "/Reports/SalesReport";
ReportExecutionServiceSoapClient rsExec = new ReportExecutionServiceSoapClient();
rsExec.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
rsExec.LoadReport(trusteduserHeader, reportPath, historyID, out serviceInfo, out execInfo);
execHeader.ExecutionID = execInfo.ExecutionID;
_parameters = execInfo.Parameters;
_ParameterValue = new ParameterValue[1];
_ParameterValue[0] = new ParameterValue();
_ParameterValue[0].Name = _parameters[0].Name;
_ParameterValue[0].Value = "12345";
rsExec.SetExecutionParameters(execHeader, null, _ParameterValue, "en-us", out execInfo);
rsExec.Render(execHeader, null, format, deviceInfo, out result, out extension, out mimeType, out encoding, out warnings, out streamIDs);
using (FileStream stream = File.OpenWrite(PDFFile))
{
stream.Write(result, 0, result.Length);
}
SendFileToPrinter(PDFFile);
}
catch (Exception ex)
{
}
}
#region Print SSRS Report
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
private class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
private static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
[DllImport("winspool.drv", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool GetDefaultPrinter(StringBuilder pszBuffer, ref int size);
public bool SendFileToPrinter(string pdfFileName)
{
try
{
#region Get Connected Printer Name
PrintDocument pd = new PrintDocument();
StringBuilder dp = new StringBuilder(256);
int size = dp.Capacity;
if (GetDefaultPrinter(dp, ref size))
{
pd.PrinterSettings.PrinterName = dp.ToString().Trim();
}
#endregion Get Connected Printer Name
FileStream fs = new FileStream(pdfFileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = new Byte[fs.Length];
bool success = false;
IntPtr ptrUnmanagedBytes = new IntPtr(0);
int nLength = Convert.ToInt32(fs.Length);
bytes = br.ReadBytes(nLength);
ptrUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
Marshal.Copy(bytes, 0, ptrUnmanagedBytes, nLength);
success = SendBytesToPrinter(pd.PrinterSettings.PrinterName, ptrUnmanagedBytes, nLength);
Marshal.FreeCoTaskMem(ptrUnmanagedBytes);
return success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
private bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
try
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool success = false;
di.pDocName = Path.GetFileNameWithoutExtension(PDFFile);
di.pDataType = "RAW";
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
if (StartDocPrinter(hPrinter, 1, di))
{
if (StartPagePrinter(hPrinter))
{
success = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
if (success == false)
{
dwError = Marshal.GetLastWin32Error();
}
return success;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
#endregion