我真的是 VB 新手。所以我有一个简单的程序,只有 4 个按钮(预览、打印、设置、保存)。这 4 个按钮用于打印、预览和保存简单的图形。问题是当我调用 PrintDocument1.print() 时,它不会将我重定向到保存文件或打印机选择。
这是我的代码:
Imports System.Drawing.Printing
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub PrintDocument1_PrintPage_1(sender As Object, e As PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim font As New Font("Arial", 16, FontStyle.Regular)
e.Graphics.DrawString("Hello World", font, Brushes.Black, 200, 200)
End Sub
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub
Private Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
Private Sub btnSetting_Click(sender As Object, e As EventArgs) Handles Button3.Click
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.Document.DefaultPageSettings.Color = False
PageSetupDialog1.ShowDialog()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
End Sub
End Class
我想将我的 PrintDocument(包括 e.graphic.drawstring)保存到 Fialog 本地的 PDF 文件中以选择文件路径。
谢谢大家,恳请您的帮助。
您可以创建一个 pdfPrintDocument 类来代替 PrintDocument。 为我们要在其上绘制图形的每个页面创建一个位图图像。使用 iTextSharp 库,将图像转换为 pdf。使用示例是这里
Imports System.Drawing.Printing
Imports System.IO
Imports System.Data
Imports iTextSharp
Imports iTextSharp.text.pdf
Public Class pdfPrintDocument
Inherits PrintDocument
Public Shadows Event PrintPage(ByVal sender As System.Object, ByVal e As PrintPageEventArgs)
Public Shadows Event BeginPrint As PrintEventHandler
Public Shadows Event EndPrint As PrintEventHandler
Public Shadows Sub print()
Dim peArg As New PrintEventArgs
RaiseEvent BeginPrint(Nothing, peArg)
Dim pe As PrintPageEventArgs
Dim fn As String = Me.DocumentName & ".pdf" 'Pdf file name
fn = "x.pdf"
'msgbox(fn)
Using stream As New FileStream(fn, FileMode.Create) 'Create and open pdf file
Dim img As iTextSharp.text.Image
Dim pdfDoc As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 0, 0, 0, 0)
'pdfDoc.LeftMargin = 0
PdfWriter.GetInstance(pdfDoc, stream)
pdfDoc.Open()
'msgbox(pdfdoc.PageSize.ToString)
Do 'Keep generating images and add as pdfpage until HasmorePages is false
Dim ps As PrinterSettings = Me.PrinterSettings
Dim Psize as New Point(pdfDoc.PageSize.Width*1.38889, pdfDoc.PageSize.Height*1.38889) 'ItextSharp page size is in points. Multiply by 1.38889 to get in Pixels
Dim im As Image = New Bitmap(psize.X, psize.Y) 'create image with size as page
Dim g As Graphics = Graphics.FromImage(im) 'get graphics from image
g.Clear(Color.White) 'Set page background
Dim mb As Rectangle = ps.DefaultPageSettings.Bounds
pe = New PrintPageEventArgs(g, New Rectangle(new Point(0, 0), psize), mb, Nothing) 'Create a print event with graphics for drawing
pe.HasMorePages = False
RaiseEvent PrintPage(Nothing, pe)
img = iTextSharp.text.Image.GetInstance(im, system.Drawing.Imaging.ImageFormat.png) 'Convert image to pdfImage
'img.ScalePercent(100)
img.ScaleToFit(pdfDoc.PageSize.Width, pdfDoc.PageSize.Height) 'Scale image to fit on pdfPage
pdfDoc.Add(img) 'Add image to pdf document
pdfDoc.NewPage() 'Create a new pdfPage
Loop While pe.HasMorePages = True
pdfDoc.Close()
stream.Close()
End Using
RaiseEvent EndPrint(Nothing, peArg)
End Sub
End Class
使用表单中的代码。
Imports System.Drawing.Printing
Public Class Form1
Dim page As Int16 = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim pd As New pdfPrintDocument
pd.DocumentName = "test"
AddHandler pd.PrintPage, AddressOf PrintDocument1_PrintPage
pd.Print()
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim g As Graphics = e.Graphics
g.DrawString("TEST Page " & page, New Font("Arial", 50), Brushes.Black, 20, 20)
If page > 10 Then
e.HasMorePages = False
page = 1
Exit Sub
End If
e.HasMorePages = True
page += 1
End Sub
End Class