279 lines
9.6 KiB
C#
279 lines
9.6 KiB
C#
using Microsoft.Reporting.WinForms;
|
|
using SteamWare.IO;
|
|
using SteamWare.Logger;
|
|
using SteamWare.Reports;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Drawing.Imaging;
|
|
using System.Drawing.Printing;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace MagData
|
|
{
|
|
/// <summary>
|
|
/// Classe che si occupa di stampare report da reportViewer via printer remota
|
|
/// </summary>
|
|
public class reportPrinter
|
|
{
|
|
#region Private Fields
|
|
|
|
private int m_currentPageIndex;
|
|
|
|
/// <summary>
|
|
/// stream del report...
|
|
/// </summary>
|
|
private IList<Stream> m_streams;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
protected bool doPdfCopy = false;
|
|
protected int logLevel = 0;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Fields
|
|
|
|
/// <summary>
|
|
/// singleton pubblico
|
|
/// </summary>
|
|
public static reportPrinter obj = new reportPrinter();
|
|
|
|
#endregion Public Fields
|
|
|
|
#region Protected Constructors
|
|
|
|
/// <summary>
|
|
/// oggetto protected
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
protected reportPrinter()
|
|
{
|
|
logLevel = memLayer.ML.CRI("_logLevel");
|
|
doPdfCopy = memLayer.ML.CRB("doPdfCopy");
|
|
}
|
|
|
|
#endregion Protected Constructors
|
|
|
|
#region Private Methods
|
|
|
|
/// <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)
|
|
{
|
|
// creo files con nomi univoci...
|
|
string filePathName = string.Format(@"~\temp\{0}_{1:HHmmss}_{1:ffff}.{2}", name, DateTime.Now, fileNameExtension);
|
|
Stream stream = new FileStream(fileMover.getFilePath(filePathName), FileMode.Create);
|
|
//Stream stream = new FileStream(fileMover.getFilePath(@"~\temp\" + name + "." + fileNameExtension), FileMode.Create);
|
|
m_streams.Add(stream);
|
|
return stream;
|
|
}
|
|
|
|
/// <summary>
|
|
/// ciclo da fornire al renderizzatore dei report, per salvare 1 pdf 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 CreateStreamPdf(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
|
|
{
|
|
// creo Directory se non c'è
|
|
fileMover fm = new fileMover(string.Format(@"{0}\{1:yyyy}\{1:MM}\{1:dd}\", memLayer.ML.confReadString("PdfFolder"), DateTime.Now), "");
|
|
fm.checkDir();
|
|
string pdfPathName = string.Format(@"{0}\{1:yyyy}\{1:MM}\{1:dd}\{2}_{1:HHmmss}_{1:ffff}.{3}", memLayer.ML.confReadString("PdfFolder"), DateTime.Now, name, fileNameExtension);
|
|
//Stream stream = new FileStream(fileMover.getFilePath(@"~\temp\" + name + "." + fileNameExtension), FileMode.Create);
|
|
Stream stream = new FileStream(fileMover.getFilePath(pdfPathName), FileMode.Create);
|
|
m_streams.Add(stream);
|
|
return stream;
|
|
}
|
|
|
|
/// <summary>
|
|
/// esegue print vero e proprio
|
|
/// </summary>
|
|
/// <param name="printerName"></param>
|
|
/// <param name="report"></param>
|
|
/// <param name="deviceInfo"></param>
|
|
private void doEmfPrint(string printerName, LocalReport report, string deviceInfo)
|
|
{
|
|
// export in EMF
|
|
Export(report, deviceInfo);
|
|
m_currentPageIndex = 0;
|
|
// stampo
|
|
Print(printerName);
|
|
// do dispose?
|
|
Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// effettua stampa in PDF dei vari report in una cartella Anno/Mese/Giorno
|
|
/// </summary>
|
|
/// <param name="report"></param>
|
|
/// <param name="deviceInfo"></param>
|
|
private void doLocalPdfPrint(LocalReport report, string deviceInfo)
|
|
{
|
|
// export in PDF
|
|
ExportPDF(report, deviceInfo);
|
|
m_currentPageIndex = 0;
|
|
// do dispose?
|
|
Dispose();
|
|
}
|
|
|
|
/// <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>();
|
|
report.Render("Image", deviceInfo, CreateStream, out warnings);
|
|
foreach (Stream stream in m_streams)
|
|
{
|
|
stream.Position = 0;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Export del report come PDF file.
|
|
/// </summary>
|
|
/// <param name="report"></param>
|
|
private void ExportPDF(LocalReport report, string deviceInfo)
|
|
{
|
|
Warning[] warnings;
|
|
m_streams = new List<Stream>();
|
|
report.Render("PDF", deviceInfo, CreateStreamPdf, out warnings);
|
|
foreach (Stream stream in m_streams)
|
|
{
|
|
stream.Position = 0;
|
|
}
|
|
}
|
|
|
|
/// <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";
|
|
if (m_streams == null || m_streams.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
PrintDocument printDoc = new PrintDocument();
|
|
printDoc.PrinterSettings.PrinterName = printerName;
|
|
if (!printDoc.PrinterSettings.IsValid)
|
|
{
|
|
Logging.Instance.Error($"Impostazioni non valide per la stampante {printerName}");
|
|
return;
|
|
}
|
|
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
|
printDoc.Print();
|
|
}
|
|
|
|
/// <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);
|
|
}
|
|
|
|
#endregion Private Methods
|
|
|
|
#region Public Methods
|
|
|
|
/// <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;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// effettua pulizia della cartella temp x i files più vecchi di X ore (web.config)
|
|
/// </summary>
|
|
public void pulisciDir()
|
|
{
|
|
// num max ore di età x files "vecchi" da tenere in temp area...
|
|
int maxOre = memLayer.ML.CRI("maxAgeTempAreaHours");
|
|
int eliminati = 0;
|
|
// ottengo elenco files *.emf
|
|
fileMover.obj.setDirectory(@"~\temp\");
|
|
FileInfo[] _fis = fileMover.obj.elencoFiles_FI("*.emf");
|
|
bool fatto = false;
|
|
foreach (FileInfo _file in _fis)
|
|
{
|
|
if (_file.CreationTime < DateTime.Now.AddHours(-maxOre)) // elimino files vecchi...
|
|
{
|
|
fatto = fileMover.obj.eliminaFile(_file);
|
|
if (fatto)
|
|
{
|
|
Logging.Instance.Info(String.Format("Eliminato file {0}", _file.Name));
|
|
eliminati++;
|
|
}
|
|
}
|
|
}
|
|
// salvo il log degli update
|
|
if (eliminati > 0)
|
|
{
|
|
Logging.Instance.Info(String.Format("Eliminati {0} files temporanei da area temp", eliminati));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Effettua la stampa del documento indicato
|
|
/// </summary>
|
|
/// <param name="documento">tipo di documento dastampare</param>
|
|
/// <param name="printerName">coda di stampa da utilizzare</param>
|
|
/// <param name="keyParam">Chiave del documento</param>
|
|
/// <returns></returns>
|
|
public bool stampaReport(tipoDocumento documento, string printerName, string keyParam)
|
|
{
|
|
bool answ = false;
|
|
MagDataLayer dtProx = new MagDataLayer();
|
|
// gestione coda stampa...
|
|
|
|
// incomincio con gestione della coda di stampa... inserisco in tab la richiesta di stampa...
|
|
if (memLayer.ML.CRB("disable_singleton"))
|
|
{
|
|
dtProx.taPJQ.insertQuery(documento.ToString(), keyParam, printerName);
|
|
}
|
|
else
|
|
{
|
|
MagDataLayer.man.taPJQ.insertQuery(documento.ToString(), keyParam, printerName);
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |