简体   繁体   中英

Form feed in c# printing

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 InserPageBreak(System.Drawing.Printing.PrintPageEventArgs e)
{
      Font sFont = new Font("Arial", 10);
      Brush sBrush = Brushes.White;
      e.Graphics.DrawString("\f", sFont, sBrush, 0, 0);
}

I use PrintDialog to print the page contents. I am using "\\f" C#'s form feed character.

Any thoughts on how to implement/make this form feed to work ?

PS: I even tried this:

//ASCII code 12 - printer's form feed control code.

 string test = char.ConvertFromUtf32(12);
 e.Graphics.DrawString(test, sFont, sBrush, 0, 0);

internally c# converts that to "\\f", but didn't do form feed, anyone who has implemented "\\f", please share your thoughts.

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM