none
Print button RRS feed

  • Question

  • Hello everyone... 

    Im trying to creat a contract form for the business i manage. Im trying to get a print button at the bottom of the form so that once i complete the form I can hit that and everything prints out, and saves a .pdf of the current worksheet and then reverts it back to the original unedited form.... I havent really gotten ever gotten into word this far but have seen its possible.... Anyone have any ideas on if this is still possible and or how to do it!!!!

    thanks in advance

    lundi 26 octobre 2020 23:38

Toutes les réponses

  • Hi

    Here is an example of a very simple Print routine.

    This is a stand alone example and need a blank Form1 and copy/replace code with this code.

    This example just prints a text file, but can be changed to suit what you want.

    If you want further help you should post details of the form you want to print - is it a series of textboxes, panels, datagridview, etc etc etc.

    *

    As for the 'saves a .pdf' - that is another question and you should start a new thread for it (better after you have solved the print question though)

    BYW: why .PDF?

    ' STAND ALONE PRINTING EXAMPLE
    ' NO CONTROLS NEEDED IN DESIGNER
    ' AMMEND PATH IN "stringToPrint" VARIABLE
    ' TO A LONG TEXT FILE ON YOUR SYSTEM
    Option Strict On
    Option Explicit On
    Imports System.Drawing.Printing
    Public Class Form1
    	Dim WithEvents PDoc As New PrintDocument
    	Dim pPrev As New PrintPreviewDialog
    
    	' for this eample, set the following to
    	' a text file on your system.
    	Dim stringToPrint As String = IO.File.ReadAllText("C:\Users\lesha\Desktop\TESTTEXTDOC.txt")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		pPrev.Document = PDoc
    		pPrev.ShowDialog()
    	End Sub
    	Private Sub PrintDocument1_PrintPage(sender As Object, e As PrintPageEventArgs) Handles PDoc.PrintPage
    
    		' This example Prints the Text from a text
    		' File.  It can be made to print anything
    		' you just have to tell it what needs to
    		' be Printed (and take care of any further
    		' pages to be printed)
    
    		' ******* FROM MS DOCS *******
    
    		Dim charactersOnPage As Integer = 0
    		Dim linesPerPage As Integer = 0
    
    		' Sets the value of charactersOnPage to the number of characters 
    		' of stringToPrint that will fit within the bounds of the page.
    		e.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size,
    				StringFormat.GenericTypographic, charactersOnPage, linesPerPage)
    
    		' Draws the string within the bounds of the page
    		e.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black,
    				e.MarginBounds, StringFormat.GenericTypographic)
    
    		' Remove the portion of the string that has been printed.
    		stringToPrint = stringToPrint.Substring(charactersOnPage)
    
    		' Check to see if more pages are to be printed.
    		e.HasMorePages = stringToPrint.Length > 0
    
    	End Sub
    End Class


    Regards Les, Livingston, Scotland



    mardi 27 octobre 2020 00:05