【问题标题】:Printing a tab item打印选项卡项
【发布时间】:2017-04-03 21:42:34
【问题描述】:

如何在 XAML/XAML.cs 中启用打印整个 tabitem 或其中的一部分?

我正在使用下面的代码并且能够打印 tabitem,但我想控制大小和预览。如果我使用横向页面格式,它仍然不会打印完整的页面,而是截断其中的一部分。

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

【问题讨论】:

    标签: c# wpf xaml tabcontrol tabitem


    【解决方案1】:

    我对@9​​87654321@ 从来没有好运。我总是不得不生成一个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;
        }
    

    【讨论】:

      猜你喜欢
      • 2012-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多