#How do i add a page to windows 2011 document code#
You can also build this example in Visual Studio by pasting the code into a new project.Microsoft Word is the commercial word processing program that the Microsoft Corporation created. References to the System,, System.Drawing assemblies.įor information about building this example from the command line for Visual Basic or Visual C#, see Building from the Command Line or Command-line Building With csc.exe.
Private Sub printButton_Click(ByVal sender As Object, ByVal e As EventArgs)Īpplication.SetCompatibleTextRenderingDefault(False)Ī text file named testPage.txt containing the text to print, located in the root of drive C:\. Private void printButton_Click(object sender, EventArgs e)Īpplication.SetCompatibleTextRenderingDefault(false) Private void printDocument1_PrintPage(object sender, PrintPageEventArgs e) New PrintPageEventHandler(printDocument1_PrintPage) Associate the PrintPage event handler with the PrintPage event. += new System.EventHandler(this.printButton_Click) Private PrintDocument printDocument1 = new PrintDocument() ' Check to see if more pages are to be printed.Į.HasMorePages = stringToPrint.Length > 0Ĭall the Print method to raise the PrintPage event. 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, 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, _ Check to see if more pages are to be printed.Į.HasMorePages = (stringToPrint.Length > 0) 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 private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
In the following code example, the event handler is used to print the contents of the "testPage.txt" file in the same font as is used on the form. Also, make sure the PrintPage event is associated with its event-handling method. The PrintPage event is raised until HasMorePages is false. After each page is drawn, check to see if it is the last page, and set the HasMorePages property of the PrintPageEventArgs accordingly. In the PrintPage event handler, use the Graphics property of the PrintPageEventArgs class and the document contents to calculate line length and lines per page. Using (StreamReader reader = new StreamReader(stream))ĭim stream As New FileStream(docPath + docName, FileMode.Open) Using (FileStream stream = new FileStream(docPath + docName, FileMode.Open)) If printing a document, set the DocumentName property to the document you wish to print, and open and read the documents contents to the string you added previously. Private printDocument1 As New PrintDocument() private PrintDocument printDocument1 = new PrintDocument() To print textĪdd a PrintDocument component and a string to your form. You should always use the DrawString methods of Graphics, as shown in the following code example, to draw text for printing purposes.
The DrawText methods of TextRenderer are not supported for printing.