private void bpreview_Click(object sender, EventArgs e) { if (!Reportman.Reporting.Forms.ParamsForm.ShowParams(Report)) { return; } Report.MetaFile.Clear(); if (nprintdriver == null) { nprintdriver = new PrintOutReportWinForms(Report); } if (Report != nprintdriver.Report) { nprintdriver.Dispose(); nprintdriver = new PrintOutReportWinForms(Report); } //PrintOutReportWinForms nprintdriver = new PrintOutReportWinForms(Report); nprintdriver.Preview = true; nprintdriver.WindowMode = PreviewWindowMode.Window; nprintdriver.PreviewWindow.Parent = Parent; nprintdriver.PreviewWindow.Dock = DockStyle.Fill; nprintdriver.ShowEmptyReportMessage = true; try { nprintdriver.Print(Report.MetaFile); nprintdriver.PreviewWindow.BringToFront(); } catch { nprintdriver.PreviewWindow.SendToBack(); throw; } /* previewmetafile = new PreviewMetaFile(); * SetReportEvents(); * previewmetafile.OptimizeWMF = OptimizeWMF; * previewmetafile.MetaFile = meta; * previewmetafile.SetDriver(this); * previewwindow.ShowInTaskbar = ShowInTaskbar; * previewwindow.PreviewReport(previewmetafile); * * PreviewWindow.BringToFront(); * try * { * PreviewWindow.PreviewReport(Report.MetaFile); * } * catch * { * PreviewWindow.SendToBack(); * throw; * }*/ }
public static void Main(string[] args) { string printername = ""; AssemblyResolver.HandleUnresolvedAssemblies(); #if REPMAN_DOTNET2 // Bug in .Net 1.x, don't enable, but you can use a .manifest file #if FORMS Application.EnableVisualStyles(); #endif #endif bool dothrow = false; Report rp = new Report(); try { bool asyncexecution = true; bool stdin = false; bool pdf = false; bool metafile = false; bool showprintdialog = false; string filename = ""; string pdffilename = ""; string fieldsfilename = ""; bool deletereport = false; bool showprogress = false; bool preview = false; bool showparams = false; bool compressedpdf = true; bool systempreview = false; bool showdata = false; bool showfields = false; bool testconnection = false; string connectionname = ""; bool doprint = true; bool evaluatetext = false; bool syntaxcheck = false; bool doread = true; string evaltext = ""; string dataset = ""; SortedList <string, string> ParamValues = new SortedList <string, string>(); #if REPMAN_DOTNET2 bool showproviders = false; string providersfilename = ""; #endif try { for (int i = 0; i < args.Length; i++) { if (args[i].Trim().Length > 0) { switch (args[i].ToUpper()) { case "-STDIN": stdin = true; break; case "-PREVIEW": preview = true; break; case "-SHOWPARAMS": showparams = true; break; case "-SYNCEXECUTION": asyncexecution = false; break; case "-SYSTEMPREVIEW": systempreview = true; break; case "-U": compressedpdf = false; break; case "-THROW": dothrow = true; break; case "-PDF": pdf = true; if (args.GetUpperBound(0) > i) { i++; pdffilename = args[i]; } break; case "-M": metafile = true; if (args.GetUpperBound(0) > i) { i++; pdffilename = args[i]; } break; case "-SHOWDATA": showdata = true; doprint = false; if (args.GetUpperBound(0) > i) { i++; dataset = args[i]; } break; case "-TESTCONNECTION": doprint = false; testconnection = true; if (args.GetUpperBound(0) > i) { i++; connectionname = args[i]; } break; case "-EVAL": doprint = false; evaluatetext = true; if (args.GetUpperBound(0) > i) { i++; evaltext = args[i]; } break; case "-SYNTAX": doprint = false; syntaxcheck = true; if (args.GetUpperBound(0) > i) { i++; evaltext = args[i]; } break; case "-SHOWFIELDS": showfields = true; doprint = false; if (args.GetUpperBound(0) > i) { i++; dataset = args[i]; } if (args.GetUpperBound(0) > i) { i++; fieldsfilename = args[i]; } break; case "-GETPROVIDERS": showproviders = true; doprint = false; doread = false; if (args.GetUpperBound(0) > i) { i++; providersfilename = args[i]; } break; case "-GETPRINTERS": System.Text.StringBuilder nprinters = new System.Text.StringBuilder(); nprinters.AppendLine("Installed printers:"); foreach (string pname in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { nprinters.AppendLine(pname); } #if FORMS MessageBox.Show(nprinters.ToString()); #else System.Console.WriteLine(nprinters.ToString()); #endif break; case "-PRINTERNAME": if (args.GetUpperBound(0) > i) { i++; printername = args[i]; } else { throw new Exception("A printer name must be provided after -printername"); } break; case "-SHOWPROGRESS": showprogress = true; break; case "-DELETEREPORT": deletereport = true; break; case "-PRINTDIALOG": showprintdialog = true; break; default: // Get parameter names and values string argname = args[i]; if (argname[0] == '-') { bool correctparam = false; if (argname.Length > 7) { if (argname.Substring(0, 6).ToUpper() == "-PARAM") { argname = argname.Substring(6, argname.Length - 6); int indexequ = argname.IndexOf('='); if (indexequ < 0) { throw new Exception("Invalid syntax in -param, -paramPARAMNAME=value syntax must be used"); } string paramvalue = argname.Substring(indexequ + 1, argname.Length - indexequ - 1); argname = argname.Substring(0, indexequ); ParamValues.Add(argname, paramvalue); System.Console.WriteLine("Decoded parameter: " + argname + " value " + paramvalue); correctparam = true; } } if (!correctparam) { throw new Exception("Invalid argument:" + args[i]); } } else { if (filename.Length > 0) { filename = args[i]; } else { filename = args[i]; } } break; } } } AddCustomFactories(); #if REPMAN_DOTNET2 if (showproviders) { string messageproviders = ""; /*#if REPMAN_MONO * if (providersfilename.Length == 0) * { * foreach (Provider p in ProviderFactory.Providers) * { * if (messageproviders.Length!=0) * messageproviders=messageproviders+(char)13+(char)10; * messageproviders=messageproviders+p.Name; * * } * MessageBox.Show(messageproviders,"Data providers"); * } * else * { * FileStream providersstream = new FileStream(providersfilename,System.IO.FileMode.Create,System.IO.FileAccess.Write,System.IO.FileShare.None); * try * { * foreach (Provider p2 in ProviderFactory.Providers) * { * StreamUtil.SWriteLine(providersstream,p2.Name); * } * } * finally * { * providersstream.Close(); * } * * } #else*/ int indexp; DataTable atable = DbProviderFactories.GetFactoryClasses(); if (providersfilename.Length == 0) { bool firebirdfound = false; bool mysqlfound = false; bool sqlitefound = false; for (indexp = 0; indexp < atable.Rows.Count; indexp++) { if (messageproviders.Length != 0) { messageproviders = messageproviders + (char)13 + (char)10; } string nprovider = atable.Rows[indexp][2].ToString(); if (nprovider == DatabaseInfo.FIREBIRD_PROVIDER) { firebirdfound = true; } if (nprovider == DatabaseInfo.MYSQL_PROVIDER) { mysqlfound = true; } if (nprovider == DatabaseInfo.SQLITE_PROVIDER) { sqlitefound = true; } messageproviders = messageproviders + atable.Rows[indexp][2].ToString(); } if (!firebirdfound) { messageproviders = DatabaseInfo.FIREBIRD_PROVIDER + (char)13 + (char)10 + messageproviders; } if (!mysqlfound) { messageproviders = DatabaseInfo.MYSQL_PROVIDER + (char)13 + (char)10 + messageproviders; } if (!sqlitefound) { messageproviders = DatabaseInfo.SQLITE_PROVIDER + (char)13 + (char)10 + messageproviders; } #if FORMS MessageBox.Show(messageproviders, "Data providers"); #else System.Console.WriteLine("Data providers"); System.Console.WriteLine(messageproviders); #endif } else { FileStream providersstream = new FileStream(providersfilename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); try { bool firebirdfound = false; bool mysqlfound = false; bool sqlitefound = false; Strings nstrings = new Strings(); for (indexp = 0; indexp < atable.Rows.Count; indexp++) { string nprovider = atable.Rows[indexp][2].ToString(); if (nprovider == DatabaseInfo.FIREBIRD_PROVIDER) { firebirdfound = true; } if (nprovider == DatabaseInfo.MYSQL_PROVIDER) { mysqlfound = true; } if (nprovider == DatabaseInfo.SQLITE_PROVIDER) { sqlitefound = true; } nstrings.Add(nprovider); } if (!firebirdfound) { nstrings.Insert(0, DatabaseInfo.FIREBIRD_PROVIDER); } if (!mysqlfound) { nstrings.Insert(0, DatabaseInfo.MYSQL_PROVIDER); } if (!sqlitefound) { nstrings.Insert(0, DatabaseInfo.SQLITE_PROVIDER); } foreach (string nstring in nstrings) { StreamUtil.SWriteLine(providersstream, nstring); } } finally { providersstream.Close(); } } //#endif } #endif if (doread) { if (stdin) { Stream astream = System.Console.OpenStandardInput(); rp.LoadFromStream(astream, 8192); } else { if (filename.Length == 0) { throw new Exception("You must provide a report filename"); } rp.LoadFromFile(filename); } foreach (string nparamname in ParamValues.Keys) { string nvalue = ParamValues[nparamname]; if (rp.Params.IndexOf(nparamname) < 0) { System.Console.WriteLine("Warning: parameter not found created: " + nparamname); Reportman.Reporting.Param xparam = new Reportman.Reporting.Param(rp); xparam.Alias = nparamname; xparam.Visible = false; rp.Params.Add(xparam); //throw new Exception("Parameter " + nparamname + " not found"); } rp.Params[nparamname].Value = nvalue; } } if (showdata) { rp.PrintOnlyIfDataAvailable = false; PrintOutPDF printpdf3 = new PrintOutPDF(); rp.BeginPrint(printpdf3); #if FORMS DataShow.ShowData(rp, dataset); #else rp.DataInfo[dataset].Connect(); ReportDataset ndataset = rp.DataInfo[dataset].Data; foreach (DataColumn ncol in ndataset.Columns) { string nstring = ncol.ColumnName.PadLeft(30); if (nstring.Length > 30) { nstring = nstring.Substring(0, 30); } System.Console.Write(nstring); } System.Console.WriteLine(); while (!ndataset.Eof) { foreach (DataColumn ncol in ndataset.Columns) { string nstring = ndataset.CurrentRow[ncol.ColumnName].ToString().PadLeft(30); if (nstring.Length > 30) { nstring = nstring.Substring(0, 30); } System.Console.Write(nstring); } System.Console.WriteLine(); ndataset.Next(); } #endif } if (testconnection) { int conindex = rp.DatabaseInfo.IndexOf(connectionname); if (conindex < 0) { throw new Exception("Connection name not found:" + connectionname); } rp.DatabaseInfo[conindex].Connect(); #if FORMS MessageBox.Show("Connexion successfull:" + connectionname); #else System.Console.WriteLine("Connexion successfull:" + connectionname); #endif } if (showfields) { int index = rp.DataInfo.IndexOf(dataset); if (index < 0) { throw new Exception("Dataset not found:" + dataset); } rp.DataInfo[index].Connect(); FileStream fstream = new FileStream(fieldsfilename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.None); try { rp.DataInfo[index].GetFieldsInfo(fstream); } finally { fstream.Close(); } } if ((evaluatetext) || (syntaxcheck)) { rp.PrintOnlyIfDataAvailable = false; PrintOutPDF printpdf2 = new PrintOutPDF(); rp.BeginPrint(printpdf2); if (evaluatetext) { try { Variant aresult = rp.Evaluator.EvaluateText(evaltext); #if FORMS MessageBox.Show("Result:" + aresult.ToString()); #else System.Console.WriteLine("Result:" + aresult.ToString()); #endif } catch (EvalException e) { #if FORMS MessageBox.Show("Error Line: " + e.SourceLine.ToString() + " Error position:" + e.SourcePos.ToString() + " - " + e.Message); #else System.Console.WriteLine("Error Line: " + e.SourceLine.ToString() + " Error position:" + e.SourcePos.ToString() + " - " + e.Message); #endif } catch (Exception E) { #if FORMS MessageBox.Show("Error: " + E.Message); #else System.Console.WriteLine("Error: " + E.Message); #endif } } else { try { rp.Evaluator.CheckSyntax(evaltext); #if FORMS MessageBox.Show("Syntax check ok"); #else System.Console.WriteLine("Syntax check ok"); #endif } catch (Exception E) { #if FORMS MessageBox.Show("Error: " + E.Message); #else System.Console.WriteLine("Error: " + E.Message); #endif } } } // Ask for parameters? if (doprint) { if (showparams) { #if FORMS doprint = ParamsForm.ShowParams(rp); #else throw new Exception("Show params not supported in console mode"); #endif } } if (doprint) { if (showprogress) #if FORMS { ReportProgressForm fprogres = new ReportProgressForm(); fprogres.SetMetaFile(rp.MetaFile); fprogres.Show(); } #else { throw new Exception("Show progress not supported in console mode"); } #endif if (pdf) { rp.AsyncExecution = false; PrintOutPDF printpdf = new PrintOutPDF(); printpdf.FileName = pdffilename; printpdf.Compressed = compressedpdf; printpdf.Print(rp.MetaFile); } else if (metafile) { rp.AsyncExecution = false; rp.TwoPass = true; PrintOutPDF printpdf = new PrintOutPDF(); printpdf.FileName = ""; printpdf.Compressed = compressedpdf; printpdf.Print(rp.MetaFile); rp.MetaFile.SaveToFile(pdffilename, compressedpdf); } else { rp.AsyncExecution = asyncexecution; #if FORMS PrintOutReportWinForms prw = new PrintOutReportWinForms(rp); prw.Preview = preview; prw.ShowInTaskbar = true; prw.SystemPreview = systempreview; prw.ShowPrintDialog = showprintdialog; prw.Print(rp.MetaFile); #else PrintOutPrint prw = new PrintOutPrint(); if (printername.Length > 0) { PrintOutNet.DefaultPrinterName = printername; } prw.Print(rp.MetaFile); #endif // PrintOutWinForms prw = new PrintOutWinForms(); // prw.OptimizeWMF = WMFOptimization.Gdiplus; } } } finally { if (deletereport) { if (filename.Length > 0) { System.IO.File.Delete(filename); } } } } catch (Exception E) { if (!dothrow) { int i; string amessage = E.Message + (char)13 + (char)10; for (i = 0; i < args.Length; i++) { amessage = amessage + (char)13 + (char)10 + "Arg" + i.ToString() + ":" + args[i]; } Strings astrings = GetParams(); for (i = 0; i < astrings.Count; i++) { amessage = amessage + (char)13 + (char)10 + astrings[i]; } #if FORMS MessageBox.Show(amessage, "Error"); #else System.Console.WriteLine(amessage); #endif System.Console.WriteLine(E.StackTrace); } else { PrintParams(); throw; } // Variant.WriteStringToUTF8Stream(amessage,System.Console.OpenStandardError()); } }