打印DockPanel并将其安装到页面

时间:2013-10-10 20:13:31

标签: wpf vb.net

我目前正在浪费大量的纸张试图解决这个问题。我有一个名为Dock的Dockpanel,带有堆叠面板,边框和文本框,我希望整个底座可以打印到一个页面。

    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

我不理解这背后的数学吗?还有另一种方法我应该接近这个吗?

编辑:我面临的问题是,即使底座看起来尺寸正确,打印时也会有一个巨大的左下边距。我已经尝试调整保证金,但似乎起点仍然是关闭。该页面也在顶部和右侧被切断。

1 个答案:

答案 0 :(得分: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