[ACCEPTED]-Printing in C# (wpf)-printing
Printing in WPF is both simple and not so 7 simple.
It starts with basically with one 6 or two lines of code you are printing already.
private void PrintBtn_Click(object sender, RoutedEventArgs e)
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDialog.PrintVisual(grid, "My First Print Job");
}
}
However, pagination 5 in WPF is not done with a single line of 4 code. Then you get into FlowDocuments and 3 similar more advanced topics.
If you are 2 making a non-commercial tool for yourself, consider 1 iTextSharp which is very good too.
If you want to print all records from datagrid 17 in WPF.In which I have create flow document 16 using code you can understand logic and 15 make it according to own requirement.After 14 a lot of working.I have done code recently.It 13 is tested code.It will print every datagrid 12 with all records.It is easy and simple code.You 11 would add a class.If you want to decorate 10 a datagrid then go to PrintDG class then 9 decorate it acording to own requirement.
Follow 8 these steps.
Step1:Add these references 7 on top.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
Step2: Add class PrintDG.cs.
public class PrintDG
{
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();
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);
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();
r = new TableRow();
for (int j = 0; j < row.Row.ItemArray.Count(); j++)
{
r.Cells.Add(new TableCell(new Paragraph(new Run(row.Row.ItemArray[j].ToString()))));
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, "");
}
}
}
Step2: Then 6 go to print button click event and create 5 object of PrintDG class then call printDG 4 pass to It two parameters datagridname and 3 title.
Like :
private void print_button_Click(object sender, RoutedEventArgs e)
{
PrintDG print = new PrintDG();
print.printDG(datagridName, "Title");
}
If any erorr occure during 2 execution tell me I will solve It.The is 1 running code only you copy and past.
These links may help you in understanding 1 how printing works and what exactly to use:
http://www.charlespetzold.com/blog/2006/02/201111.html
http://msdn.microsoft.com/en-us/library/ms742418(v=vs.100).aspx
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.