Visual Basic

How To Print In Visual Basic

To print in a Visual Basic Windows Forms application, add a PrintDocument component, handle its PrintPage event and draw your text or images through e.Graphics. Call the document’s Print method to send the output to the selected printer. Use PrintDialog for printer selection and PrintPreviewDialog.

Which licences fit your PC setup?

Prices are an AUD snapshot, not a live quote. Confirm variant and total on the listing.



Choose the printing approach for your project

These instructions cover VB.NET Windows Forms applications in Visual Studio. Printing code depends on the environment: examples written for classic Visual Basic or an Office macro cannot simply be pasted into a Windows Forms project.

  • VB.NET Windows Forms: use System.Drawing.Printing.PrintDocument and its PrintPage event.
  • Classic VB6: older examples use the Printer object, including Printer.Print and Printer.EndDoc. Those commands belong to a different printing model.
  • Office VBA: use the host application’s printing methods. In Excel, for example, ActiveSheet.PrintOut prints the active sheet.

If you are working inside Office, start with opening Microsoft Visual Basic for Applications. For other language fundamentals, browse the Visual Basic guides.

Set up a basic Windows Forms print job

Start with one line of text. Once that prints correctly, extend the same drawing handler to produce a report, invoice or receipt. Microsoft’s PrintDocument overview explains the component and its role in Windows Forms printing.

  1. Open your form in the Visual Studio designer. Add a Button and a PrintDocument component from the Toolbox. Keep the document’s name as PrintDocument1 for the expressions below.

  2. Select PrintDocument1, open the Events view in the Properties window and double-click PrintPage. Visual Studio creates an event handler with an argument named e. Put the page-drawing instructions inside that handler.

  3. Add this statement inside the handler: e.Graphics.DrawString("Hello, World!", Me.Font, System.Drawing.Brushes.Black, e.MarginBounds.Left, e.MarginBounds.Top). It draws a black text line using the form’s font and the margin bounds as its starting position.

  4. Add e.HasMorePages = False after the drawing statement. This example produces a single page; it does not need another PrintPage call.

  5. Add a PrintDialog component named PrintDialog1. Double-click your button to create its Click handler, then assign PrintDialog1.Document = PrintDocument1 inside that handler.

  6. In the Click handler, test If PrintDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then. Inside that If block, call PrintDocument1.Print(), then close the block with End If. Cancelling the dialog must leave the Print call unexecuted.

  7. Run the application, click the button, select a printer and confirm. Check that the printed text appears in the intended position.

The button starts the job; PrintPage supplies its content. Calling Print does not automatically print the form’s controls or their contents. Your handler must draw the information you want on paper.

Set paper size, orientation and margins

Use PrintDocument1.PrinterSettings for the selected printer and copy count. Use PrintDocument1.DefaultPageSettings for paper size, orientation, margins and colour. Margins belong to PageSettings, rather than PrinterSettings.

For landscape output, set PrintDocument1.DefaultPageSettings.Landscape = True before printing. Choose a paper size supported by the selected printer from PrinterSettings.PaperSizes. A custom size is useful only when the driver and printer support it.

To let users configure the page, add PageSetupDialog, assign its Document property to PrintDocument1 and call ShowDialog(). Apply settings before opening preview so users can inspect the intended layout. See Microsoft’s DefaultPageSettings reference for the relevant properties.

Leave room for the printer’s non-printable edges. Generous document margins help, but check physical output when accurate placement matters, particularly for labels and pre-printed stationery.

Preview the document before printing

  1. Add a PrintPreviewDialog component to the form.
  2. Assign PrintPreviewDialog1.Document = PrintDocument1.
  3. From a separate Preview button’s Click handler, call PrintPreviewDialog1.ShowDialog().
  4. Inspect text wrapping, image placement and page breaks before sending the job to paper.

Preview uses the same PrintPage drawing logic as printing. That event can run again when the user prints, so avoid deleting source data or permanently advancing through records during preview. Microsoft’s print preview walkthrough demonstrates this shared rendering process.

Control page breaks explicitly

PrintDocument does not paginate your data automatically. Track the next line, character or record to print, and draw only what fits on the current page. Measure text with e.Graphics.MeasureString, allowing space for headings and footers.

If content remains, set e.HasMorePages = True and return from the handler. The next PrintPage event should continue from the saved position. Set it to False after the final page. Reset your position and page counter in BeginPrint so every preview or print operation starts at the beginning.

For conditional headers or different report layouts, keep the decision separate from the drawing instructions. The guide to nested If statements in Visual Basic can help when several conditions affect the output.

Draw images and tabular data

Use e.Graphics.DrawImage to place an image within a chosen rectangle. Preserve its proportions and use an image with enough detail for its printed size. Use DrawLine or DrawRectangle for borders, and DrawString for headings and cell values.

For database reports, retrieve the required records before rendering and keep a stable copy for preview and printing. Binding records to a DataGridView does not automatically produce a printed table: calculate column widths, row heights and page breaks yourself.

Dispose of fonts, images and brushes you create when they are no longer needed. Do not dispose of the e.Graphics object supplied by the printing framework.

Resolve common printing problems

  • Blank pages: confirm that PrintPage is connected to the correct PrintDocument and contains drawing instructions.
  • Repeated pages: advance the saved content position and set HasMorePages to False when finished.
  • Clipped content: check paper size, margins, text measurements and the printer’s printable area.
  • Missing content after preview: reset pagination state before each rendering operation.
  • Printer errors: check PrinterSettings.IsValid and handle exceptions around printing with Try…Catch. Paper jams or other faults may still need attention in the Windows print queue or at the device.

Run the single-line example through preview, then print one page on the intended printer. Check its placement before adding your application’s data.

Other editions for separate workloads

Other editions for separate workloads; prices in AUD, confirm the total on the listing.

How to check this worked

  1. Open Settings, System, Activation and confirm the edition shown.
  2. Enter the key and run activation; expect a green tick within a minute.
  3. Reboot and re-check activation survives the restart.

FAQ

What else should you know before buying?

What do I need before starting?
Read the requirements and confirm the edition first; the steps assume the machine is compatible.
How do I know the licence is genuine?
Activate on Microsoft's servers and check Settings, System, Activation. A working activation is the test.
Is this a subscription?
Keys sold here are one-time lifetime licences. Microsoft 365 plans are billed by Microsoft instead.
What if the key fails?
Email hi@ms.codes with the order number and a screenshot. Replacement or refund applies within 30 days.

Check who prepared and reviewed this guide

Written by
MS.Codes editorial
Reviewed by
Support team

Last verified: (source review; see test status above).

Recent Post