Search code examples
c#.netwinformsprintdocument

How to cancel a print in extended OnBeginPrint method?


I have created a win form application to print labels and it is working fine. The only problem is the content is cut off when it has more content than the label size. I want to show a notification when the content length is greater than the height of the label. A possible place to calculate that is in method OnBeginPrint() where I'm getting all the parameters to calculate the dimensions. But I didn't find any parameter or property which cancels the print when the condition fails. Please share your thoughts on this.


Solution

  • Most of events raising before some action begins has Cancel property on *EventArgs arguments. In this case, it's PrintEventArgs.Cancel.

    Sample:

    void MyPrintDocument_BeginPrint(object sender, PrintEventArgs e) {
       e.Cancel = true; // will cancel printing
    }