. Net (C #) Printing-Multi-page printing

Source: Internet
Author: User

If you want to implement multi-page printing, use the HasMorePages property of the PrintPageEventArgs class.

We make the following changes to the previous code:
Increased PrintDocument of BeginPrint and EndPrint events. The BeginPrint event is used to get the printed content. EndPrint is used to release resources. Paging is implemented in the PrintDocument PrintPage event.
Kizhong: The BeginPrint event method is called before the PrintPage event method.
The Pintpage event method is called before the Endprintpage event method.
The EndPrint event method is called at last, and the EndPrint event method is returned to the Printdocument.print () method to perform the printing.

The process is as follows:
1. Instantiate the printed document
2, Subscribe to events (subscribe to the BeginPrint event, to get the content to be printed; Pinrtpage event for drawing individual page contents; EndPrint event for freeing resources)
3, call the method of BeginPrint event, get the print content
4, call the method of the Pinrtpage event, draw multiple printed pages, and according to the judgment, set whether to print multiple pages
5. Call the EndPrint event method, release the resource, and start printing when finished

The code is as follows:


Using System.IO;
Using System.Drawing.Printing;

Namespace Simpleeditor
{
public partial class Simpleeditorform:form
{
private string filename = "Untitled";

1. Instantiate the printed document
PrintDocument pddocument = new PrintDocument ();

Private string[] lines;
private int linesprinted;
Public Simpleeditorform ()
{
InitializeComponent ();

2. Subscribe to Events

Subscribe to Pinrtpage events for drawing individual page content
Pddocument.printpage + = new Printpageeventhandler (onprintpage);
Subscribe to the BeginPrint event to get the printed content
Pddocument.beginprint + = new Printeventhandler (pddocument_beginprint);
Subscribe to the EndPrint event to release resources
Pddocument.endprint + = new Printeventhandler (pddocument_endprint);
}

private void OnFilePrint (object sender, EventArgs e)
{
Try
{
Call to print
Pddocument.print ();

/*
* The print () method of the PrintDocument object executes the PrintPage event with the help of the Printcontroller class.
*/
}
catch (Invalidprinterexception ex)
{
MessageBox.Show (ex. Message, "Simple Editor", MessageBoxButtons.OK, Messageboxicon.error);
Throw
}
}

<summary>
3. Get Printed Content
Each print task only calls OnBeginPrint () once.
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Pddocument_beginprint (object sender, PrintEventArgs e)
{
char[] param ={' \ n '};
lines = TextBoxEdit.Text.Split (param);

int i = 0;
char[] Trimparam ={' \ R '};
foreach (string s in lines)
{
lines[i++] = S.trimend (Trimparam);
}
}




<summary>
4. Draw multiple printed pages
PrintDocument's PrintPage Event
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Onprintpage (object sender, PrintPageEventArgs e)
{
/*
* Get an array of strings for each row in the textbox
* \ n line break
* \ r Enter
*/

int x = 20;
int y = 20;
while (Linesprinted<lines. Length)
{
Draw a page to print
E.graphics.drawstring (lines[linesprinted++], new Font ("Arial", ten), Brushes.black, x, y);

Y + = 55;

Allows multiple pages to be printed when more than one page is judged
if (y >= e.pagebounds.height-80)
{
Allow multi-page printing
E.hasmorepages = true;

/*
* When the Haemorepages property of the PrintPageEventArgs class is true, notifies the control device that the Onprintpage () method must be called again to print a page.
* Printloopi () has a sequence example for each page to be printed. If HasMorePages is False,printloop () it will stop.
*/
Return
}
}

linesprinted = 0;
Turn off the multi-page printing feature after drawing is complete
E.hasmorepages = false;

}

<summary>
5. EndPrint events, releasing resources
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
void Pddocument_endprint (object sender, PrintEventArgs e)
{
The variable lines occupies and references a string array, which is now freed
lines = null;
}
}
This completes the multi-page printing function.

PageSetupDialog:
You can configure the page size and paper source, orientation, and margins, because these options depend on the printer, so you can also select a printer in this dialog box.

Allowpager

Alloworientation

Pagesettings.landscape

Allowprinter

Pagesettings.margins

Minmagins

Allowmargins

Pagesettings.papersource

Pagesettings.papersize

It has some properties such as:

Description

1. Page
Allowpaper: Whether the page size and paper source can be selected.
The PageSetupDialog.PageSetting.PaperSize property returns an instance of PaperSize with its properties height, width, and papername to read the height, breadth, and paper name.
Papername specify names such as letter and A4.
The Kind property returns an enumeration from which to get a value for the PaperKind enumeration, PaperKind enumeration contains many different paper sizes, such as A3, A4, A5, letter, Letterplus, and letterrotated.
The PageSetupDiaog.PageSettings.PaperSource property returns an PaperSource instance that can read the printer paper source and the corresponding paper type (as long as the printer is properly configured with the printer settings).

2. Page margins
The Allowmargins property allows the user to set the margin value of the printout.
Minmargins can define the minimum margin value entered for the user.
PageSetupDialog.PageSettings.Margins reads the margins, and the values have the Bottom,left,right and top properties.

3. Direction
The Alloworientation property defines whether users can choose to print vertically and horizontally.
The PageSetupDialog.PageSettings.Landscape value can read the selected value. True horizontally, false vertically

4. Printer
The Allowprinter property specifies whether the user can select a printer.



The use of PageSetupDialog

The method of using PageSetupDialog is relatively simple, just
1, in the instantiation of a PageSetupDialog
2. Set document property to documents that need to be printed
3. Call the ShowDialog () method to open the layout settings

. Net (C #) Printing-Multi-page printing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.