How to save PrintDocument (e.Graphic) as PDF in VB.net

473 Views Asked by At

I am really newbie with VB here. so I have a simple program within 4 button (Preview, Print, Setup, Save). These 4 buttons would like to print, preview, and saving a simple Graphic. The problem is when I call PrintDocument1.print() , It does not redirect me to saving a file neither printer selection.

Here's my code:

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

I'd like to save my PrintDocument (that include e.graphic.drawstring) into PDF file locally within a Fialog to choose the file path.

Thank you guys, kindly need your help.

1

There are 1 best solutions below

1
Nforndzi Ngen On

You can create a pdfPrintDocument class to use in place of PrintDocument. A bitmap image is created for each page on which we are going to be drawing our graphics. Using iTextSharp library, the images are converted to pdf. Example on the use is here

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

Using the code in you form.

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