gpt4 book ai didi

.net - 如何设置 PrintDocument 的边距?

转载 作者:行者123 更新时间:2023-12-04 13:23:12 27 4
gpt4 key购买 nike

我在尝试允许用户设置报告的边距时遇到了很糟糕的情况。现在我对它进行了硬编码,但我更愿意在运行时设置它们,以便可以更改它们。我试过这个:

Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

Me.OriginAtMargins = True
Me.DefaultPageSettings.Margins = New Margins(25, 25, 25, 25)

End Sub

要打印远远超出打印机硬边距的 1/4"边距。不打印任何内容。根据 Margins,参数以数百英寸为单位。如果我将代码更改为:
Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

Me.OriginAtMargins = True
Me.DefaultPageSettings.Margins = New Margins(20, 20, 23, 23)

End Sub

我确实得到了很好的输出:
Good output

但是当我将代码更改为:
Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

Me.OriginAtMargins = True
Me.DefaultPageSettings.Margins = New Margins(21, 20, 23, 23)

End Sub

这是我得到的,这没有意义。看起来它移动了 1 英寸:
Bad output

而且我不是唯一一个有利润问题的人,还有 no response here我什至发帖给微软, no response.

由于报告作者可以生成大多数文档,因此我认为使用 PrintDocument 的人并不多。目的。因此,在我向 Microsoft 报告错误之前,我看到这里的任何人都必须说些什么,这将需要相当长的时间才能得到响应。

编辑:我做了一个更简单的版本,得到了类似但仍然奇怪的结果。我不得不在类、类定义、属性等中留下大量代码。调用者需要的。我刚刚从事件中删除了(几乎)所有代码。

这是事件代码:
Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

Me.OriginAtMargins = True
Me.DefaultPageSettings.Margins = New Margins(19, 20, 23, 23)
mintLevel = 0
'Reset the loop trackers (used when we need to break out to print a page but remember our place) and others.
mintRowLoopStart = 0
mintTableLoopStart = 0
'Flags
ResetHeaderFooterFlags()
'Page counters
mintCurrentPage = 0
mintTotalPages = 0

End Sub

