How to create print rule with custome design page in c#
Loading
How to create print rule with custome design page in c#
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mohamed Azarudeen ZRank #297 · 8.5K reputationPosted Jun 19, 2023, 5:41 AM EDT
Hi arvnd
Create a new Windows Forms Application project in Visual Studio.
Add a button to your form that will trigger the printing action. You can do this by dragging and dropping a Button control from the Toolbox onto your form.
Double-click on the button to generate the click event handler and add the following code inside the event handler:
private void printButton_Click(object sender, EventArgs e) { PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += PrintDocument_PrintPage; PrintDialog printDialog = new PrintDialog(); printDialog.Document = printDocument; if (printDialog.ShowDialog() == DialogResult.OK) { printDocument.Print(); } }Implement the
PrintDocument_PrintPageevent handler to define the custom printing logic. This event will be triggered for each page of the document to be printed. Inside this event handler, you can use theGraphicsobject to draw your custom content on the page. Here's an example that draws a simple message on the page:private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) { Graphics graphics = e.Graphics; float x = e.MarginBounds.Left; float y = e.MarginBounds.Top; string message = "Hello, World!"; using (Font font = new Font("Arial", 12)) { graphics.DrawString(message, font, Brushes.Black, x, y); } }Run the application, and when you click the button, the print dialog will appear, allowing you to select the printer and adjust print settings. After confirming the settings, the
PrintDocument_PrintPageevent will be triggered for each page, and the custom content will be drawn on each page.Deepak RawatRank #646 · 3.7K reputationPosted Jun 19, 2023, 6:49 AM EDT
private void PrintButton_Click(object sender, EventArgs e) { PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += new PrintPageEventHandler(PrintDocument_PrintPage); // Optionally, you can customize the print settings, such as page orientation, margins, etc. // printDocument.DefaultPageSettings... PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog(); printPreviewDialog.Document = printDocument; printPreviewDialog.ShowDialog(); } private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) { // Obtain the Graphics object for drawing Graphics graphics = e.Graphics; // Define the content and layout of the print document using the graphics object // Draw text, images, tables, or any other desired content // Use methods like graphics.DrawString, graphics.DrawImage, graphics.DrawRectangle, etc. // Example: string text = "Hello, World!"; Font font = new Font("Arial", 12, FontStyle.Regular); PointF point = new PointF(100, 100); graphics.DrawString(text, font, Brushes.Black, point); }PrintButton_Clickevent handler is responsible for initiating the print process. It creates an instance of thePrintDocumentclass and subscribes to itsPrintPageevent. ThePrintDocument_PrintPageevent handler is responsible for defining the content and layout of the print document using theGraphicsobject.You can customize the print document as per your requirements, adding your own design, layout, and content using various
Graphicsmethods.Rajkiran SwainRank #165 · 14.4K reputationPosted Jun 19, 2023, 6:57 AM EDT
To create a print rule with a custom-designed page in C#, you can follow these general steps:
Design the print page: Create a custom-designed page using a library like Windows Forms or WPF. You can add various controls, such as labels, images, and text boxes, to represent the content you want to print.
Set up a PrintDocument object: Instantiate a PrintDocument object, which represents the document to be printed. Set the properties of the PrintDocument object, such as the document name and the event handlers for printing-related events.
Handle the PrintPage event: Implement the event handler for the PrintPage event of the PrintDocument object. This event is raised for each page to be printed. In the event handler, you can use the Graphics object provided by the event arguments to draw the content onto the print page based on your custom design.
Handle the BeginPrint and EndPrint events (optional): If necessary, you can implement event handlers for the BeginPrint and EndPrint events to perform any initialization or cleanup tasks before and after the printing process.
Call the Print method: After setting up the print document and its event handlers, you can call the Print method of the PrintDocument object to start the printing process. This will trigger the PrintPage event for each page to be printed.
Here's a simplified example using Windows Forms:
private void PrintButton_Click(object sender, EventArgs e) { PrintDocument printDocument = new PrintDocument(); printDocument.DocumentName = "Custom Print Page"; printDocument.PrintPage += PrintDocument_PrintPage; // Optionally handle BeginPrint and EndPrint events // Call the Print method to start printing printDocument.Print(); } private void PrintDocument_PrintPage(object sender, PrintPageEventArgs e) { // Draw your custom design onto the print page using the Graphics object Graphics graphics = e.Graphics; // Example: Draw a text string string text = "Hello, World!"; Font font = new Font("Arial", 12); Brush brush = Brushes.Black; PointF position = new PointF(50, 50); graphics.DrawString(text, font, brush, position); // Add more drawing operations as needed for your custom design }