如何在C#中将长字符串打印到多个页面

时间:2017-07-07 21:19:30

标签: c#

我正在尝试使用下面的代码打印一个很长的字符串,但它只在一个页面中打印整个文本。有没有简单的方法正确打印?

string text="the text has like 1000 words";

System.Drawing.Printing.PrintDocument p = new System.Drawing.Printing.PrintDocument();

p.PrintPage += delegate (object sender1, System.Drawing.Printing.PrintPageEventArgs e1)
{
    e1.Graphics.DrawString(text, new System.Drawing.Font("Times New Roman", 12), new System.Drawing.SolidBrush(System.Drawing.Color.Black), new System.Drawing.RectangleF(50, 50, p.DefaultPageSettings.PrintableArea.Width - 50, p.DefaultPageSettings.PrintableArea.Height - 50));

};
try
{
    p.Print();
}
catch (Exception ex)
{
    throw new Exception("Exception Occured While Printing", ex);
}

我试图用this article来解决它,但是这个方法陷入了一些我不知道如何解决的循环。

3 个答案:

答案 0 :(得分:1)

您必须记住,只要告诉它有更多页面,PrintDocument就会引发事件PrintPage。您的任务是跟踪打印的内容,接下来需要打印的内容以及是否需要其他页面。

有几种方法可以实现这一目标。在这种情况下,我选择了获取text的内容,检查它在当前页面上的大小,DrawString,然后更新我的处理字符串,称为remainingtext,以便能够重复为下一页。在我们完成时,通过将PrintEvent参数实例的HasMorepages设置为true或false来控制是否需要下一页。

以下是代码:

PrintDocument p = new PrintDocument();

var font = new Font("Times New Roman", 12);
var margins = p.DefaultPageSettings.Margins;
var layoutArea = new RectangleF(
    margins.Left, 
    margins.Top, 
    p.DefaultPageSettings.PrintableArea.Width - (margins.Left + margins.Right ), 
    p.DefaultPageSettings.PrintableArea.Height - (margins.Top + margins.Bottom));
var layoutSize = layoutArea.Size;
layoutSize.Height = layoutSize.Height - font.GetHeight(); // keep lastline visible
var brush = new SolidBrush(Color.Black);

// what still needs to be printed
var remainingText = text;

p.PrintPage += delegate (object sender1, PrintPageEventArgs e1)
{
    int charsFitted, linesFilled;

    // measure how many characters will fit of the remaining text
    var realsize = e1.Graphics.MeasureString(
        remainingText, 
        font, 
        layoutSize, 
        StringFormat.GenericDefault, 
        out charsFitted,  // this will return what we need
        out linesFilled);

    // take from the remainingText what we're going to print on this page
    var fitsOnPage = remainingText.Substring(0, charsFitted);
    // keep what is not printed on this page 
    remainingText = remainingText.Substring(charsFitted).Trim();

    // print what fits on the page
    e1.Graphics.DrawString(
        fitsOnPage, 
        font, 
        brush, 
        layoutArea);

    // if there is still text left, tell the PrintDocument it needs to call 
    // PrintPage again.
    e1.HasMorePages = remainingText.Length > 0;
};
p.Print();

当我连接PrintPreviewControl时,结果如下:

shows the printpage in action

答案 1 :(得分:1)

感谢rene,我想出了如何正确打印它。我编辑他的代码,这里的不同之处在于用户选择纸张尺寸,并且选择纸张边距大小的矩形绘图。如果您想使用它,这是一种现成的打印方法......

private void Print(string thetext){


            try
            {

                System.Drawing.Printing.PrintDocument p = new System.Drawing.Printing.PrintDocument();

                var font = new Font("Times New Roman", 12); 
                var brush = new System.Drawing.SolidBrush(System.Drawing.Color.Black);

                // what still needs to be printed
                var remainingText = theText;

                p.PrintPage += delegate (object sender1, System.Drawing.Printing.PrintPageEventArgs e1)
                {
                    int charsFitted, linesFilled;

                // measure how many characters will fit of the remaining text

                var realsize = e1.Graphics.MeasureString(
                        remainingText,
                        font,
                        e1.MarginBounds.Size, 
                        System.Drawing.StringFormat.GenericDefault,
                        out charsFitted,  // this will return what we need
                        out linesFilled);

                // take from the remainingText what we're going to print on this page
                var fitsOnPage = remainingText.Substring(0, charsFitted);
                // keep what is not printed on this page 
                remainingText = remainingText.Substring(charsFitted).Trim();

                // print what fits on the page
                e1.Graphics.DrawString(
                        fitsOnPage,
                        font,
                        brush,
                        e1.MarginBounds); 

                    // if there is still text left, tell the PrintDocument it needs to call 
                    // PrintPage again.
                    e1.HasMorePages = remainingText.Length > 0;
                };

                System.Windows.Forms.PrintDialog pd = new System.Windows.Forms.PrintDialog();  
                pd.Document = p; 
                DialogResult result = pd.ShowDialog();

                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    p.Print();
                } 


            }catch(Exception e2)
            {
                System.Windows.MessageBox.Show(e2.Message, "Unable to print",MessageBoxButton.OK);
            }

}

答案 2 :(得分:0)

“用户单击“ btnPrint”按钮时,打印“ rtbEditor”富文本框文本的代码段”:

        private void btnPrint_Click(object sender, EventArgs e) {

            var d = new PrintDialog();
            d.Document = new PrintDocument();
            d.Document.PrintPage += Dd_PrintPage;
            remainingText = rtbEditor.Text;
            var res = d.ShowDialog();
            try { if (res == DialogResult.OK) d.Document.Print(); }
            catch { }
        }

        string remainingText;
        private void Dd_PrintPage(object sender, PrintPageEventArgs e){

            //this code was for 1 page
            //e.Graphics.DrawString(rtbEditor.Text,new Font("Comic Sans MS", 12f),Brushes.Black,new PointF(10,10));

            //this is for multiple pages
            PrintDocument p = ((PrintDocument)sender);
            var font = new Font("Times New Roman", 12);
            var margins = p.DefaultPageSettings.Margins;
            var layoutArea = new RectangleF(
                margins.Left,
                margins.Top,
                p.DefaultPageSettings.PrintableArea.Width - (margins.Left + margins.Right),
                p.DefaultPageSettings.PrintableArea.Height - (margins.Top + margins.Bottom));
            var layoutSize = layoutArea.Size;
            layoutSize.Height = layoutSize.Height - font.GetHeight(); // keep lastline visible
            var brush = new SolidBrush(Color.Black);
            int charsFitted, linesFilled;

            // measure how many characters will fit of the remaining text
            var realsize = e.Graphics.MeasureString(
                remainingText,
                font,
                layoutSize,
                StringFormat.GenericDefault,
                out charsFitted,  // this will return what we need
                out linesFilled);
            // take from the remainingText what we're going to print on this page
            var fitsOnPage = remainingText.Substring(0, charsFitted);
            // keep what is not printed on this page 
            remainingText = remainingText.Substring(charsFitted).Trim();

            // print what fits on the page
            e.Graphics.DrawString(
                fitsOnPage,
                font,           
                brush,
                layoutArea);

            // if there is still text left, tell the PrintDocument it needs to call 
            // PrintPage again.
            e.HasMorePages = remainingText.Length > 0;
        }