-1

使用 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
4

2 回答 2

0

You are nearly there. The trick is that the pd_PrintPage method will be called for each page to be printed. In your case, only the last page is printed because you are overwriting your intended visuals.

Try something like this instead:

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");
}
于 2013-02-15T07:38:52.043 回答
-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
于 2013-02-15T13:25:17.920 回答