2017-04-03 1 views
1

Comment activer l'impression de l'intégralité d'un onglet ou d'une partie de celui-ci dans XAML/XAML.cs?Impression d'un élément d'onglet

J'utilise le code ci-dessous et capable d'imprimer le tabitem mais je veux contrôler la taille et l'aperçu. Si j'utilise le format de page paysage, il n'imprime toujours pas la page complète mais tronque une partie de celle-ci.

TabItem Header = "Stars"

XAML:

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

XAML.CS:

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

Répondre

1

Je n'ai jamais eu bonne chance avec PrintVisual(). J'ai toujours dû générer un FixedDocument puis utiliser PrintDocument().

Ce code est conçu pour imprimer un ImageSource, mais je pense qu'il peut être facilement adapté pour imprimer un contrôle en ajoutant le contrôle à la 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; 
    }