Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 860
Sharpeye500 Avatar asked May 15 '12 21:05

Sharpeye500


People also ask

What is form feed and line feed?

Line feed ( \n or 0xA ): To Take control at starting on the next line. Form feed ( \f or 0xC ): To take control at starting on the next page.

What means form feed?

form feed (plural form feeds) (computing) A control character traditionally used to cause the printer to eject the current page of output and start a new page, more recently equivalent to a carriage return.

What is carriage return in C#?

The letters hits the paper pressed against the rubberized bar; the bar with the paper is mounted in the "carriage" (it "carries" it) which moves horizontally to move from one character position to the next one. When the line is complete, it moves all the way to the left. It corresponds to "\r", "carriage return".


1 Answers

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();
    }
like image 185
figabytes Avatar answered Sep 28 '22 04:09

figabytes