C# 打印选项卡项
如何在XAML/XAML.cs中启用打印整个或部分选项卡项 我使用下面的代码,能够打印tabitem,但我想控制大小和预览。如果我使用横向页面格式,它仍然不会打印整个页面,但会截断部分页面C# 打印选项卡项,c#,wpf,xaml,tabcontrol,tabitem,C#,Wpf,Xaml,Tabcontrol,Tabitem,如何在XAML/XAML.cs中启用打印整个或部分选项卡项 我使用下面的代码,能够打印tabitem,但我想控制大小和预览。如果我使用横向页面格式,它仍然不会打印整个页面,但会截断部分页面 TabItem标题=“Stars” XAML: <Button Margin=" 5,5,5,5" Grid.Row="3" x:Name="PrintOilTab" Click="PrintOilTab_Click" Content="Print" FontSize="10"/>
TabItem标题
=“Stars
”
XAML:
<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");
}
}
我从来没有在
PrintVisual()
方面交过好运。我总是需要生成一个FixedDocument
,然后使用PrintDocument()
此代码设计用于打印图像源
,但我认为,通过将控件添加到固定文档
,它可以轻松地适配为打印任何控件:
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;
}