简体   繁体   English

打印预览并在wpf中打印

[英]print preview and print in wpf

I have a WPF Form ,I need to print it ,i use DocumentViewer to print. 我有WPF表单,我需要打印它,我使用DocumentViewer进行打印。 but when I want to Print it or Preview , I only See the first page while i have more than one page 但是当我要打印它或预览时,当我有多个页面时,我只会看到第一页

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, I am currently looking for a similar answer. user1780436,我目前正在寻找类似的答案。 You are trying to print a panel, correct? 您正在尝试打印面板,对吗?

I am finding that scaling the element is the biggest problem. 我发现缩放元素是最大的问题。 Which brings another issue with scaling what you want to print and not the actual visible element. 这给缩放要打印的内容而不是实际的可见元素带来了另一个问题。 You will have to copy the element to a new element. 您将必须将元素复制到新元素。

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

}

or 要么

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

I have found this answer repeatedly on multiple sites to copy/clone an element, but I have had issues with string xaml = XamlWriter.Save(element); 我已经在多个站点上重复地找到了这个答案以复制/克隆一个元素,但是我遇到了string xaml = XamlWriter.Save(element); causing stackoverflows. 导致堆栈溢出。

I am currently using. 我目前正在使用。

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

Either one you use there becomes the issue of changing the size of the Element. 您使用的任何一种都将成为更改元素大小的问题。 This is what I am looking for. 这就是我想要的。

I tried a rendering pass , but that just pointed out that in my situation to use a copied/cloned element. 我尝试了一次渲染传递 ,但这只是指出在我的情况下要使用复制/克隆的元素。 Although while writing this I did get some of it to work, but gives me a black image, note I am trying to scale a Chart. 尽管在编写此代码时确实可以使用其中的一些功能,但是却给了我黑色的图像,请注意我正在尝试缩放Chart。

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

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

I tried a layout pass , and didn't get it. 我尝试了布局通行证 ,但没有得到。

I am going to keep working on mine and I will post anything new I find. 我将继续从事我的工作,并将发布新发现的内容。 Please do the same if you find the answer. 如果找到答案,请执行相同的操作。

Edit - Here is what I did. 编辑 -这是我所做的。 My problem and solution 我的问题和解决方案

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

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