0

我目前正在浪费大量的纸张试图弄清楚这一点。我有一个名为 Dock 的 Dockpanel,带有堆栈面板、边框和文本框,我希望整个 Dock 打印到一页。

    Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
    'Define Printer Dialog
    Dim dialog As New PrintDialog
    'Define Printer Capabilities
    Dim capabil As PrintCapabilities = dialog.PrintQueue.GetPrintCapabilities(dialog.PrintTicket)
    'Scale content to capabilities of printer
    Dim scale As Double = Math.Min(capabil.PageImageableArea.ExtentWidth / Dock.ActualWidth, capabil.PageImageableArea.ExtentHeight /
                Dock.ActualHeight)
    Dock.LayoutTransform = New ScaleTransform(scale, scale)
    'Create a margin
    Dim pagemargin As Integer = 20
    'Define the size of the page
    Dim pagesize As New Size(capabil.PageImageableArea.ExtentWidth - pagemargin, capabil.PageImageableArea.ExtentHeight - pagemargin)
    Dock.Measure(pagesize)
    'Resize dock to optimal page size
    Dock.Arrange(New Rect(New Point(capabil.PageImageableArea.OriginWidth, capabil.PageImageableArea.OriginHeight), pagesize))
    'Prompt and Print
    If dialog.ShowDialog = True Then
        dialog.PrintVisual(Dock, "Printed Page.")
    End If
End Sub

我不理解这背后的数学原理吗?我应该用另一种方法来解决这个问题吗?

编辑:我面临的问题是,即使扩展坞的大小似乎正确,打印时仍有巨大的左边距和底部边距。我已经尝试调整边距,但似乎起点仍然是关闭的。该页面的顶部和右侧也被截断。

4

1 回答 1

1

这就是我使用PageMediaSize的一些线索想出的。这可能是也可能不是您要寻找的东西,但无论如何它都可以为您提供一些想法:

Private Sub Button_Click_1(sender As Object, e As RoutedEventArgs)
  Dim dialog As New PrintDialog
  If dialog.ShowDialog = True Then
    Dim dockParent As StackPanel = Dock.Parent
    dockParent.Children.Remove(Dock)

    Dim vis = PerformTransform(Dock, dialog.PrintQueue)
    dialog.PrintVisual(vis, "Printed Page.")

    vis.Children.Remove(Dock)
    dockParent.Children.Add(Dock)
  End If
End Sub

Private Function PerformTransform(v As Visual, pq As PrintQueue) As ContainerVisual
  Dim root As ContainerVisual = New ContainerVisual()
  Const inch As Double = 96 'DPI

  'Set the margins
  Dim xMargin = 1.25 * inch
  Dim yMargin = 1 * inch

  'Get the user print ticket and media dimensions
  Dim pt = pq.UserPrintTicket()
  Dim printableWidth = pt.PageMediaSize.Width.Value
  Dim printableHeight = pt.PageMediaSize.Height.Value

  Dim xScale = (printableWidth - xMargin * 2) / printableWidth
  Dim yScale = (printableHeight - yMargin * 2) / printableHeight

  root.Children.Add(v)
  root.Transform = New MatrixTransform(xScale, 0, 0, yScale, xMargin, yMargin)

  Return root
End Function
于 2013-10-10T21:58:43.027 回答