288 lines
11 KiB
C#
288 lines
11 KiB
C#
using Microsoft.Reporting.WinForms;
|
|
using SteamWare;
|
|
using SteamWare.IO;
|
|
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.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace LPA
|
|
{
|
|
public class reportPrinter
|
|
{
|
|
/// <summary>
|
|
/// Directory di abse applicazione
|
|
/// </summary>
|
|
protected string baseDir;
|
|
/// <summary>
|
|
/// Path x salvataggio locale pdf
|
|
/// </summary>
|
|
protected string localPdfPath = "pdf";
|
|
/// <summary>
|
|
/// Classe reportprinter
|
|
/// </summary>
|
|
public reportPrinter()
|
|
{
|
|
baseDir = AppDomain.CurrentDomain.BaseDirectory;
|
|
localPdfPath = memLayer.ML.confReadString("localPdfPath");
|
|
}
|
|
|
|
#region metodi stampa effettiva report
|
|
|
|
/// <summary>
|
|
/// Indice pagina corrente x stampa ogni report
|
|
/// </summary>
|
|
private int m_currentPageIndex;
|
|
/// <summary>
|
|
/// stream del report...
|
|
/// </summary>
|
|
private IList<Stream> m_streams;
|
|
/// <summary>
|
|
/// Crea un report locale da file rdlc, carica i dati, esporta report come EMF file e quindi lo invia alla stampante + eventuale backup pdf
|
|
/// </summary>
|
|
/// <param name="reportTemplate">Nome report (file)</param>
|
|
/// <param name="localReportPath">Cartella locale dei report</param>
|
|
/// <param name="printerName">nome completo stampante (rispetto al server)</param>
|
|
/// <param name="rdsData">tabella dati da caricare nel report</param>
|
|
/// <param name="deviceInfoParam">parametri "device input"</param>
|
|
/// <param name="doPdfCopy">effettua ANCHE copia pdf</param>
|
|
public bool printReport(string reportTemplate, string localReportPath, string printerName, Dictionary<string, DataTable> rdsData, devInfoParam deviceInfoParam, bool doPdfCopy)
|
|
{
|
|
bool answ = false;
|
|
if (deviceInfoParam != null)
|
|
{
|
|
using (LocalReport report = new LocalReport())
|
|
{
|
|
//LocalReport report = new LocalReport();
|
|
report.EnableExternalImages = true;
|
|
report.ReportPath = $"{localReportPath}{reportTemplate}";
|
|
// per ogni oggetto rds nome/tabela aggiungo la sorgente dati...
|
|
string rdsName = "";
|
|
DataTable tabDati = null;
|
|
foreach (var item in rdsData)
|
|
{
|
|
rdsName = item.Key;
|
|
tabDati = item.Value;
|
|
report.DataSources.Add(new ReportDataSource(rdsName, tabDati));
|
|
}
|
|
|
|
// stampa da EMF
|
|
answ = doEmfPrint(printerName, report, deviceInfoParam.xmlParam);
|
|
// controllo se devo fare copia PDF... stampiamosu pdf su una folder locale
|
|
if (doPdfCopy)
|
|
{
|
|
devInfoParam devInfoPdf = new devInfoParam("PDF", deviceInfoParam.PageHeight, deviceInfoParam.PageWidth, deviceInfoParam.MarginLeft, deviceInfoParam.MarginRight, deviceInfoParam.MarginTop, deviceInfoParam.MarginBottom);
|
|
// salva ANCHE pdf
|
|
doLocalPdfPrint(report, devInfoPdf.xmlParam);
|
|
}
|
|
}
|
|
}
|
|
return answ;
|
|
}
|
|
/// <summary>
|
|
/// esegue print vero e proprio
|
|
/// </summary>
|
|
/// <param name="printerName"></param>
|
|
/// <param name="report"></param>
|
|
/// <param name="deviceInfo"></param>
|
|
private bool doEmfPrint(string printerName, LocalReport report, string deviceInfo)
|
|
{
|
|
bool fatto = false;
|
|
// export in EMF
|
|
Export(report, deviceInfo);
|
|
m_currentPageIndex = 0;
|
|
// stampo
|
|
fatto = Print(printerName);
|
|
// do dispose...
|
|
Dispose();
|
|
return fatto;
|
|
}
|
|
/// <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.Flush();
|
|
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.Flush();
|
|
stream.Position = 0;
|
|
}
|
|
}
|
|
/// <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>
|
|
/// 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)
|
|
{
|
|
// compongo stringhe x file con nomi univoci
|
|
DateTime adesso = DateTime.Now;
|
|
Random randNum = new Random();
|
|
string filePathName = $"{baseDir}{localPdfPath}\\temp\\{name}_{adesso:HHmmss}_{adesso:ffffff}_{randNum.Next(0, 999):000}.{fileNameExtension}";
|
|
Stream stream = new FileStream(filePathName, 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)
|
|
{
|
|
// compongo stringhe
|
|
DateTime adesso = DateTime.Now;
|
|
string dailyDir = $"{baseDir}{localPdfPath}\\{adesso:yyyy}\\{adesso:MM}\\{adesso:dd}\\";
|
|
string pdfPathName = $"{dailyDir}\\{name}_{adesso:HHmmss}_{adesso:ffff}.{fileNameExtension}";
|
|
// creo Directory se non c'è
|
|
fileMover fm = new fileMover(dailyDir, "");
|
|
fm.checkDir();
|
|
Stream stream = new FileStream(pdfPathName, 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)
|
|
{
|
|
try
|
|
{
|
|
Metafile pageImage = new Metafile(m_streams[m_currentPageIndex]);
|
|
ev.Graphics.DrawImage(pageImage, ev.PageBounds);
|
|
m_currentPageIndex++;
|
|
ev.HasMorePages = (m_currentPageIndex < m_streams.Count);
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Instance.Error($"Errore in PrintPage{Environment.NewLine}{exc}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// funzione di stampa...
|
|
/// </summary>
|
|
private bool Print(string printerName)
|
|
{
|
|
if (m_streams == null || m_streams.Count == 0)
|
|
return false;
|
|
|
|
bool fatto = false;
|
|
// provo a stampare
|
|
try
|
|
{
|
|
using (PrintDocument printDoc = new PrintDocument())
|
|
{
|
|
printDoc.PrinterSettings.PrinterName = printerName;
|
|
if (!printDoc.PrinterSettings.IsValid)
|
|
{
|
|
Log.Instance.Error($"Impostazioni non valide per la stampante {printerName}");
|
|
return fatto;
|
|
}
|
|
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
|
printDoc.Print();
|
|
fatto = true;
|
|
};
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Log.Instance.Error($"Errore in Print{Environment.NewLine}{exc}");
|
|
}
|
|
return fatto;
|
|
}
|
|
/// <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 maxAgeMinutes = memLayer.ML.CRI("maxAgeTempArea");
|
|
int eliminati = 0;
|
|
// ottengo elenco files *.emf
|
|
fileMover.obj.setDirectory($"{baseDir}{localPdfPath}\\temp\\");
|
|
FileInfo[] _fis = fileMover.obj.elencoFiles_FI("*.emf");
|
|
bool fatto = false;
|
|
foreach (FileInfo _file in _fis)
|
|
{
|
|
if (_file.CreationTime < DateTime.Now.AddMinutes(-maxAgeMinutes)) // elimino files vecchi...
|
|
{
|
|
fatto = fileMover.obj.eliminaFile(_file);
|
|
if (fatto)
|
|
{
|
|
Log.Instance.Info($"Eliminato file {_file.Name}");
|
|
eliminati++;
|
|
}
|
|
}
|
|
}
|
|
// salvo il log degli update
|
|
if (eliminati > 0)
|
|
{
|
|
Log.Instance.Info($"Eliminati {eliminati} files temporanei da area temp");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|