2009-08-12 19 views
5

Bir BlockUIContainer'daki ItemsControl nedeniyle yüksekliğe değişen bir FlowDocument var. Bazı durumlarda, ItemsControl sayfa yüksekliğinin ötesine uzanır. FlowDocument'i gerektiğinde yazdırmadan hemen önce bir sayfaya sığdırmak için bir yol var mı (8.5 "X 11")?WPF FlowDocument Ölçeğine Sığacak Sayfa

Şu andan itibaren, FlowDocument 'doc' adlı ve ben kullanıyorum baskı için bir yöntemdir:

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

bu sorun için bir çözüm buldular mı? –

+0

Hayır Hala yok. Baştan sona yapmaktan kaçınmak istediğim şeyleri kontrole yerleştirmek ve yerleştirmek zorundaydım. – Johnathan1

cevap

3

Geç biraz, ama burada ben ile geldi çözümdür biliyoruz.

İlk olarak, bizim için belge sayfalarını oluşturacak bir sarıcı oluşturuyoruz. Her sayfa, geri dönmeden önce ona uygulanan bir ölçek dönüşümüne sahip olacaktır. İlgilenirsen burada,

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

biz ölçeği belirlenen nasıl: Bunu kullanarak

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

oldukça basittir. Bizim örneğimizde, belge sayfa genişliğini aşacak şekilde genişletildi, ancak sayfa yüksekliğine uyum sağlamak için kolayca değiştirilebilir.

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; 
     } 
    } 
İlgili konular