Aggiunto progetto applicazione console per stampa report locale della scheda di identificazione
git-svn-id: https://keyhammer.ath.cx/svn/XPS/trunk@98 43c8e981-f90d-406c-a89a-24a2c4268d51
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Data;
|
||||
using System.Text;
|
||||
using System.Drawing.Imaging;
|
||||
using System.Drawing.Printing;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows.Forms;
|
||||
using Microsoft.Reporting.WinForms;
|
||||
|
||||
public class stampaSchedaIdent : IDisposable
|
||||
{
|
||||
private int m_currentPageIndex;
|
||||
private IList<Stream> m_streams;
|
||||
|
||||
/// <summary>
|
||||
/// carica i dati richiesti dal report dalla StoredProcedure (filtrando quindi...)
|
||||
/// </summary>
|
||||
/// <param name="idxObj"></param>
|
||||
/// <returns></returns>
|
||||
private DataTable caricaDati(string idxObj)
|
||||
{
|
||||
PrintLocalReport.DS_applicazioneTableAdapters.sp_schedaIdentTableAdapter taSp = new PrintLocalReport.DS_applicazioneTableAdapters.sp_schedaIdentTableAdapter();
|
||||
return (DataTable)taSp.GetData(idxObj);
|
||||
}
|
||||
/// <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)
|
||||
{
|
||||
Stream stream = new FileStream(@"..\..\" + name + "." + fileNameExtension, FileMode.Create);
|
||||
m_streams.Add(stream);
|
||||
return stream;
|
||||
}
|
||||
/// <summary>
|
||||
/// Export del report come EMF (Enhanced Metafile) file.
|
||||
/// </summary>
|
||||
/// <param name="report"></param>
|
||||
private void Export(LocalReport report)
|
||||
{
|
||||
string deviceInfo =
|
||||
"<DeviceInfo>" +
|
||||
" <OutputFormat>EMF</OutputFormat>" +
|
||||
" <PageWidth>25cm</PageWidth>" +
|
||||
" <PageHeight>17.6cm</PageHeight>" +
|
||||
" <MarginTop>0.2cm</MarginTop>" +
|
||||
" <MarginLeft>0.2cm</MarginLeft>" +
|
||||
" <MarginRight>0.2cm</MarginRight>" +
|
||||
" <MarginBottom>0.2cm</MarginBottom>" +
|
||||
"</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>
|
||||
/// 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()
|
||||
{
|
||||
//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)
|
||||
{
|
||||
string msg = String.Format(
|
||||
"Can't find printer \"{0}\".", printerName);
|
||||
MessageBox.Show(msg, "Print Error");
|
||||
return;
|
||||
}
|
||||
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
|
||||
printDoc.Print();
|
||||
}
|
||||
// Create a local report for Report.rdlc, load the data,
|
||||
// export the report to an .emf file, and print it.
|
||||
private void Run(string idxObjRich)
|
||||
{
|
||||
LocalReport report = new LocalReport();
|
||||
report.ReportPath = @"..\..\ReportSchedaIdentificazione.rdlc";
|
||||
report.DataSources.Add(new ReportDataSource("Equa", caricaDati(idxObjRich)));
|
||||
ReportParameter[] idxObj = { new ReportParameter("IdxObj", idxObjRich) };
|
||||
report.SetParameters(idxObj);
|
||||
Export(report);
|
||||
m_currentPageIndex = 0;
|
||||
Print();
|
||||
}
|
||||
/// <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;
|
||||
}
|
||||
}
|
||||
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
using (stampaSchedaIdent stampaScheda = new stampaSchedaIdent())
|
||||
{
|
||||
stampaScheda.Run(args[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user