换用C#打印 [英] Form feed in c# printing

查看:196
本文介绍了换用C#打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个进纸和放大器;跳过1页,而打印,但与下面的行code,我不能够做一个换。

 私人无效InserPageBreak(System.Drawing.Printing.PrintPageEventArgs E)
{
      字体sFont =新的字体(宋体,10);
      刷sBrush = Brushes.White;
      e.Graphics.DrawString(\ f,采用sFont,sBrush,0,0);
}
 

我使用PrintDialog类进行打印的页面内容。我使用\ FC#的换页符。

这是如何实现的任何想法/做出这种换工作?

PS:我甚至尝试这样的:

// ASCII code 12 - 打印机的进纸控制code

 字符串测试= char.ConvertFromUtf32(12);
 e.Graphics.DrawString(测试,sFont,sBrush,0,0);
 

在内部C#转换,为\ F,但没有做进纸,谁实施了\ F任何人,请分享你的看法。

解决方案

在.NET中,PrintPageEventArgs.HasMorePage属性应该被用来发送一个换页到打印机

。通过调用e.Graphics.DrawString(\ F,sFont,sBrush,0,0)要打印,你只是渲染文本文档,这将永远是由打印机作为换PTED除$ P $

而不是调用你的InserPageBreak方法,因为你知道你想要打破页,集PrintPageEventArgs.HasMorePages =您的PrintPage事件处理程序中真实的。这将发出一个换到打印机和您的PrintPage事件将继续被解雇,直到你设置HasMorePages的=假。

我希望这有助于。这可能是有用的,看看你是如何实现你的PrintPage事件处理程序。

例如:

使用的BeginPrint处理程序在打印之前初始化数据

 无效_document_BeginPrint(对象发件人,PrintEventArgs E)
    {
        //产生一些虚拟的字符串打印
        _pageData =新的名单,其中,串>()
                {
                    第1数据,
                    第2页数据,
                    第3页数据,
                };

        //获取枚举哑弦
        _pageEnumerator = _pageData.GetEnumerator();

        //位置,第一个字符串打印(即第一页)
        _pageEnumerator.MoveNext();
    }
 

在的PrintPage处理程序,在同一时间打印单页,并设置HasMorePages的指示是否有另一个页面进行打印。在本例中,三​​页将打印,一根弦的每一页上。第3页后,_pageEnumerator.MoveNext()将返回false,结束打印作业。

 无效_document_PrintPage(对象发件人,PrintPageEventArgs E)
    {
        字体sFont =新的字体(宋体,10);
        刷sBrush = Brushes.Black;

        //打印当前页面
        e.Graphics.DrawString(_pageEnumerator.Current,sFont,sBrush,10,10);

        //提前枚举来确定,如果我们有更多的页面。
        e.HasMorePages = _pageEnumerator.MoveNext();
    }
 

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

这篇关于换用C#打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