public void printDG(ListBox listBox, string title) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontStyle = listBox.FontStyle; p.FontFamily = listBox.FontFamily; p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 0; fd.Blocks.Add(table); printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); } }
public void printDG(DataGrid dataGrid, string title) { PrintDialog printDialog = new PrintDialog(); FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontStyle = dataGrid.FontStyle; p.FontFamily = dataGrid.FontFamily; p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r1 = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 0; var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList(); for (int j = 0; j < headerList.Count; j++) { r1.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j])))); r1.Cells[j].ColumnSpan = 4; r1.Cells[j].Padding = new Thickness(4); r1.Cells[j].BorderBrush = Brushes.Black; r1.Cells[j].FontWeight = FontWeights.Bold; r1.Cells[j].Background = Brushes.DarkGray; r1.Cells[j].Foreground = Brushes.White; r1.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1); } tableRowGroup.Rows.Add(r1); table.RowGroups.Add(tableRowGroup); for (int i = 0; i < dataGrid.Items.Count; i++) { DataRowView row = (DataRowView)dataGrid.Items.GetItemAt(i); table.BorderBrush = Brushes.Gray; table.BorderThickness = new Thickness(1, 1, 0, 0); table.FontStyle = dataGrid.FontStyle; table.FontFamily = dataGrid.FontFamily; table.FontSize = 13; tableRowGroup = new TableRowGroup(); r1 = new TableRow(); for (int j = 0; j < row.Row.ItemArray.Count(); j++) { r1.Cells.Add(new TableCell(new Paragraph(new Run(row.Row.ItemArray[j].ToString())))); r1.Cells[j].ColumnSpan = 4; r1.Cells[j].Padding = new Thickness(4); r1.Cells[j].BorderBrush = Brushes.DarkGray; //r1.Cells[j].Background = Brushes.White; r1.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1); } tableRowGroup.Rows.Add(r1); table.RowGroups.Add(tableRowGroup); } fd.Blocks.Add(table); printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); }
// https://stackoverflow.com/questions/5661034/printing-in-c-sharp-wpf // Test code private void PrintSchedule(object sender, RoutedEventArgs e) { PrintDialog pDialog = new PrintDialog(); if (pDialog.ShowDialog() == true) { //print landscape pDialog.PrintTicket.PageOrientation = PageOrientation.Landscape; FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run("this is a title")); // fd.Blocks.Add(ScheduleDG); Table tab = new Table(); TableRowGroup tg = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = pDialog.PrintableAreaWidth; fd.PageHeight = pDialog.PrintableAreaHeight; fd.BringIntoView(); fd.ColumnWidth = 800; for (int i = 0; i < 10; i++) { r.Cells.Add(new TableCell(new Paragraph(new Run("Header")))); r.Cells[i].ColumnSpan = 4; r.Cells[i].Padding = new Thickness(4); r.Cells[i].BorderBrush = Brushes.Black; r.Cells[i].FontWeight = FontWeights.Bold; r.Cells[i].Background = Brushes.DarkGray; r.Cells[i].Foreground = Brushes.White; r.Cells[i].BorderThickness = new Thickness(1, 1, 1, 1); } tg.Rows.Add(r); tab.RowGroups.Add(tg); fd.Blocks.Add(p); fd.Blocks.Add(tab); // pDialog.PrintVisual(ScheduleDG, "GAAAHH!"); pDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); } }
public void printDG(DataGrid dataGrid, string title) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontStyle = dataGrid.FontStyle; p.FontFamily = dataGrid.FontFamily; p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 0; var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList(); List <dynamic> bindList = new List <dynamic>(); for (int j = 0; j < headerList.Count; j++) { r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j])))); r.Cells[j].ColumnSpan = 4; r.Cells[j].Padding = new Thickness(4); r.Cells[j].BorderBrush = Brushes.Black; r.Cells[j].FontWeight = FontWeights.Bold; r.Cells[j].Background = Brushes.DarkGray; r.Cells[j].Foreground = Brushes.White; r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1); var binding = (dataGrid.Columns[j] as DataGridBoundColumn).Binding as Binding; bindList.Add(binding.Path.Path); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); for (int i = 0; i < dataGrid.Items.Count; i++) { dynamic row; if (dataGrid.ItemsSource.ToString().ToLower() == "system.data.linqdataview") { row = (DataRowView)dataGrid.Items.GetItemAt(i); } else { row = (Result)dataGrid.Items.GetItemAt(i); } table.BorderBrush = Brushes.Gray; table.BorderThickness = new Thickness(1, 1, 0, 0); table.FontStyle = dataGrid.FontStyle; table.FontFamily = dataGrid.FontFamily; table.FontSize = 13; tableRowGroup = new TableRowGroup(); r = new TableRow(); for (int j = 0; j < row.Rows.ItemArray.Count(); j++) { if (dataGrid.ItemsSource.ToString().ToLower() == "system.data.linqdataview") { r.Cells.Add(new TableCell(new Paragraph(new Run(row.Row.ItemArray[j].ToString())))); } else { r.Cells.Add(new TableCell(new Paragraph(new Run(row.GetType().GetProperty(bindList[j]).GetValue(row, null))))); } r.Cells[j].ColumnSpan = 4; r.Cells[j].Padding = new Thickness(4); r.Cells[j].BorderBrush = Brushes.DarkGray; r.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); } fd.Blocks.Add(table); printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); } }
public void printDG(DataGrid dataGrid, string title) { PrintDialog printDialog = new PrintDialog(); FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontStyle = dataGrid.FontStyle; p.FontFamily = dataGrid.FontFamily; p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 0; table.FlowDirection = FlowDirection.RightToLeft; var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList(); headerList.Reverse(); List<BindingExpression> bindList = new List<BindingExpression>(); if (dataGrid.ItemsSource != null) { foreach (object item in dataGrid.ItemsSource) { DataGridRow row1 = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(item); if (row1 != null) { foreach (BindingExpression binding in row1.BindingGroup.BindingExpressions) { MessageBox.Show(binding.ToString()); bindList.Add(binding); } } } } for (int j = 0; j < headerList.Count; j++) { r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j])))); r.Cells[j].ColumnSpan = 4; r.Cells[j].Padding = new Thickness(4); r.Cells[j].BorderBrush = Brushes.Black; r.Cells[j].FontWeight = FontWeights.Bold; r.Cells[j].Background = Brushes.DarkGray; r.Cells[j].Foreground = Brushes.White; r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); int count; for (int i = 0; i < dataGrid.Items.Count; i++) { //if(dataGrid.get) dynamic row; if (dataGrid.ItemsSource.ToString() == "") { row = (BalanceClient)dataGrid.Items.GetItemAt(i); } else { row = (DataRowView)dataGrid.Items.GetItemAt(i); } table.BorderBrush = Brushes.Gray; table.BorderThickness = new Thickness(1, 1, 0, 0); table.FontStyle = dataGrid.FontStyle; table.FontFamily = dataGrid.FontFamily; table.FontSize = 13; tableRowGroup = new TableRowGroup(); r = new TableRow(); for (int j = 0; j < bindList.Count; j++) { r.Cells.Add(new TableCell(new Paragraph(new Run(row.bindList[j].ToString())))); r.Cells[j].ColumnSpan = 4; r.Cells[j].Padding = new Thickness(4); r.Cells[j].BorderBrush = Brushes.DarkGray; //r1.Cells[j].Background = Brushes.White; r.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); } fd.Blocks.Add(table); printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); }
public void PrintPersonenLijst(string title, List <string> headerList, List <List <string> > cellList) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 1; for (int j = 0; j < headerList.Count; j++) { r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j])))); r.Cells[j].ColumnSpan = 9; //r.Cells[j].Padding = new Thickness(9); r.Cells[j].BorderBrush = Brushes.Black; r.Cells[j].FontWeight = FontWeights.Bold; r.Cells[j].Background = Brushes.LightGray; r.Cells[j].Foreground = Brushes.Black; r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); for (int i = 0; i < cellList.Count; i++) { TableRowGroup g = new TableRowGroup(); TableRow ro = new TableRow(); for (int j = 0; j < cellList[i].Count; j++) { ro.Cells.Add(new TableCell(new Paragraph(new Run(cellList[i][j])))); } for (int k = 0; k < cellList[0].Count; k++) { ro.Cells[k].ColumnSpan = 9; //ro.Cells[k].Padding = new Thickness(9); ro.Cells[k].BorderBrush = Brushes.Black; ro.Cells[k].FontWeight = FontWeights.Bold; ro.Cells[k].BorderThickness = new Thickness(1, 1, 1, 1); } g.Rows.Add(ro); table.RowGroups.Add(g); Debug.WriteLine(cellList[0].Count.ToString()); } fd.Blocks.Add(table); try { printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); } catch { MessageBox.Show("Probleem met afdrukken, herstart het programma en probeer het nog eens!", "Probleem met afdrukken"); } } }
public void PrintRoundRobinOfMatchLijst(string title, List <string> headerList, List <List <string> > cellList, List <string> afwezigen = null) { if (afwezigen == null) { afwezigen = new List <string>(); } PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == true) { FlowDocument fd = new FlowDocument(); Paragraph p = new Paragraph(new Run(title)); p.FontSize = 18; fd.Blocks.Add(p); Table table = new Table(); TableRowGroup tableRowGroup = new TableRowGroup(); TableRow r = new TableRow(); fd.PageWidth = printDialog.PrintableAreaWidth; fd.PageHeight = printDialog.PrintableAreaHeight; fd.BringIntoView(); fd.TextAlignment = TextAlignment.Center; fd.ColumnWidth = 500; table.CellSpacing = 0; for (int j = 0; j < headerList.Count; j++) { r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j])))); r.Cells[j].ColumnSpan = 4; r.Cells[j].Padding = new Thickness(4); r.Cells[j].BorderBrush = Brushes.Black; r.Cells[j].FontWeight = FontWeights.Bold; r.Cells[j].Background = Brushes.LightGray; r.Cells[j].Foreground = Brushes.Black; r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1); } tableRowGroup.Rows.Add(r); table.RowGroups.Add(tableRowGroup); for (int i = 0; i < cellList.Count; i++) { TableRowGroup g = new TableRowGroup(); TableRow ro = new TableRow(); if (cellList[i][0].StartsWith("Ronde ")) { Debug.WriteLine("dit is een ronde cell, voeg lege toe.."); foreach (var item in cellList[i]) { ro.Cells.Add(new TableCell(new Paragraph(new Run("")))); Debug.WriteLine("lege cell toegevoegd!"); } g.Rows.Add(ro); table.RowGroups.Add(g); g = new TableRowGroup(); ro = new TableRow(); } for (int j = 0; j < cellList[i].Count; j++) { ro.Cells.Add(new TableCell(new Paragraph(new Run(cellList[i][j])))); } for (int k = 0; k < cellList[0].Count; k++) { ro.Cells[k].ColumnSpan = 4; ro.Cells[k].Padding = new Thickness(4); ro.Cells[k].BorderBrush = Brushes.Black; ro.Cells[k].FontWeight = FontWeights.Bold; ro.Cells[k].BorderThickness = new Thickness(1, 1, 1, 1); Debug.WriteLine(cellList[i][k]); if (cellList[i][0].StartsWith("Ronde ")) { ro.Cells[k].Background = Brushes.AliceBlue; ro.Cells[2].Foreground = Brushes.AliceBlue; } } g.Rows.Add(ro); table.RowGroups.Add(g); } Table tableafw = new Table(); TableRowGroup tableRowGroupafw = new TableRowGroup(); TableRow rafw = new TableRow(); tableafw.CellSpacing = 0; rafw.Cells.Add(new TableCell(new Paragraph(new Run("")))); rafw.Cells.Add(new TableCell(new Paragraph(new Run("Afwezigen")))); rafw.Cells.Add(new TableCell(new Paragraph(new Run("")))); rafw.Cells[0].ColumnSpan = 4; rafw.Cells[0].Padding = new Thickness(4); rafw.Cells[1].ColumnSpan = 4; rafw.Cells[1].Padding = new Thickness(4); rafw.Cells[2].ColumnSpan = 4; rafw.Cells[2].Padding = new Thickness(4); rafw.Cells[1].BorderBrush = Brushes.Black; rafw.Cells[1].FontWeight = FontWeights.Bold; rafw.Cells[1].Background = Brushes.LightGray; rafw.Cells[1].Foreground = Brushes.Black; rafw.Cells[1].BorderThickness = new Thickness(1, 1, 1, 1); tableRowGroupafw.Rows.Add(rafw); tableafw.RowGroups.Add(tableRowGroupafw); for (int i = 0; i < afwezigen.Count; i++) { TableRowGroup g = new TableRowGroup(); TableRow ro = new TableRow(); ro.Cells.Add(new TableCell(new Paragraph(new Run("")))); ro.Cells.Add(new TableCell(new Paragraph(new Run(afwezigen[i].ToString())))); ro.Cells.Add(new TableCell(new Paragraph(new Run("")))); ro.Cells[0].Padding = new Thickness(4); ro.Cells[0].ColumnSpan = 4; ro.Cells[1].Padding = new Thickness(4); ro.Cells[1].ColumnSpan = 4; ro.Cells[2].Padding = new Thickness(4); ro.Cells[2].ColumnSpan = 4; ro.Cells[1].BorderBrush = Brushes.Black; ro.Cells[1].BorderThickness = new Thickness(1); ro.Cells[1].FontWeight = FontWeights.Bold; ro.Cells[1].Foreground = Brushes.Black; g.Rows.Add(ro); tableafw.RowGroups.Add(g); } fd.Blocks.Add(table); if (afwezigen.Count > 0) { fd.Blocks.Add(tableafw); } try { printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, ""); } catch { MessageBox.Show("Probleem met afdrukken, herstart het programma en probeer het nog eens!", "Probleem met afdrukken"); } } }