Form feed in c# printing

后端 未结 1 1837
心在旅途
心在旅途 2021-01-18 12:50

I am trying to do a form feed & skip 1 page while printing, however with the below lines of code, i am not able to make a form feed.

private void InserPa         


        
相关标签:
1条回答
  • 2021-01-18 13:36

    In .NET, the PrintPageEventArgs.HasMorePage property should be used to send a form feed to the printer. By calling e.Graphics.DrawString("\f", sFont, sBrush, 0, 0), you are simply rendering text to the document to be printed, which will never be interpreted by the printer as a form feed.

    Since you know where you want to break the page, instead of calling your InserPageBreak method, set PrintPageEventArgs.HasMorePages = true within your PrintPage event handler. That will send a form feed to the printer and your PrintPage event will continue to be fired until you set HasMorePages = false.

    I hope this helps. It may be useful to see how you have implemented your PrintPage event handler.

    Example:

    Use the BeginPrint handler to initialize data before printing

        void _document_BeginPrint(object sender, PrintEventArgs e)
        {
            //generate some dummy strings to print
            _pageData = new List<string>()
                    {
                        "Page 1 Data",
                        "Page 2 Data",
                        "Page 3 Data",
                    };
    
            // get enumerator for dummy strings
            _pageEnumerator = _pageData.GetEnumerator();
    
            //position to first string to print (i.e. first page)
            _pageEnumerator.MoveNext();
        }
    

    In the PrintPage handler, print a single page at a time, and set HasMorePages to indicate whether or not there is another page to print. In this example, three pages will print, one string on each page. After the 3rd page, _pageEnumerator.MoveNext() will return false, ending the print job.

        void _document_PrintPage(object sender, PrintPageEventArgs e)
        {
            Font sFont = new Font("Arial", 10);
            Brush sBrush = Brushes.Black;
    
            //print current page
            e.Graphics.DrawString(_pageEnumerator.Current, sFont, sBrush, 10, 10);
    
            // advance enumerator to determine if we have more pages.
            e.HasMorePages = _pageEnumerator.MoveNext();
        }
    
    0 讨论(0)
提交回复
热议问题