使用Silverlight 5和VB.Net,我试图将LayoutRoot中的4个网格打印成4张独立的纸,如下所示,但我只打印了最后一个网格。我明白为什么会这样,但我应该怎么做呢?我将网格缩放到原始大小的75%,因为这样可以使它们适合纵向纵向的纸张。
感谢。
Imports System.Windows.Printing
Partial Public Class MainPage
Inherits UserControl
Public WithEvents pd As New PrintDocument
Public Sub New()
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As Object, ByVal e As RoutedEventArgs) Handles Button1.Click
pd.Print("My Test Print")
Call ScaleUp()
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles pd.PrintPage
Dim ScaleDown As New ScaleTransform
ScaleDown.ScaleX = "0.75"
ScaleDown.ScaleY = "0.75"
CompChartGrid.RenderTransform = ScaleDown
AttChartGrid.RenderTransform = ScaleDown
CompDetGrid.RenderTransform = ScaleDown
AttDetGrid.RenderTransform = ScaleDown
e.PageVisual = CompChartGrid
e.HasMorePages = True
e.PageVisual = AttChartGrid
e.HasMorePages = True
e.PageVisual = CompDetGrid
e.HasMorePages = True
e.PageVisual = AttDetGrid
e.HasMorePages = False
End Sub
End Class
答案 0 :(得分:0)
你快到了。诀窍是将为每个要打印的页面调用pd_PrintPage方法。在您的情况下,只打印最后一页,因为您正在覆盖预期的视觉效果。
尝试这样的事情:
private void Button_Click(object sender, RoutedEventArgs e)
{
var doc = new PrintDocument();
var pages = new Stack<UIElement>();
pages.Push(AttDetGrid);
pages.Push(CompDetGrid);
pages.Push(AttChartGrid);
pages.Push(CompChartGrid);
doc.PrintPage += (x, y) =>
{
if (pages.Count > 0)
{
y.PageVisual = pages.Pop();
y.HasMorePages = pages.Count > 0;
}
};
doc.Print("My Printed document name");
}
答案 1 :(得分:-1)
好的,我最终这样做了:
Private Sub pd_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles pd.PrintPage
Static Counter As Integer = 1
If Counter = 1 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid1)
PrintSurface.Children.Add(Grid1)
e.PageVisual = PrintSurface
Counter += 1
e.HasMorePages = True
Exit Sub
End If
If Counter = 2 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid2)
PrintSurface.Children.Add(Grid2)
e.PageVisual = PrintSurface
Counter += 1
e.HasMorePages = True
Exit Sub
End If
If Counter = 3 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid3)
PrintSurface.Children.Add(Grid3)
e.PageVisual = PrintSurface
Counter += 1
e.HasMorePages = True
Exit Sub
End If
If Counter = 4 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid4)
PrintSurface.Children.Add(Grid4)
e.PageVisual = PrintSurface
Counter += 1
e.HasMorePages = True
Exit Sub
End If
If Counter = 5 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid5)
PrintSurface.Children.Add(Grid5)
e.PageVisual = PrintSurface
Counter += 1
e.HasMorePages = True
Exit Sub
End If
If Counter = 6 Then
Dim PrintSurface As New Canvas
LayoutRoot.Children.Remove(Grid6)
PrintSurface.Children.Add(Grid6)
e.PageVisual = PrintSurface
e.HasMorePages = False
Exit Sub
End If
End Sub