繁体   English   中英

打印预览并在wpf中打印

[英]print preview and print in wpf

我有WPF表单,我需要打印它,我使用DocumentViewer进行打印。 但是当我要打印它或预览时,当我有多个页面时,我只会看到第一页

private void Print(object sender, RoutedEventArgs e)
{
PrintSettings printSettings = PrintSettings.Default;
UIElement container = this.Content as UIElement;
ScrollViewer containerPanel = Helper.FindVisualChildren<ScrollViewer>(container).FirstOrDefault();
var origParentDirection = containerPanel.FlowDirection;
var origDirection = (containerPanel.Content as FrameworkElement).FlowDirection;
if (containerPanel != null && containerPanel.FlowDirection == FlowDirection.RightToLeft)
{
containerPanel.FlowDirection = FlowDirection.LeftToRight;
(containerPanel.Content as FrameworkElement).FlowDirection = FlowDirection.RightToLeft;
}
var window = new Window();
string tempFileName = System.IO.Path.GetTempFileName();
System.IO.File.Delete(tempFileName);
using (XpsDocument xpsDocument = new XpsDocument(tempFileName, FileAccess.ReadWrite, System.IO.Packaging.CompressionOption.Fast))
{
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(xpsDocument);
(containerPanel.Content as FrameworkElement).Margin = new Thickness(20);
writer.Write((containerPanel.Content as FrameworkElement), printSettings.PrintTicket);
var doc = xpsDocument.GetFixedDocumentSequence();
doc.PrintTicket = printSettings.PrintTicket;       
window.FlowDirection = System.Windows.FlowDirection.RightToLeft;
window.Content = new DocumentViewer { Document = doc };
window.Margin = new Thickness(10);
window.ShowDialog();
}
(containerPanel.Content as FrameworkElement).FlowDirection = origDirection;
containerPanel.FlowDirection = origParentDirection;
}

user1780436,我目前正在寻找类似的答案。 您正在尝试打印面板,对吗?

我发现缩放元素是最大的问题。 这给缩放要打印的内容而不是实际的可见元素带来了另一个问题。 您将必须将元素复制到新元素。

public class Copy<T>
{
    public static T DeepCopy<T>(T element)
    {
        string xaml = XamlWriter.Save(element);
        StringReader xamlString = new StringReader(xaml);
        XmlTextReader xmlTextReader = new XmlTextReader(xamlString);
        var DeepCopyobject = (T)XamlReader.Load(xmlTextReader);
        return DeepCopyobject;
    }

}

要么

myNewElement = XamlReader.Parse(XamlWriter.Save(myOldElement.DataContext)) as ElementType

我已经在多个站点上重复地找到了这个答案以复制/克隆一个元素,但是我遇到了string xaml = XamlWriter.Save(element); 导致堆栈溢出。

我目前正在使用。

myNewElement = new ElementType() { DataContext = myOldElement.DataContext }

您使用的任何一种都将成为更改元素大小的问题。 这就是我想要的。

我尝试了一次渲染传递 ,但这只是指出在我的情况下要使用复制/克隆的元素。 尽管在编写此代码时确实可以使用其中的一些功能,但是却给了我黑色的图像,请注意我正在尝试缩放Chart。

myNewElement.Width = newWidth;
myNewElement.Height = newHeight;

myNewElement.Measure(new System.Windows.Size(newWidth, newHeight));
myNewElement.Arrange(new Rect(0, 0, newWidth, newHeight));

我尝试了布局通行证 ,但没有得到。

我将继续从事我的工作,并将发布新发现的内容。 如果找到答案,请执行相同的操作。

编辑 -这是我所做的。 我的问题和解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM