9

I am writing code to print from a FlowDocument.

        PrintDialog printDialog = new PrintDialog();
        bool? result = printDialog.ShowDialog();
        if (result == true)
        {
            FlowDocument fd = new FlowDocument();
            fd.Blocks.Add(new Paragraph(new Run(String.Format("Message:\r\n{0}\r\n", txtMessage.Text))));
            fd.PageHeight = printDialog.PrintableAreaHeight;
            fd.PageWidth = printDialog.PrintableAreaWidth;
            printDialog.PrintDocument((fd as IDocumentPaginatorSource).DocumentPaginator, "print test");
        }

This code will print multiple columns in one page. How to avoid this?

Nishant
  • 443
  • 3
  • 9

2 Answers2

17

I figured out. I need to set the ColumnWidth of FlowDocument.

fd.PagePadding = new Thickness(50);
fd.ColumnGap = 0;
fd.ColumnWidth = printDialog.PrintableAreaWidth; 
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
Nishant
  • 443
  • 3
  • 9
  • 2
    Thanks for this, I have short documents and was sitting here scratching my head wondering why only half of my page was being used. I wonder why they took two columns as default? – Mishax Sep 23 '13 at 11:56
0

In case, there is no printDialog involved (e.g. Writing a XML-File), this solution worked for me:

        .PagePadding = New Thickness(50)
        .ColumnGap = 0
        .PageWidth = 21 * 96 / 2.54
        .PageHeight = 29.7 * 96 / 2.54

        .ColumnWidth = .PageWidth - .PagePadding.Left - .PagePadding.Right
DrMarbuse
  • 804
  • 11
  • 30