/// <summary> /// Print the data that is shown in a grid /// </summary> /// <param name="APrintApplication">The print application to use - either Word or Excel</param> /// <param name="APreviewOnly">True if preview, False to print without preview</param> /// <param name="AModule">The module that is making the call</param> /// <param name="ATitleText">Title for the page</param> /// <param name="AGrid">A grid displaying data</param> /// <param name="AGridColumnOrder">Zero-based grid column number order for column titles</param> /// <param name="ATableColumnOrder">Zero-based table column order that matches the grid columns</param> /// <param name="ABaseFilter">A filter that is always applied to the grid even when it is apparently showing all rows</param> public static void PrintGrid(TPrintUsing APrintApplication, bool APreviewOnly, TModule AModule, string ATitleText, TSgrdDataGrid AGrid, int[] AGridColumnOrder, int[] ATableColumnOrder, string ABaseFilter = "") { TFormDataKeyDescriptionList recordList = new TFormDataKeyDescriptionList(); List <TFormData> formDataList = new List <TFormData>(); string msgTitle = Catalog.GetString("Print"); int numColumns = Math.Min(AGridColumnOrder.GetLength(0), ATableColumnOrder.GetLength(0)); // Title of the document recordList.Title = ATitleText; // First two columns: key and description recordList.KeyTitle = AGrid.Columns[AGridColumnOrder[0]].HeaderCell.ToString(); recordList.DescriptionTitle = AGrid.Columns[AGridColumnOrder[1]].HeaderCell.ToString(); // Other columns recordList.Field3Title = numColumns > 2 ? AGrid.Columns[AGridColumnOrder[2]].HeaderCell.ToString() : string.Empty; recordList.Field4Title = numColumns > 3 ? AGrid.Columns[AGridColumnOrder[3]].HeaderCell.ToString() : string.Empty; recordList.Field5Title = numColumns > 4 ? AGrid.Columns[AGridColumnOrder[4]].HeaderCell.ToString() : string.Empty; // Look at each data row and set values for key, description and other columns and add to the Form Data list DataView dv = ((DevAge.ComponentModel.BoundDataView)AGrid.DataSource).DataView; for (int i = 0; i < dv.Count; i++) { TFormDataKeyDescription record = new TFormDataKeyDescription(); DataRowView drv = dv[i]; record.Key = GetPrintableText(AGrid, AGridColumnOrder[0], drv.Row[ATableColumnOrder[0]]); record.Description = GetPrintableText(AGrid, AGridColumnOrder[1], drv.Row[ATableColumnOrder[1]]); record.Field3 = numColumns > 2 ? GetPrintableText(AGrid, AGridColumnOrder[2], drv.Row[ATableColumnOrder[2]]) : string.Empty; record.Field4 = numColumns > 3 ? GetPrintableText(AGrid, AGridColumnOrder[3], drv.Row[ATableColumnOrder[3]]) : string.Empty; record.Field5 = numColumns > 4 ? GetPrintableText(AGrid, AGridColumnOrder[4], drv.Row[ATableColumnOrder[4]]) : string.Empty; recordList.Add(record); } formDataList.Add(recordList); // Work out the template file name to use. This is based on the number of columns and any special preferences passed in. // For the present time the options are limited! string formName = "OM Print Grid "; formName += (APrintApplication == TPrintUsing.Excel ? "X" : "W"); if (numColumns <= 4) { formName += "P"; // Portrait } else { formName += "L"; // Landscape } // Get the template from the db or file system string templatePath = GetTemplatePath(AModule, APrintApplication, formName); // Tell the user if we didn't find it - and quit if (templatePath.Length == 0) { MessageBox.Show(Catalog.GetString("Could not find the template to use for printing."), msgTitle); return; } #if USING_TEMPLATER string targetDir = TTemplaterAccess.GetFormLetterBaseDirectory(TModule.mPartner); TTemplaterAccess.AppendUserAndDateInfo(ref targetDir, Path.GetFileNameWithoutExtension(templatePath)); // Set up the last few data items recordList.PrintedBy = UserInfo.GUserInfo.UserID; recordList.Date = StringHelper.DateToLocalizedString(DateTime.Now, true, true); recordList.Filename = Path.Combine(targetDir, ATitleText + Path.GetExtension(templatePath)); // And the sub-title int nGridRowsData = AGrid.Rows.Count - 1; int nFullViewRowsData = new DataView(dv.Table, ABaseFilter, "", DataViewRowState.CurrentRows).Count; if (nGridRowsData == nFullViewRowsData) { recordList.SubTitle = String.Format( Catalog.GetString("The table below shows all {0} rows of data in the data table."), nGridRowsData); } else { recordList.SubTitle = String.Format( Catalog.GetString("The table below shows a filtered selection of {0} out of {1} rows of data in the complete data table."), nGridRowsData, nFullViewRowsData); } // Send to Templater to print! try { bool allDocumentsOpened; bool printOnCompletion; string outputPath = TTemplaterAccess.PrintTemplaterDocument(TModule.mPartner, formDataList, templatePath, false, APreviewOnly, !APreviewOnly, out allDocumentsOpened, out printOnCompletion, targetDir, ATitleText); if (printOnCompletion) { string[] files = null; if (Directory.Exists(outputPath)) { files = Directory.GetFiles(outputPath); } if ((files == null) || (files.Length != 1)) { // Where has it gone?? string msg = string.Format(Catalog.GetString( "Unexpectedly failed to find the document to print in the folder{0}'{1}'"), Environment.NewLine, outputPath); MessageBox.Show(msg, Catalog.GetString("Print Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string printerName; // request print information from user using (PrintDialog pd = new PrintDialog()) { if (pd.ShowDialog() != DialogResult.OK) { return; } // Remember this for use lower down printerName = String.Format("\"{0}\"", pd.PrinterSettings.PrinterName); } TTemplaterAccess.RunPrintJob(printerName, files[0]); } } catch (Exception ex) { MessageBox.Show(Catalog.GetString("An error occurred while trying to print the document. The error message was: ") + ex.Message, msgTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } #endif }
/// <summary> /// Prints from either a grid or a custom print. /// </summary> /// <param name="ARecordList"></param> /// <param name="ANumColumns"></param> /// <param name="APrintApplication"></param> /// <param name="ARowsCount"></param> /// <param name="ADv"></param> /// <param name="ABaseFilter"></param> /// <param name="APreviewOnly"></param> public static void PrintRecordList(TFormDataKeyDescriptionList ARecordList, int ANumColumns, TPrintUsing APrintApplication, int ARowsCount, DataView ADv, string ABaseFilter, bool APreviewOnly) { List <TFormData> formDataList = new List <TFormData>(); string msgTitle = Catalog.GetString("Print"); formDataList.Add(ARecordList); // Work out the template file name to use. string formName; //Chooses the template depending on the number of columns formName = "OM Print Grid " + ANumColumns + " "; formName += (APrintApplication == TPrintUsing.Excel ? "X" : "W"); if (ANumColumns <= 4) { formName += "P"; // Portrait } else { formName += "L"; // Landscape } // Get the template from the db or file system string templatePath = GetTemplatePath(APrintApplication, formName); // Tell the user if we didn't find it - and quit if (templatePath.Length == 0) { MessageBox.Show(Catalog.GetString("Could not find the template to use for printing."), msgTitle); return; } #if USING_TEMPLATER string targetDir = TTemplaterAccess.GetFormLetterBaseDirectory(TModule.mPartner); TTemplaterAccess.AppendUserAndDateInfo(ref targetDir, Path.GetFileNameWithoutExtension(templatePath)); // Set up the last few data items ARecordList.PrintedBy = UserInfo.GUserInfo.UserID; ARecordList.Date = StringHelper.DateToLocalizedString(DateTime.Now, true, true); ARecordList.Filename = Path.Combine(targetDir, ARecordList.Title + Path.GetExtension(templatePath)); // And the sub-title int nGridRowsData = ARowsCount; //AGrid.Rows.Count - 1; int nFullViewRowsData = new DataView(ADv.Table, ABaseFilter, "", DataViewRowState.CurrentRows).Count; if (nGridRowsData == nFullViewRowsData) { ARecordList.SubTitle = String.Format( Catalog.GetString("The table below shows all {0} rows of data in the data table."), nGridRowsData); } else { ARecordList.SubTitle = String.Format( Catalog.GetString("The table below shows a filtered selection of {0} out of {1} rows of data in the complete data table."), nGridRowsData, nFullViewRowsData); } // Send to Templater to print! try { bool allDocumentsOpened; bool printOnCompletion; string outputPath = TTemplaterAccess.PrintTemplaterDocument(TModule.mPartner, formDataList, templatePath, false, APreviewOnly, !APreviewOnly, out allDocumentsOpened, out printOnCompletion, targetDir, ARecordList.Title); if (printOnCompletion) { string[] files = null; if (Directory.Exists(outputPath)) { files = Directory.GetFiles(outputPath); } if ((files == null) || (files.Length != 1)) { // Where has it gone?? string msg = string.Format(Catalog.GetString( "Unexpectedly failed to find the document to print in the folder{0}'{1}'"), Environment.NewLine, outputPath); MessageBox.Show(msg, Catalog.GetString("Print Error"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } string printerName; // request print information from user using (PrintDialog pd = new PrintDialog()) { if (pd.ShowDialog() != DialogResult.OK) { return; } // Remember this for use lower down printerName = String.Format("\"{0}\"", pd.PrinterSettings.PrinterName); } TTemplaterAccess.RunPrintJob(printerName, files[0]); } } catch (Exception ex) { MessageBox.Show(Catalog.GetString("An error occurred while trying to print the document. The error message was: ") + ex.Message, msgTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } #endif }