![]() ' Draws the string within the bounds of the page.Į.Graphics.DrawString(stringToPrint, Me.Font, Brushes.Black,Į.MarginBounds, StringFormat.GenericTypographic) StringFormat.GenericTypographic, charactersOnPage, linesPerPage) ' of stringToPrint that will fit within the bounds of the page.Į.Graphics.MeasureString(stringToPrint, Me.Font, e.MarginBounds.Size, ' Sets the value of charactersOnPage to the number of characters Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles PrintDocument1.PrintPage If there are no more pages, reset the string to be printed. Check to see if more pages are to be printed.Į.HasMorePages = (stringToPrint.Length > 0) StringToPrint = stringToPrint.Substring(charactersOnPage) Remove the portion of the string that has been printed. Draws the string within the bounds of the page.Į.Graphics.DrawString(stringToPrint, this.Font, Brushes.Black,Į.MarginBounds, StringFormat.GenericTypographic) of stringToPrint that will fit within the bounds of the page.Į.Graphics.MeasureString(stringToPrint, this.Font,Į.MarginBounds.Size, StringFormat.GenericTypographic, Sets the value of charactersOnPage to the number of characters void PrintDocument1_PrintPage(object sender, PrintPageEventArgs e) In the following code example, the event handler is used to print the "testPage.txt" file in the same font used on the form. If you have implemented printing in your application, you may have already completed step 5 and 6. Also, ensure that the PrintPage event is associated with its event-handling method. When the document has finished rendering, reset the string to be rendered. The PrintPage event is raised until HasMorePages is false. After each page is drawn, check to see if it's the last page, and set the HasMorePages property of the PrintPageEventArgs accordingly. StringToPrint = System.IO.File.ReadAllText(fullPath)Īs you would for printing the document, in the PrintPage event handler, use the Graphics property of the PrintPageEventArgs class and the file contents to calculate lines per page and render the document's contents. ![]() StringToPrint = System.IO.File.ReadAllText(fullPath) ĭim docPath As String = "C:\Users\v-rsatao\Desktop\"ĭim fullPath As String = System.IO.Path.Combine(docPath, docName) String docPath = fullPath = System.IO.Path.Combine(docPath, docName) ' Declare a variable to hold the portion of the document thatīack in the Click event handler code, set the DocumentName property to the document you wish to print, and open and read the document's contents to the string you added previously. ' Declare a string to hold the entire document contents. Declare a variable to hold the portion of the document that Outside the scope of the event handler, add two private string variables to the class named documentContents and stringToPrint: // Declare a string to hold the entire document contents. In the Properties pane, select the Event filter button and then double-click the Click event to generate an event handler. ![]() In the Visual Designer of the form, select the button. ![]() This opens the Visual Designer.įrom the Toolbox pane, double-click both the PrintDocument component and the PrintPreviewDialog component, to add them to the form.Įither add a Button to the form, or use a button that is already on the form. In Visual Studio, use the Solution Explorer pane and double-click the form you want to print from. To preview a text document with a PrintPreviewDialog control An easy way to add print preview services to your application is to use a PrintPreviewDialog control in combination with the PrintPage event-handling logic for printing a file. It's common in Windows Forms programming to offer print preview in addition to printing services.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |