6

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.

Sharpeye500
  • 8,775
  • 25
  • 95
  • 143
  • Only out of curiosity, have you tried changing the brush to Black? – Joshua Drake May 15 '12 at 21:21
  • No, i tried with Black too, didn't work. – Sharpeye500 May 15 '12 at 21:34
  • See [PrintDocument.PrintPage Event](http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage%28v=vs.100%29.aspx) found from [Form feed usage?](http://bytes.com/topic/c-sharp/answers/274193-form-feed-usage). – Joshua Drake May 15 '12 at 21:54
  • Thanks, I have same kind of concept, unfortunately that URL given in that link from microsoft site is not opening. – Sharpeye500 May 15 '12 at 22:08
  • Do the raw links: http://msdn.microsoft.com/en-us/library/system.drawing.printing.printdocument.printpage%28v=vs.100%29.aspx or http://msdn.microsoft.com/en-us/library/cwbe712d.aspx work? – Joshua Drake May 15 '12 at 22:10
  • The first link, now it opens, thanks, but again that is not giving enough information to form feed. – Sharpeye500 May 15 '12 at 22:13
  • There is no concept of "form feed" in Windows printing, a page is the basic unit of print output. I wrote a PrintDocument replacement that emulates FormFeed(), you'll find the code here: http://social.msdn.microsoft.com/Forums/en/winforms/thread/577f1ecc-0434-4083-a50c-027db781595c – Hans Passant May 15 '12 at 23:54
  • Thanks, sorry i can't use your control in this project,though i appreciate your help, also when you look http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/what-character-escape-sequences-are-available.aspx explains there is \f for form feed. – Sharpeye500 May 16 '12 at 00:05
  • Any thoughts/some answers please? – Sharpeye500 May 16 '12 at 01:45
  • @Sharpeye500 The post you link states **"Of these, \a, \f, \v, \x and \U are rarely used in my experience."** see [Page break](http://en.wikipedia.org/wiki/Page_break) which states **"Form feed is seldom used when programming with modern printers in modern operating environments like Windows... Instead, form feeds are generated by having the printing program call a form feed API function... when printing using the .NET Framework, the PrintPageEventArgs.HasMorePages property is used to indicate a form feed is desired."** – Joshua Drake May 16 '12 at 15:51
  • `\f` to do a form feed only works when you're writing to a text stream (i.e. you opened the printer with a `StreamWriter` or similar). When you're printing to a canvas, `\f` is rendered as a character and you have to use the printer driver interface (such as `PrintPageEventArgs.HasMorePages`) to control paging and such. – Jim Mischel May 16 '12 at 19:49
  • @JimMischel - Do you have links that i can check for \f using streamwriter to do a form feed? I tried but not able to succeed. Create a file, write("Page1" , "\f", "Page2") via stream writer, create printdocument, print the page, however i don't see \f happening. – Sharpeye500 May 16 '12 at 21:51

1 Answers1

5

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();
    }
figabytes
  • 303
  • 1
  • 8
  • Thanks. I tried to set PrintPageEventArgs.HasMorePages = true; It didn't do a form feed. Do you any quick sample/example to show? – Sharpeye500 May 16 '12 at 23:02
  • 1
    I added a short sample. I tested it before posting and it does print 3 separate pages. – figabytes May 17 '12 at 02:24