Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this flowdocument table always printing 2 columns

I have a ListView in my WPF app that is bound to a collection of tasks to perform (A to-do list). I want the user to be able to print their list and have created the following code based on the MSDN guidelines. (This my first foray into printing)

public FlowDocument GetPrintDocument()
{
    FlowDocument flowDoc = new FlowDocument();
    Table table = new Table();

    int numColumns = 3;

    flowDoc.Blocks.Add(table);

    for(int x=0;x<numColumns;x++)
    {
        table.Columns.Add(new TableColumn());
    }
    GridLengthConverter glc = new GridLengthConverter();
    table.Columns[0].Width = (GridLength)glc.ConvertFromString("300");
    table.Columns[1].Width = (GridLength)glc.ConvertFromString("50");
    table.Columns[2].Width = (GridLength)glc.ConvertFromString("50");

    table.RowGroups.Add(new TableRowGroup());

    table.RowGroups[0].Rows.Add(new TableRow());
    // store current working row for reference
    TableRow currentRow = table.RowGroups[0].Rows[0];

    currentRow.FontSize = 16;
    currentRow.FontWeight = FontWeights.Bold;

    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Subject"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Due Date"))));
    currentRow.Cells.Add(new TableCell(new Paragraph(new Run("Urgency"))));

    for (int i = 1; i < issues.Count+1; i++)
    {
        table.RowGroups[0].Rows.Add(new TableRow());
        currentRow = table.RowGroups[0].Rows[i];
        currentRow.FontSize = 12;
        currentRow.FontWeight = FontWeights.Normal;

        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssSubject))));
        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssDueDate.Date.ToString()))));
        currentRow.Cells.Add(new TableCell
                            (new Paragraph
                            (new Run
                            (issues[i - 1].IssUrgency.ToString()))));
    }
    return flowDoc;
} 

When I try to print with the following code I always have my page split down the middle with 2 columns (Each containing the 3 columns of the table). I have tried different GridLength values but had no success.

printDialog.PrintDocument(((IDocumentPaginatorSource)StatusBoardViewModel
               .GetPrintDocument())
               .DocumentPaginator 
            ,"Flow Document Print Job");
like image 920
Mike B Avatar asked Mar 01 '10 21:03

Mike B


2 Answers

I guess the best way to get an answer is to give up and ask, then you find it yourself.

The issue was in the line to print the pages, not the flowdoc itself. By default they print with 2 columns. The corrected code is (this also deals with the margin and printable area):

PrintDialog printDialog = new PrintDialog();

if (printDialog.ShowDialog() == true)
{

    FlowDocument flowDoc = statusBoardViewModel.GetPrintDocument();

    flowDoc.PageHeight = printDialog.PrintableAreaHeight;
    flowDoc.PageWidth = printDialog.PrintableAreaWidth;
    flowDoc.PagePadding = new Thickness(25);

    flowDoc.ColumnGap = 0;

    flowDoc.ColumnWidth = (flowDoc.PageWidth - 
                           flowDoc.ColumnGap - 
                           flowDoc.PagePadding.Left -  
                           flowDoc.PagePadding.Right);

    printDialog.PrintDocument(((IDocumentPaginatorSource)flowDoc)
                             .DocumentPaginator,
                             "Task Manager Print Job");

}

By the way I found this in Matthew MacDonald's "Pro WPF in C# 2008" which I highly recommend.

like image 108
Mike B Avatar answered Nov 10 '22 14:11

Mike B


Thanks for the info. I fixed it by just setting the columnwidth like:

flowDoc.ColumnWidth = pageSize.Width

FYI don't ever try to get help from netframeworkdev or .Net Framework Develop b/c they never have good answers. I wish my search engine would have pointed me at StackOverflow instead of that worthless site. StackOverflow always has the answers. :) Thanks again.

(Wish you could just block sites from ever showing in your search results, you know how to do that please tell me.)

like image 22
Kasper Avatar answered Nov 10 '22 13:11

Kasper