[英]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.