2009-08-12 40 views
5

Tôi có một FlowDocument thay đổi về chiều cao do một ItemsControl trong một BlockUIContainer. Trong một số trường hợp, ItemsControl mở rộng vượt quá chiều cao của trang. Có cách nào để mở rộng FlowDocument để phù hợp với một trang (8,5 "X 11") ngay trước khi in nếu cần?WPF FlowDocument Scale to Fit Trang

Tính đến ngay bây giờ, các FlowDocument được đặt tên là 'doc' và phương pháp để in Tôi đang sử dụng là:

private void Print_Click(object sender, RoutedEventArgs e) 
    { 

     PrintDialog pd = new PrintDialog(); 
     doc.PageHeight = pd.PrintableAreaHeight; 
     doc.PageWidth = pd.PrintableAreaWidth; 
     doc.ColumnGap = 0; 
     doc.ColumnWidth = pd.PrintableAreaWidth; 
     IDocumentPaginatorSource dps = doc; 
     pd.PrintDocument(dps.DocumentPaginator, "Sheet"); 
    } 
+0

Bạn có tìm thấy giải pháp cho vấn đề này không? –

+0

Không, tôi vẫn chưa có. Tôi đã phải giải quyết và đặt một wrappanel bên trong itemscontrol đó là những gì tôi muốn tránh làm từ đầu. – Johnathan1

Trả lời

3

Tôi biết đó là một hơi muộn, nhưng đây là giải pháp tôi đã đưa ra.

Trước tiên, chúng tôi tạo trình bao bọc sẽ tạo trang tài liệu cho chúng tôi. Mỗi trang sẽ có quy mô chuyển đổi được áp dụng cho nó trước khi trả lại.

public class FittedDocumentPaginator : DocumentPaginator 
{ 
    public DocumentPaginator Base { get; private set; } 
    public double Scale { get; private set; } 
    private readonly ScaleTransform _sTransform; 

    public FittedDocumentPaginator(DocumentPaginator baseDp, double scale) 
    { 
     if (baseDp == null) 
      throw new ArgumentNullException("baseDp"); 

     Base = baseDp; 
     Scale = scale; 
     _sTransform = new ScaleTransform(scale, scale); 
    } 

    public override DocumentPage GetPage(int pageNumber) 
    { 
     var page = Base.GetPage(pageNumber); 
     ((ContainerVisual)page.Visual).Transform = _sTransform; 

     return page; 
    } 

    public override bool IsPageCountValid 
    { 
     get { return Base.IsPageCountValid; } 
    } 

    public override int PageCount 
    { 
     get { return Base.PageCount; } 
    } 

    public override Size PageSize 
    { 
     get { return Base.PageSize; } 
     set { Base.PageSize = value; } 
    } 

    public override IDocumentPaginatorSource Source 
    { 
     get { return Base.Source; } 
    } 
} 

Sử dụng nó khá đơn giản:

private void PrintDocument(PrintDialog pd, FlowDocument document, double scale, string title) 
    { 
     DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator; 
     FittedDocumentPaginator fdp = new FittedDocumentPaginator(dp, scale); 

     pd.PrintDocument(fdp, title); 
    } 

Nếu bạn quan tâm, đây là cách chúng tôi xác định quy mô. Trong trường hợp của chúng tôi, tài liệu đã được mở rộng qua chiều rộng trang, nhưng nó có thể dễ dàng được sửa đổi để phù hợp với chiều cao của trang.

private void Print(FlowDocument document, string title, double documentWidth) 
    { 
     var pd = new PrintDialog(); 

     if (pd.ShowDialog() == true) 
     { 
      // Set the printing margins. 
      Thickness pageMargins = document.PagePadding; 
      document.PagePadding = new Thickness(15.0); 

      // In our case, the document's width is NaN so we have to navigate the visual tree to get the ActualWidth, which is represented by 'documentWidth'. 
      double scale = documentWidth/pd.PrintableAreaWidth; 

      if (scale < 1.0) 
      { 
       // The report fits on the page just fine. Don't scale. 
       scale = 1.0; 
      } 

      double invScale = 1/scale; 

      document.PageHeight = pd.PrintableAreaHeight * scale; 
      document.PageWidth = pd.PrintableAreaWidth * scale; 

      DocumentPaginator dp = ((IDocumentPaginatorSource)document).DocumentPaginator; 
      FittedDocumentPaginator fdp = new FittedDocumentPaginator(dp, invScale); 

      pd.PrintDocument(fdp, title); 

      // Restore the original values so the GUI isn't altered. 
      document.PageHeight = Double.NaN; 
      document.PageWidth = Double.NaN; 
      document.PagePadding = pageMargins; 
     } 
    } 
Các vấn đề liên quan