233 lines
8.2 KiB
C#
233 lines
8.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.IO;
|
|
using System.Data;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Printing;
|
|
using System.Windows.Forms;
|
|
using Microsoft.Reporting.WinForms;
|
|
using System.Configuration;
|
|
using SteamWare;
|
|
|
|
namespace XPS_data
|
|
{
|
|
/// <summary>
|
|
/// Summary description for reportPrinter
|
|
/// </summary>
|
|
public class reportPrinter
|
|
{
|
|
|
|
#region area codice da non modificare
|
|
|
|
|
|
private int m_currentPageIndex;
|
|
private IList<Stream> m_streams;
|
|
/// <summary>
|
|
/// ciclo da fornire al renderizzatore dei report, per salvare 1 immagine da ogni pagina del report
|
|
/// </summary>
|
|
/// <param name="name"></param>
|
|
/// <param name="fileNameExtension"></param>
|
|
/// <param name="encoding"></param>
|
|
/// <param name="mimeType"></param>
|
|
/// <param name="willSeek"></param>
|
|
/// <returns></returns>
|
|
private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
|
{
|
|
string filePath = System.Web.HttpContext.Current.Server.MapPath(@"~\tmp\" + name + "." + fileNameExtension);
|
|
if (memLayer.ML.CRI("_logLevel") > 6) logger.lg.scriviLog(string.Format("Creazione file EMF in {0}", filePath), tipoLog.INFO);
|
|
Stream stream = new FileStream(filePath, FileMode.Create);
|
|
m_streams.Add(stream);
|
|
return stream;
|
|
}
|
|
/// <summary>
|
|
/// Handler per PrintPageEvents
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="ev"></param>
|
|
private void PrintPage(object sender, PrintPageEventArgs ev)
|
|
{
|
|
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
|
|
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
|
|
m_currentPageIndex++;
|
|
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
|
|
}
|
|
/// <summary>
|
|
/// funzione di stampa...
|
|
/// </summary>
|
|
private void Print(string printerName)
|
|
{
|
|
//const string printerName = "Microsoft Office Document Image Writer";
|
|
//const string printerName = "Brother HL-2170W series";
|
|
logger.lg.scriviLog(string.Format("inizio print su {0}", printerName), tipoLog.INFO);
|
|
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");
|
|
logger.lg.scriviLog("ERRORE IN check printDoc.PrinterSettings.IsValid", tipoLog.ERROR);
|
|
return;
|
|
}
|
|
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
|
printDoc.Print();
|
|
logger.lg.scriviLog("Fine print", tipoLog.INFO);
|
|
}
|
|
/// <summary>
|
|
/// Export del report come EMF (Enhanced Metafile) file.
|
|
/// </summary>
|
|
/// <param name="report"></param>
|
|
private void Export(LocalReport report, string deviceInfo)
|
|
{
|
|
Warning[] warnings;
|
|
m_streams = new List<Stream>();
|
|
//2017.09.15 cerco di intercettare eccezione...
|
|
try
|
|
{
|
|
report.Render("Image", deviceInfo, CreateStream, out warnings);
|
|
}
|
|
catch(Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("ERRORE in fase di rendering del report: {0}{1}", Environment.NewLine, exc));
|
|
}
|
|
try
|
|
{
|
|
foreach (Stream stream in m_streams) stream.Position = 0;
|
|
}
|
|
catch(Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("ERRORE in fase di ACCODAMENTO STREAM per report: {0}{1}", Environment.NewLine, exc));
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// dispone l'applicazione e rilascia le risorse
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (m_streams != null)
|
|
{
|
|
foreach (Stream stream in m_streams)
|
|
stream.Close();
|
|
m_streams = null;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region area codice da modificare
|
|
|
|
/// <summary>
|
|
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
|
|
/// </summary>
|
|
/// <param name="idxObj"></param>
|
|
/// <returns></returns>
|
|
private DataTable caricaDati(reportRichiesto tipoReport, string idxObj)
|
|
{
|
|
DataTable tab = new DataTable();
|
|
if (memLayer.ML.CRI("_logLevel") > 6) logger.lg.scriviLog(string.Format("Richiesto tabella x report {0}", tipoReport), tipoLog.INFO);
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.Barcode:
|
|
tab = (DataTable)XPS.obj.taDdt.GetData(idxObj);
|
|
break;
|
|
case reportRichiesto.SchedaIdentificazione:
|
|
tab = (DataTable)XPS.obj.taSchedaIdent.GetData(idxObj);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
if (memLayer.ML.CRI("_logLevel") > 6) logger.lg.scriviLog(string.Format("Effettuata lettura tabella x report {0}", tipoReport), tipoLog.INFO);
|
|
return tab;
|
|
}
|
|
/// <summary>
|
|
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante
|
|
/// </summary>
|
|
/// <param name="idxObjRich"></param>
|
|
/// <param name="printerName"></param>
|
|
public void printReport(reportRichiesto tipoReport, string printerName, string parametro)
|
|
{
|
|
if (parametro != "")
|
|
{
|
|
LocalReport report = new LocalReport();
|
|
string outForm = "";
|
|
string pagWidth = "";
|
|
string pagHeigth = "";
|
|
string margin = "";
|
|
// carico i dati del report da web.config
|
|
outForm = "EMF";
|
|
pagWidth = memLayer.ML.confReadString(string.Format("PageWidth{0}", tipoReport));
|
|
pagHeigth = memLayer.ML.confReadString(string.Format("PageHeight{0}", tipoReport));
|
|
margin = memLayer.ML.confReadString(string.Format("Margin{0}", tipoReport));
|
|
// compongo parametri stampa
|
|
devInfoParam deviceInfoPar = new devInfoParam(outForm, pagHeigth, pagWidth, margin, margin, margin, margin);
|
|
// path rdlc report
|
|
report.ReportPath = "";// string.Format(@".\Reports\{0}", memLayer.ML.confReadString(string.Format("rdlc{0}", tipoReport)));
|
|
switch (tipoReport)
|
|
{
|
|
case reportRichiesto.Barcode:
|
|
//report.ReportPath = @".\Reports\LabelEqua.rdlc";
|
|
report.ReportPath = string.Format(@".\{0}\LabelEqua.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("Equa", caricaDati(tipoReport, parametro))); // OCCHIO!!! va messa sorgente dati CORRETTA
|
|
break;
|
|
case reportRichiesto.SchedaIdentificazione:
|
|
//report.ReportPath = @".\Reports\ReportSchedaIdentificazione.rdlc";
|
|
report.ReportPath = string.Format(@".\{0}\ReportSchedaIdentificazione.rdlc", memLayer.ML.CRS("ReportBasePath"));
|
|
report.DataSources.Add(new ReportDataSource("Equa", caricaDati(tipoReport, parametro)));
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
// setting parametro...
|
|
int numFields = 1;
|
|
ReportParameter[] rParam = new ReportParameter[numFields];
|
|
rParam[0] = new ReportParameter("IdxObj", parametro);
|
|
report.SetParameters(rParam);
|
|
try
|
|
{
|
|
// export in EMF
|
|
Export(report, deviceInfoPar.xmlParam);
|
|
if (memLayer.ML.CRI("_logLevel") > 6) logger.lg.scriviLog("Fine export EMF", tipoLog.INFO);
|
|
m_currentPageIndex = 0;
|
|
// stampo
|
|
Print(printerName);
|
|
if (memLayer.ML.CRI("_logLevel") > 6) logger.lg.scriviLog("Fine Stampa", tipoLog.INFO);
|
|
// do dispose?
|
|
Dispose();
|
|
}
|
|
catch(Exception exc)
|
|
{
|
|
logger.lg.scriviLog(string.Format("ERRORE in fase di EXPORT del report in formato EMF: {0}{1}", Environment.NewLine, exc));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
logger.lg.scriviLog("ERRORE! parametro idxObj vuoto!!!", tipoLog.ERROR);
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// oggetto protected
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
protected reportPrinter()
|
|
{
|
|
//
|
|
}
|
|
/// <summary>
|
|
/// singleton pubblico
|
|
/// </summary>
|
|
public static reportPrinter obj = new reportPrinter();
|
|
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// tipologia di report ammessi alla stampa...
|
|
/// </summary>
|
|
public enum reportRichiesto
|
|
{
|
|
Barcode,
|
|
SchedaIdentificazione
|
|
}
|
|
} |