Private Sub rotHCReport_QueryPageSettings(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles Me.QueryPageSettings

e.PageSettings.Landscape = mePageOrientation = Orientation.Landscape
mintCurrentPage += 1

End Sub

Private Sub rotHCReport_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage


Dim DR As DataRow
Dim PrintFont As Font


e.Graphics.PageUnit = GraphicsUnit.Inch
PrintFont = New Font("Arial", 8, FontStyle.Regular)

For intRow = 0 To 10

DR = mDS.Tables(0).Rows(intRow)

For intColumn = 0 To 5

e.Graphics.DrawString(DR(intColumn).ToString, PrintFont, New SolidBrush(Color.Black), intColumn + 1, CSng(intRow + 1))

Next
Next

End Sub

Private Sub rotHCReport_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

mbSetUpRequired = True

End Sub

产生(看起来不到 1/8 英寸):
Less

并改变:
Me.DefaultPageSettings.Margins = New Margins(20, 20, 23, 23)

产生(看起来超过 1/4 英寸):
enter image description here

EDIT2:我尝试了 TnTinMn 的技术,但我很难理解 TranslateTransform method.从我的测试来看,它几乎听起来像是“增加”了您拥有的任何 margin 。更糟糕的是,如果我使用 0,我会得到比我设置的更大的边距,OriginAtMargins 在这一点上没有区别。我想要做的是在打印机之间创建一致的边距。
Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

'Me.OriginAtMargins = True
Me.DefaultPageSettings.Margins = New Margins(25, 25, 25, 25)
'Subreport level
mintLevel = 0
'Reset the loop trackers (used when we need to break out to print a page but remember our place) and others.
mintRowLoopStart = 0
mintTableLoopStart = 0
'Flags
ResetHeaderFooterFlags()
'Page counters
mintCurrentPage = 0
mintTotalPages = 0

End Sub

Private Sub rotHCReport_QueryPageSettings(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles Me.QueryPageSettings

e.PageSettings.Landscape = mePageOrientation = Orientation.Landscape
mintCurrentPage += 1

End Sub


Private Sub rotHCReport_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage


Dim DR As DataRow
Dim PrintFont As Font


e.Graphics.PageUnit = GraphicsUnit.Inch
e.Graphics.ResetTransform()
e.Graphics.TranslateTransform(e.MarginBounds.X / 100.0F, e.MarginBounds.Y / 100.0F)
PrintFont = New Font("Arial", 8, FontStyle.Regular)

For intRow = 0 To 10

DR = mDS.Tables(0).Rows(intRow)

For intColumn = 0 To 5

e.Graphics.DrawString(DR(intColumn).ToString, PrintFont, New SolidBrush(Color.Black), CSng(intColumn + 1), CSng(intRow + 1))

Next
Next

End Sub

Private Sub rotHCReport_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

mbSetUpRequired = True

End Sub

其中产生:
enter image description here

我可以将 TranslateTransform 调用更改为:
e.Graphics.TranslateTransform(-e.MarginBounds.X / 100.0F, -e.MarginBounds.Y / 100.0F)

这产生了这个,更好,但我已经有一份黑客工作。我想弄清楚如何正确地做到这一点。

enter image description here

如何正确使用 PrintDocument 似乎是一个很大的谜。

当我使用 PrintDocument 时,TnTinMn 正在使用 PrintController。我尝试合并他提出的更改,但根本不起作用。知道如何让这两个对象一起工作吗?
Public Class NewController
Inherits PrintController

Public Overrides Function OnStartPage(ByVal document As System.Drawing.Printing.PrintDocument, ByVal e As System.Drawing.Printing.PrintPageEventArgs) As System.Drawing.Graphics

Dim g As System.Drawing.Graphics


g = MyBase.OnStartPage(document, e)
g.PageUnit = GraphicsUnit.Inch
g.ResetTransform()
g.TranslateTransform((e.MarginBounds.X - e.PageSettings.HardMarginX) / 100.0F, (e.MarginBounds.Y - e.PageSettings.HardMarginY) / 100.0F)

'This does not work either
'e.Graphics.PageUnit = GraphicsUnit.Inch
'e.Graphics.ResetTransform()
'e.Graphics.TranslateTransform((e.MarginBounds.X - e.PageSettings.HardMarginX) / 100.0F, (e.MarginBounds.Y - e.PageSettings.HardMarginY) / 100.0F)

'Return e.Graphics
Return g

End Function
End Class

我换了另一个类(class)。
Private mPC As NewController


Public Sub New()

MyBase.New()
mPC = New NewController
Me.PrintController = mPC

End Sub

Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

Me.DefaultPageSettings.Margins = New Margins(25, 25, 25, 25)
'Subreport level
mintLevel = 0
'Reset the loop trackers (used when we need to break out to print a page but remember our place) and others.
mintRowLoopStart = 0
mintTableLoopStart = 0
'Flags
ResetHeaderFooterFlags()
'Page counters
mintCurrentPage = 0
mintTotalPages = 0

End Sub

'Raised before each and every page
Private Sub rotHCReport_QueryPageSettings(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles Me.QueryPageSettings

e.PageSettings.Landscape = mePageOrientation = Orientation.Landscape
mintCurrentPage += 1

End Sub


Private Sub rotHCReport_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage


Dim DR As DataRow
Dim PrintFont As Font


e.Graphics.PageUnit = GraphicsUnit.Inch
PrintFont = New Font("Arial", 8, FontStyle.Regular)

For intRow = 0 To 10

DR = mDS.Tables(0).Rows(intRow)

For intColumn = 0 To 5

e.Graphics.DrawString(DR(intColumn).ToString, PrintFont, New SolidBrush(Color.Black), CSng(intColumn + 1), CSng(intRow + 1))
'millimetersd
'e.Graphics.DrawString(DR(intColumn).ToString, PrintFont, New SolidBrush(Color.Black), CSng((intColumn + 1) * 25.4), CSng((intRow + 1) * 25.4))

Next
Next

End Sub

Private Sub rotHCReport_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

mbSetUpRequired = True

End Sub

结果一样。

编辑最终:
TnTinMn 的解决方案非常有效。我想我在此处、我的测试 dll 和生产 dll 之间复制代码时出错了。无论如何,我重新审视了所有事情,尽管更仔细一些并且它奏效了。这是在测试中工作的代码(以及在生产中工作的相同技术)
Private Sub rotHCReport_BeginPrint(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.BeginPrint

'Me.OriginAtMargins = True 'Don't need this anymore
Me.DefaultPageSettings.Margins = New Margins(25, 25, 25, 25) 'These are the margins actually used
'Subreport level
mintLevel = 0
'Reset the loop trackers (used when we need to break out to print a page but remember our place) and others.
mintRowLoopStart = 0
mintTableLoopStart = 0
'Flags
ResetHeaderFooterFlags()
'Page counters
mintCurrentPage = 0
mintTotalPages = 0

End Sub

'Raised before each and every page
Private Sub rotHCReport_QueryPageSettings(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.QueryPageSettingsEventArgs) Handles Me.QueryPageSettings

e.PageSettings.Landscape = mePageOrientation = Orientation.Landscape
mintCurrentPage += 1

End Sub

Private Sub rotHCReport_PrintPage(ByVal sender As Object, _
ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles Me.PrintPage

Dim DR As DataRow
Dim PrintFont As Font


e.Graphics.PageUnit = GraphicsUnit.Inch
e.Graphics.ResetTransform()
e.Graphics.TranslateTransform((e.MarginBounds.X - e.PageSettings.HardMarginX) / 100.0F, (e.MarginBounds.Y - e.PageSettings.HardMarginY) / 100.0F)
PrintFont = New Font("Arial", 8, FontStyle.Regular)

For intRow = 0 To 10

DR = mDS.Tables(0).Rows(intRow)

For intColumn = 0 To 5

e.Graphics.DrawString(DR(intColumn).ToString, PrintFont, New SolidBrush(Color.Black), CSng(0.25 + intColumn), CSng(0.25 + intRow))

Next
Next

End Sub

Private Sub rotHCReport_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles Me.EndPrint

mbSetUpRequired = True

End Sub

我设置了 1/4"的边距,我得到了 1/4"的边距!这是我的第 7 个屏幕截图,幸运的 #7。

enter image description here

最佳答案

在您的 PrintPage处理程序,你有语句 e.Graphics.PageUnit = GraphicsUnit.Inch并因此导致问题。收到的e.Graphics.PageUnit设置为 Display并且 Graphics 应用了一个坐标变换来反射(reflect)您的 OriginAtMargins = True环境。变换的 OffsetXOffsetY值是使用 Display 计算的PageUnit设置(以 1/100 英寸为单位)。当您更改 PageUnitInch这些偏移量现在被解释为英寸,并且您在图形上绘制的任何内容都超出了边界。这就是为什么你会得到一个空页面。

您应该能够消除 OriginAtMargins = True声明并修改PrintPage处理程序代码如下:

e.Graphics.PageUnit = GraphicsUnit.Inch
e.Graphics.ResetTransform() ' clear any previous transforms
e.Graphics.TranslateTransform((e.MarginBounds.X - e.PageSettings.HardMarginX) / 100.0F, (e.MarginBounds.X - e.PageSettings.HardMarginY) / 100.0F)

编辑:我更正了 TranslateTransform 参数以说明我最初忽略的打印机硬边距值。这是基于 StandardPrintController.OnStartPage 执行的变换方法当然后 PrintDocument.OriginAtMargins属性是真的。

关于.net - 如何设置 PrintDocument 的边距?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46119165/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com