如何缩放表格以使其适合A4纸的尺寸进行打印 [英] How do I scale a form so it will fit to size of A4 paper for printing
问题描述
在我的应用程序中,我有许多需要打印的窗口形式。
最终用户都有不同尺寸的屏幕,最低的是1366 x 768.
我尝试使用PrintForm,但程序只打印屏幕上的内容,因此表格必须很小,因此只能填写A4纸的一部分。
因此我希望能够捕获表格和将它扩展到A4以进行打印。
我从互联网上复制了一些代码并将其修改以供我使用,但它似乎没有按比例放大表格,尽管它会打印它。
有任何想法我怎么做。
请注意我是一个完全的业余爱好者。为高尔夫俱乐部的老年人部门制作软件。
我的尝试:
使用的代码如下
Public Class TeamSelect12
'定义你的打印对象
私人PrintDoc1 As PrintDocument = New PrintDocument
Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog
Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog
'打印按钮代码
Private Sub PrtTSBut_Click( sender As System.Object,e As System.EventArgs)Handles PrtTSBut.Click
PDFTeamBut.Visible = False'隐藏按钮
PrtTSBut.Visible = False'隐藏按钮
Me。刷新()
使用PageSetupDialog1
'指定文档使用
.Document = PrintDoc1
'启用打印机按钮
.AllowPrinter = True
.EnableM etric = True
'初始化对话框的PrinterSettings属性以保存用户
'定义的页面设置。
.PageSettings = New System.Drawing.Printing.PageSettings
'Initialize对话框的PrinterSettings属性,用于保存用户
'设置打印机设置。
.PrinterSettings = New System.Drawing.Printing.PrinterSettings
End with
PrintPreviewDialog1.Document = PrintDoc1
AddHandler PrintDoc1.PrintPage,AddressOf PDoc_PrintPage
Dim保证金作为新保证金(20,20,20,40)
PrintDoc1.DefaultPageSettings.Margins =保证金
PrintPreviewDialog1.ShowDialog()
'替换表格
PrtTSBut.Visible = True
PDFTeamBut.Visible = True
'隐藏形式
Me.Hide()
InputForm.Show()'返回输入形式
结束Sub
'打印处理程序和图形打印方法缩放
Private Sub PDoc_PrintPage(sender As Object,e As PrintPageEventArgs)
PrintToGraphics(e.Graphics,e.MarginBounds)
End Sub
Public Sub PrintToGraphics(图形为图形,边界为矩形)
'将控件的视图打印到图形ics对象。
'< param name =graphics>要绘制的图形对象。< / param>
'< param name =bounds>要打印的矩形。< / param>
'将控件和内容绘制到位图
Dim Bitmap As Bitmap = New Bitmap(Me.Width,Me.Height)
Me.DrawToBitmap(Bitmap,New Rectangle( 0,0,Bitmap.Width,Bitmap.Height))
'将打印限制分配给目标矩形
Dim PrtWidth,PrtHeight,PrtLeft,PrtTop As Integer
PrtWidth = bounds。宽度
PrtHeight = bounds.Height
PrtLeft = bounds.Left
PrtTop = bounds.Top
Dim target As Rectangle = New Rectangle(PrtLeft,PrtTop,PrtWidth,PrtHeight)
'缩放位图以适应目标
Dim xScale As Double = Bitmap.Width / PrtWidth
Dim yScale As Double = Bitmap.Height / PrtHeight
如果xScale< yScale然后
target.Width = Int(xScale * target.Width / yScale)
否则
target.Height = Int(yScale * target.Height / xScale)
结束如果
'绘制位图
Graphics.PageUnit = GraphicsUnit.Display
Graphics.DrawImage(位图,目标)
结束子
根本不打印表格:您可以将屏幕捕捉为图像,然后在A4处绘制该图像,但在某些屏幕上看起来像垃圾,并且不可读在其他人身上。
相反,将屏幕应该显示的信息绘制到PrintDocument上:这样,无论哪个用户,您都可以使输出看起来相同打印它,因为您可以完全控制所有内容的字体大小和位置。此外,如果您的任何表单控件都是可滚动的,捕获过程将只捕获可见位,因此不会打印任何滚动的视图 - 通过正确执行工作,您可以准确地确定要打印的内容,尺寸是多少它打印在哪里。
PrintDocument的MS文档显示了打印文本的基础知识: PrintDocument Class(System.Drawing.Printing) [ ^ ]
也许你可以使用像 PDFsharp 这样的PDF库: PDFsharp和MigraDoc Foundation的主页 - PDFsharp& ; MigraDoc [ ^ ]
或< b> ItextSharp : GitHub - itext / itextsharp:iText库的.NET端口 [ ^ ]
将数据保存为excel并从excel打印
In my application I have a number of window forms that need to printed.
The end users all have different size screens the lowest being 1366 x 768.
I tried using PrintForm but the program only prints what is on the screen so the forms have to be small and therefore would only fill part of an A4 sheet.
Therefore I want to be able to capture the form and scale it up to A4 for printing.
I copied some code from the internet and modified it for my use but it does not seem to scale up the form although it does print it.
Has any one any ideas how I can do this.
Please be aware I am a total amateur at this. Producing the software for the Seniors Section of a golf club.
What I have tried:
The code am using is as follows
Public Class TeamSelect12 'Define your print object Private PrintDoc1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog Private PageSetupDialog1 As PageSetupDialog = New PageSetupDialog 'Print Button Code Private Sub PrtTSBut_Click(sender As System.Object, e As System.EventArgs) Handles PrtTSBut.Click PDFTeamBut.Visible = False 'Hides button PrtTSBut.Visible = False 'Hides button Me.Refresh() With PageSetupDialog1 'Assign the document to use .Document = PrintDoc1 'Enable printer button .AllowPrinter = True .EnableMetric = True ' Initialize the dialog's PrinterSettings property to hold user ' defined page settings. .PageSettings = New System.Drawing.Printing.PageSettings ' Initialize dialog's PrinterSettings property to hold user ' set printer settings. .PrinterSettings = New System.Drawing.Printing.PrinterSettings End With PrintPreviewDialog1.Document = PrintDoc1 AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage Dim margins As New Margins(20, 20, 20, 40) PrintDoc1.DefaultPageSettings.Margins = margins PrintPreviewDialog1.ShowDialog() 'Replace buttons back on form PrtTSBut.Visible = True PDFTeamBut.Visible = True 'Hide form Me.Hide() InputForm.Show() ' Return to input form End Sub 'Print Handler And Graphics print method With scaling Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs) PrintToGraphics(e.Graphics, e.MarginBounds) End Sub Public Sub PrintToGraphics(Graphics As Graphics, bounds As Rectangle) 'Print the control's view to a Graphics object. '<param name="graphics">Graphics object to draw on.</param> '<param name="bounds">Rectangle to print in.</param> 'Draw the control and contents to a bitmap Dim Bitmap As Bitmap = New Bitmap(Me.Width, Me.Height) Me.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height)) 'Assign Print Bounds to target rectangle Dim PrtWidth, PrtHeight, PrtLeft, PrtTop As Integer PrtWidth = bounds.Width PrtHeight = bounds.Height PrtLeft = bounds.Left PrtTop = bounds.Top Dim target As Rectangle = New Rectangle(PrtLeft, PrtTop, PrtWidth, PrtHeight) 'Scale bitmap to fit target Dim xScale As Double = Bitmap.Width / PrtWidth Dim yScale As Double = Bitmap.Height / PrtHeight If xScale < yScale Then target.Width = Int(xScale * target.Width / yScale) Else target.Height = Int(yScale * target.Height / xScale) End If 'Draw the bitmap Graphics.PageUnit = GraphicsUnit.Display Graphics.DrawImage(Bitmap, target) End Sub
Don't print the form at all: you can do it by capturing the screen as an Image, and drawing that image at A4, but it will look like rubbish on some screens, and unreadable on others.
Instead, draw the information the screen is supposed to be displaying onto a PrintDocument: that way, you can make the output look the same regardless of which user printed it as you have complete control of the font size and position of everything. Plus, if any of your form controls are scrollable, the capture process will only capture the visible bit, so anything scrolled out of sight will no be printed - by doing the job properly, you get to decide exactly what gets printed, what size it is printed at, and where it goes.
The MS documentation for PrintDocument shows the basics of printing text: PrintDocument Class (System.Drawing.Printing)[^]
Maybe you can use a PDF library like PDFsharp: Home of PDFsharp and MigraDoc Foundation - PDFsharp & MigraDoc[^]
Or ItextSharp: GitHub - itext/itextsharp: .NET port of the iText library[^]
Saved the data to excel and printed from excel
这篇关于如何缩放表格以使其适合A4纸的尺寸进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!