2010-03-01 4 views
14

Tôi có một ListView trong ứng dụng WPF bị ràng buộc vào một tập hợp các tác vụ cần thực hiện (danh sách việc cần làm). Tôi muốn người dùng có thể in danh sách của họ và đã tạo mã sau dựa trên nguyên tắc MSDN. (Đây là bước đột phá đầu tiên của tôi vào việc in ấn)Tại sao bảng flowdocument này luôn in 2 cột

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; 
} 

Khi tôi cố gắng in với mã sau, tôi luôn chia trang ở giữa với 2 cột (mỗi cột chứa 3 cột). Tôi đã thử các giá trị GridLength khác nhau nhưng không thành công.

printDialog.PrintDocument(((IDocumentPaginatorSource)StatusBoardViewModel 
       .GetPrintDocument()) 
       .DocumentPaginator 
      ,"Flow Document Print Job"); 

Trả lời

19

Tôi đoán cách tốt nhất để có câu trả lời là từ bỏ và hỏi, sau đó bạn tự tìm thấy.

Vấn đề nằm trong dòng để in các trang, chứ không phải bản thân flowdoc. Theo mặc định, chúng in với 2 cột. Mã chỉnh là (điều này cũng giao dịch với biên độ và vùng in):

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"); 

} 

Bằng cách này tôi thấy điều này trong Matthew MacDonald của "Pro WPF trong C# 2008" mà tôi rất khuyên bạn nên.

3

Cảm ơn thông tin. Tôi đã sửa lỗi bằng cách chỉ đặt độ rộng cột như sau: flowDoc.ColumnWidth = pageSize.Width

FYI không bao giờ cố gắng nhận trợ giúp từ netframeworkdev hoặc .Net Framework Phát triển b/c họ không bao giờ có câu trả lời hay. Tôi muốn công cụ tìm kiếm của tôi sẽ chỉ cho tôi tại StackOverflow thay vì trang web vô giá trị đó. StackOverflow luôn có câu trả lời. :) Cảm ơn một lần nữa.

(Chúc các bạn chỉ có thể chặn các trang web từ bao giờ hiển thị trong kết quả tìm kiếm của bạn, bạn có biết làm thế nào để làm điều đó xin vui lòng cho tôi biết.)

+0

Tôi không thể cho bạn biết mức độ thường xuyên Tôi ước mình có thể che giấu một trang web từ tôi kết quả tìm kiếm. –