如何打印数据杂志的内容。我已经检查了以下帖子 我如何制作"打印预览"。WPF应用程序中的FlowDocument?它仅生成网格的可见部分而不是可滚动部分。我需要预览有多个页面,我应该使用FlowDocument,但我不确定该如何处理。任何想法,将不胜感激。
我曾经有一个问题。我编写了一种生成datagrid中生成 System.Windows.documents.table 的方法。我将其放在flowdocument中,并通过 XpsDocumentWriter 生成了固定的文档。然后,您将拥有数据杂志的完整分页视图,您可以在 documentViewer
使用以下方式:它可以正常工作。
public class UIPrinter
{
#region Properties
public Int32 VerticalOffset { get; set; }
public Int32 HorizontalOffset { get; set; }
public String Title { get; set; }
public UIElement Content { get; set; }
#endregion
#region Initialization
public TimelinePrinter()
{
HorizontalOffset = 20;
VerticalOffset = 20;
Title = "Print " + DateTime.Now.ToMyStringWithTime();
}
#endregion
#region Methods
public Int32 Print()
{
var dlg = new PrintDialog();
if (dlg.ShowDialog() == true)
{
//---FIRST PAGE---//
// Size the Grid.
Content.Measure(new Size(Double.PositiveInfinity,
Double.PositiveInfinity));
Size sizeGrid = Content.DesiredSize;
//check the width
if (sizeGrid.Width > dlg.PrintableAreaWidth)
{
MessageBoxResult result = MessageBox.Show(Properties.Resources.s_EN_Question_PrintWidth, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.No)
throw new PrintAborted(Properties.Resources.s_EN_Info_PrintingAborted);
}
// Position of the grid
var ptGrid = new Point(HorizontalOffset, VerticalOffset);
// Layout of the grid
Content.Arrange(new Rect(ptGrid, sizeGrid));
//print
dlg.PrintVisual(Content, Title);
//---MULTIPLE PAGES---//
double diff;
int i = 1;
while ((diff = sizeGrid.Height - (dlg.PrintableAreaHeight - VerticalOffset*i)*i) > 0)
{
//Position of the grid
var ptSecondGrid = new Point(HorizontalOffset, -sizeGrid.Height + diff + VerticalOffset);
// Layout of the grid
Content.Arrange(new Rect(ptSecondGrid, sizeGrid));
//print
int k = i + 1;
dlg.PrintVisual(Content, Title + " (Page " + k + ")");
i++;
}
return i;
}
throw new PrintAborted(Properties.Resources.s_EN_Info_PrintingAborted);
}
#endregion
}
它在多个页面上使用所选打印机打印数据杂志或任何其他控件...
用法:
MessageBoxResult result = MessageBox.Show(Properties.Resources.s_EN_Question_Print, "Print", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (result == MessageBoxResult.Yes)
{
try
{
var border = VisualTreeHelper.GetChild(MyDataGrid, 0) as Decorator;
if (border != null)
{
var scrollViewer = border.Child as ScrollViewer;
if (scrollViewer != null)
{
scrollViewer.ScrollToTop();
scrollViewer.ScrollToLeftEnd();
}
}
Title = _initialTitle + " - " + Properties.Resources.s_EN_Info_Printing;
var myPrinter = new UIPrinter{ Title = Title, Content = PrintGrid };
int nbrOfPages = myPrinter.Print();
Title = _initialTitle + " - " + Properties.Resources.s_EN_Info_PrintingDone + " (" + nbrOfPages + " Pages)";
}
catch (PrintAborted ex)
{
Title = _initialTitle + " - " + ex.Message;
}
}
编辑:我将datagrid放在一个简单的网格上,其中包含标头控件以在纸上放置标头。