我有一个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;
}发布于 2012-11-01 15:50:15
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 }无论您在那里使用哪一个,都会出现更改元素大小的问题。这就是我要找的东西。
我尝试了一个渲染通行证,但这只是指出在我的情况下使用复制/克隆元素。虽然在写这篇文章的时候,我确实做了一些工作,但是给了我一个黑色的图像,注意我正在试着缩放一个图表。
myNewElement.Width = newWidth;
myNewElement.Height = newHeight;
myNewElement.Measure(new System.Windows.Size(newWidth, newHeight));
myNewElement.Arrange(new Rect(0, 0, newWidth, newHeight));我试了一次布局孔型,但没有得到它。
我将继续我的工作,我会张贴任何新的,我发现。如果你找到答案,请做同样的事。
编辑-以下是我所做的。我的问题和解决办法
https://stackoverflow.com/questions/13133070
复制相似问题