C# 是否可以将wpf控件号作为图像打印到pdf?
要将文档打印为PDF,首先从视图中创建一个图像,然后将该图像添加到固定页面。代码如下:C# 是否可以将wpf控件号作为图像打印到pdf?,c#,wpf,C#,Wpf,要将文档打印为PDF,首先从视图中创建一个图像,然后将该图像添加到固定页面。代码如下: Size myPageSize = new Size(myView.Width, myView.Height); FixedDocument myDocument = new FixedDocument(); myDocument.DocumentPaginator.PageSize = miTamañoPagina; myView.Measure(myPageSize);
Size myPageSize = new Size(myView.Width, myView.Height);
FixedDocument myDocument = new FixedDocument();
myDocument.DocumentPaginator.PageSize = miTamañoPagina;
myView.Measure(myPageSize);
myView.Arrange(new Rect(myPageSize));
myView.UpdateLayout();
System.Windows.Media.Imaging.RenderTargetBitmap bitmap = new System.Windows.Media.Imaging
.RenderTargetBitmap(794, 1122, 96, 96, System.Windows.Media.PixelFormats.Pbgra32);
bitmap.Render(myView);
System.Windows.Controls.Image myImage = new System.Windows.Controls.Image();
myImage.Source = bitmap;
FixedPage miPage = new FixedPage();
miPage.Width = myDocument.DocumentPaginator.PageSize.Width;
miPage.Height = myDocument.DocumentPaginator.PageSize.Height;
miPage.Children.Add(myImage);
PageContent myContent = new PageContent();
((IAddChild)myContent).AddChild(miPage);
myDocument.Pages.Add(myContent);
System.Windows.Controls.PrintDialog myDialog = new System.Windows.Controls.PrintDialog();
if (myDialog.ShowDialog() == true)
{
myDialog.PrintDocument(myDocument.DocumentPaginator, string.Empty);
}
当我创建PDF时,它就像一个imagen,我无法选择文本,因此我想知道是否有任何方法可以打印视图而不将其转换为图像
实际上我正在使用.NET4.7.2和Windows10。我知道,在Windows7、Windows8.1和Windows10之间,grpahics的行为是不同的
谢谢
编辑:我注意到我可以使用PrintVisual打印视图,问题是我也需要打印背景,因此如果我将此代码添加到axml,它不会打印,但如果我删除此代码,它会打印
<UserControl.Background>
<ImageBrush AlignmentX="Center" AlignmentY="Center" Stretch="Uniform" Opacity="0.25" ImageSource="{Binding Source={x:Static vg:VariablesGlobales.Logo}}">
<ImageBrush.RelativeTransform>
<ScaleTransform ScaleX="0.75" ScaleY="0.75" CenterX=".5" CenterY="0.5" />
</ImageBrush.RelativeTransform>
</ImageBrush>
</UserControl.Background>
打印时,我可以选择文本
因此,也许我应该提出另一个问题,询问如何使用背景图像打印视觉效果。您必须手动打印所有内容(而不是图像)。可能会有一个路径WPF>WMF>PDF,这是我目前发现的,但这涉及到外部资源,可能是商业图书馆,并且不能保证您最终得到可选择的文本。Ia已经意识到,如果我使用PrintVisual(myView),它将打印空pdf,但如果我删除视图的背景,它将正确打印。