2017-04-03 12 views
1

XAML/XAML.cs에서 전체 tabitem 또는 그 일부를 인쇄하려면 어떻게합니까?탭 항목 인쇄

아래 코드를 사용하여 tabitem을 인쇄 할 수 있지만 크기와 미리보기를 제어하고 싶습니다. 가로 페이지 형식을 사용하면 전체 페이지가 인쇄되지 않고 일부만 잘립니다.

TabItem Header는 = "Stars"

XAML :

XAML.CS

<Button Margin=" 5,5,5,5" Grid.Row="3" x:Name="PrintOilTab" 
     Click="PrintOilTab_Click" Content="Print" FontSize="10"/> 
:

private void PrintOilTab_Click(object sender, RoutedEventArgs e) 
{ 
    System.Windows.Controls.PrintDialog Printdlg = 
     new System.Windows.Controls.PrintDialog(); 

    if ((bool)Printdlg.ShowDialog().GetValueOrDefault()) 
    { 
     CompleteOilLimitDiagram.Measure(
      new Size(Printdlg.PrintableAreaWidth,    
        Printdlg.PrintableAreaHeight)); 
     Printdlg.PrintVisual(CompleteOilLimitDiagram, "Stars"); 
    } 
} 

답변

1

은 내가 PrintVisual()과 행운을 가진 적이 없었습니다. 항상 FixedDocument을 생성 한 다음 PrintDocument()을 사용해야했습니다.

이 코드는 ImageSource 인쇄하도록 설계되어 있지만 나는 그것을 쉽게 FixedDocument에 제어 추가하여 제어를 인쇄하기에 적합 할 수 있다고 생각 :

using System.Windows.Documents; 

    public async void SendToPrinter() 
    { 
     if (ImageSource == null || Image == null) 
      return; 

     var printDialog = new PrintDialog(); 

     bool? result = printDialog.ShowDialog(); 
     if (!result.Value) 
      return; 

     FixedDocument doc = GenerateFixedDocument(ImageSource, printDialog); 
     printDialog.PrintDocument(doc.DocumentPaginator, ""); 

    } 

    private FixedDocument GenerateFixedDocument(ImageSource imageSource, PrintDialog dialog) 
    { 
     var fixedPage = new FixedPage(); 
     var pageContent = new PageContent(); 
     var document = new FixedDocument(); 

     bool landscape = imageSource.Width > imageSource.Height; 

     if (landscape) 
     { 
      fixedPage.Height = dialog.PrintableAreaWidth; 
      fixedPage.Width = dialog.PrintableAreaHeight; 
      dialog.PrintTicket.PageOrientation = PageOrientation.Landscape; 
     } 
     else 
     { 
      fixedPage.Height = dialog.PrintableAreaHeight; 
      fixedPage.Width = dialog.PrintableAreaWidth; 
      dialog.PrintTicket.PageOrientation = PageOrientation.Portrait; 
     } 

     var imageControl = new System.Windows.Controls.Image {Source = ImageSource,}; 
     imageControl.Width = fixedPage.Width; 
     imageControl.Height = fixedPage.Height; 

     pageContent.Width = fixedPage.Width; 
     pageContent.Height = fixedPage.Height; 

     document.Pages.Add(pageContent); 
     pageContent.Child = fixedPage; 

     // You'd have to do something different here: possibly just add your 
     // tab to the fixedPage.Children collection instead. 
     fixedPage.Children.Add(imageControl); 

     return document; 
    }