100分请教 C#自定义打印第几页共几页如何实现

时间:2022-08-30 19:51:46

如题

需要自己算出 共几页,并用循环变量得出第几页吗?

12 个解决方案

#1


是的

回复内容太短了!

#2


还有一个问题


C#打印预览组件上的打印按钮,

如何屏蔽掉呢 ?

#3



有人能回答吗?

#4


有点奇怪
帮顶!~~~~~

#5


在winform中可使用printdocument打印某页
printdocument.PrinterSettings.PaperSizes 
PrintDocument.PrinterSettings.FromPage = 2; 
PrintDocument.PrinterSettings.ToPage = 2;

#6


不会,学习下,帮顶~~等待牛人.

#7


这是我才写的一个关于打印人算法,LZ可以看一下
int _totallines;//总行数
        int _totalpages = 0;//总页面数
        float _lineheight;//行高
        int _pagelines;//每页行数
        int _curlines = 0;//当前行号(this.Editor)
        bool _isAll = true;//是否已经完全显示
        float _lastlineheight;//上次已打印高度
        Bitmap _backImage;//背景图片
        Bitmap _reallyback;
        public Bitmap ReallyBack
        {
            set
            {
                this._reallyback = value;
            }
        }
        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            SizeF sf;
            RectangleF rec;
            int num = 0, j;
            float curY = e.MarginBounds.Top+5.0f;//修正值
            //计算出行高,总行数,总页数.
            if (_totalpages == 0)
            {
                _lineheight = this.Editor.Font.GetHeight(e.Graphics);
                _pagelines = (int)(e.MarginBounds.Height / _lineheight);
                for (int i = 0; i < this.Editor.Lines.Length; i++)
                {
                    sf = e.Graphics.MeasureString(this.Editor.Lines[i], this.Editor.Font, e.MarginBounds.Width);
                    _totallines += (int)(sf.Height / _lineheight);
                }
                _totalpages = _totallines / _pagelines + (_totallines % _pagelines == 0 ? 0 : 1);
            }

            //显示内容
            e.Graphics.SetClip(new RectangleF(e.PageBounds.Left,e.MarginBounds.Top,e.PageBounds.Width,e.PageBounds.Height-(e.PageBounds.Height-e.MarginBounds.Height)/2));
            for (j = _curlines; j < this.Editor.Lines.Length; j++)
            {
                if (curY + _lineheight > e.MarginBounds.Height + e.MarginBounds.Top)
                    break;
                //对上一页面进行判断,看上一面是否已经完全显示
                //没有
                if (_isAll == false)
                {

                    sf = e.Graphics.MeasureString(this.Editor.Lines[j], this.Editor.Font, e.MarginBounds.Width);
                    rec = new RectangleF(new PointF(e.MarginBounds.Left, e.MarginBounds.Top - _lastlineheight + 5.0f), sf);//0.5f修正值
                    num += (int)((rec.Height - _lastlineheight) / _lineheight);
                    curY += rec.Height - _lastlineheight;
                    _isAll = true;
                    _lastlineheight = 0;
                    //e.Graphics.SetClip(e.PageBounds);
                }
                //已经完全显示
                else
                {
                    sf = e.Graphics.MeasureString(this.Editor.Lines[j], this.Editor.Font, e.MarginBounds.Width);
                    num += (int)(sf.Height / _lineheight);
                    //curY += sf.Height;
                    //当当前页不能完全显示第j行时
                    if (curY + sf.Height > e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        rec = new RectangleF(e.MarginBounds.Left, curY, sf.Width, sf.Height - (num - _pagelines) * _lineheight);
                        this._isAll = false;
                        _lastlineheight = sf.Height - (num - _pagelines) * _lineheight;
                        curY += _lastlineheight;
                    }
                    //能完全显示.
                    else
                    {
                        rec = new RectangleF(e.MarginBounds.Left, curY, sf.Width, sf.Height);
                        curY += rec.Height;

                    }
                }
                e.Graphics.DrawString(this.Editor.Lines[j], this.Editor.Font, new SolidBrush(this.Editor.ForeColor), rec);

            }
            e.Graphics.SetClip(e.PageBounds);
            _curlines = j;

            if (num > _pagelines)//当未完全显示此行时,在打印下一页是还是停留在该行.
                _curlines -= 1;

            if (_curlines < this.Editor.Lines.Length)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;

            e.Graphics.DrawImage(_backImage, 0, 0);
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), e.MarginBounds);
            e.Graphics.DrawString(this.Editor.Font.Size.ToString(), this.Editor.Font, new SolidBrush(Color.Blue), 0, 0);

        }

        private void menuPrintPriview_Click(object sender, EventArgs e)
        {
            //Bitmap bmp = (Bitmap)Bitmap.FromFile(Application.StartupPath + "\\swpaflag.jpg");
            _backImage = new Bitmap(_reallyback.Width, _reallyback.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int i = 0; i < _reallyback.Width; i++)
                for (int j = 0; j < _reallyback.Height; j++)
                    _backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));

                    //
            (this.printPDialog as Form).WindowState = FormWindowState.Maximized;
            this.printPDialog.ShowDialog();
                    _totalpages = 0;
            _isAll=true;
            _curlines = 0;
        }

        private void menuPrintSetup_Click(object sender, EventArgs e)
        {
            this.pageSetDialog.ShowDialog();
        }

#8


引用 5 楼 wuyq11 的回复:
在winform中可使用printdocument打印某页
printdocument.PrinterSettings.PaperSizes 
PrintDocument.PrinterSettings.FromPage = 2; 
PrintDocument.PrinterSettings.ToPage = 2;

dui

#9


 this.Editor是什么?

#10


帮顶                        d

#11


backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));
这句话是什么意思

#12


引用 11 楼 markfeier 的回复:
backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));
这句话是什么意思

是设置图片中的指定的像素(i,j)的颜色

#1


是的

回复内容太短了!

#2


还有一个问题


C#打印预览组件上的打印按钮,

如何屏蔽掉呢 ?

#3



有人能回答吗?

#4


有点奇怪
帮顶!~~~~~

#5


在winform中可使用printdocument打印某页
printdocument.PrinterSettings.PaperSizes 
PrintDocument.PrinterSettings.FromPage = 2; 
PrintDocument.PrinterSettings.ToPage = 2;

#6


不会,学习下,帮顶~~等待牛人.

#7


这是我才写的一个关于打印人算法,LZ可以看一下
int _totallines;//总行数
        int _totalpages = 0;//总页面数
        float _lineheight;//行高
        int _pagelines;//每页行数
        int _curlines = 0;//当前行号(this.Editor)
        bool _isAll = true;//是否已经完全显示
        float _lastlineheight;//上次已打印高度
        Bitmap _backImage;//背景图片
        Bitmap _reallyback;
        public Bitmap ReallyBack
        {
            set
            {
                this._reallyback = value;
            }
        }
        private void printDoc_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            SizeF sf;
            RectangleF rec;
            int num = 0, j;
            float curY = e.MarginBounds.Top+5.0f;//修正值
            //计算出行高,总行数,总页数.
            if (_totalpages == 0)
            {
                _lineheight = this.Editor.Font.GetHeight(e.Graphics);
                _pagelines = (int)(e.MarginBounds.Height / _lineheight);
                for (int i = 0; i < this.Editor.Lines.Length; i++)
                {
                    sf = e.Graphics.MeasureString(this.Editor.Lines[i], this.Editor.Font, e.MarginBounds.Width);
                    _totallines += (int)(sf.Height / _lineheight);
                }
                _totalpages = _totallines / _pagelines + (_totallines % _pagelines == 0 ? 0 : 1);
            }

            //显示内容
            e.Graphics.SetClip(new RectangleF(e.PageBounds.Left,e.MarginBounds.Top,e.PageBounds.Width,e.PageBounds.Height-(e.PageBounds.Height-e.MarginBounds.Height)/2));
            for (j = _curlines; j < this.Editor.Lines.Length; j++)
            {
                if (curY + _lineheight > e.MarginBounds.Height + e.MarginBounds.Top)
                    break;
                //对上一页面进行判断,看上一面是否已经完全显示
                //没有
                if (_isAll == false)
                {

                    sf = e.Graphics.MeasureString(this.Editor.Lines[j], this.Editor.Font, e.MarginBounds.Width);
                    rec = new RectangleF(new PointF(e.MarginBounds.Left, e.MarginBounds.Top - _lastlineheight + 5.0f), sf);//0.5f修正值
                    num += (int)((rec.Height - _lastlineheight) / _lineheight);
                    curY += rec.Height - _lastlineheight;
                    _isAll = true;
                    _lastlineheight = 0;
                    //e.Graphics.SetClip(e.PageBounds);
                }
                //已经完全显示
                else
                {
                    sf = e.Graphics.MeasureString(this.Editor.Lines[j], this.Editor.Font, e.MarginBounds.Width);
                    num += (int)(sf.Height / _lineheight);
                    //curY += sf.Height;
                    //当当前页不能完全显示第j行时
                    if (curY + sf.Height > e.MarginBounds.Height + e.MarginBounds.Top)
                    {
                        rec = new RectangleF(e.MarginBounds.Left, curY, sf.Width, sf.Height - (num - _pagelines) * _lineheight);
                        this._isAll = false;
                        _lastlineheight = sf.Height - (num - _pagelines) * _lineheight;
                        curY += _lastlineheight;
                    }
                    //能完全显示.
                    else
                    {
                        rec = new RectangleF(e.MarginBounds.Left, curY, sf.Width, sf.Height);
                        curY += rec.Height;

                    }
                }
                e.Graphics.DrawString(this.Editor.Lines[j], this.Editor.Font, new SolidBrush(this.Editor.ForeColor), rec);

            }
            e.Graphics.SetClip(e.PageBounds);
            _curlines = j;

            if (num > _pagelines)//当未完全显示此行时,在打印下一页是还是停留在该行.
                _curlines -= 1;

            if (_curlines < this.Editor.Lines.Length)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;

            e.Graphics.DrawImage(_backImage, 0, 0);
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Red)), e.MarginBounds);
            e.Graphics.DrawString(this.Editor.Font.Size.ToString(), this.Editor.Font, new SolidBrush(Color.Blue), 0, 0);

        }

        private void menuPrintPriview_Click(object sender, EventArgs e)
        {
            //Bitmap bmp = (Bitmap)Bitmap.FromFile(Application.StartupPath + "\\swpaflag.jpg");
            _backImage = new Bitmap(_reallyback.Width, _reallyback.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            for (int i = 0; i < _reallyback.Width; i++)
                for (int j = 0; j < _reallyback.Height; j++)
                    _backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));

                    //
            (this.printPDialog as Form).WindowState = FormWindowState.Maximized;
            this.printPDialog.ShowDialog();
                    _totalpages = 0;
            _isAll=true;
            _curlines = 0;
        }

        private void menuPrintSetup_Click(object sender, EventArgs e)
        {
            this.pageSetDialog.ShowDialog();
        }

#8


引用 5 楼 wuyq11 的回复:
在winform中可使用printdocument打印某页
printdocument.PrinterSettings.PaperSizes 
PrintDocument.PrinterSettings.FromPage = 2; 
PrintDocument.PrinterSettings.ToPage = 2;

dui

#9


 this.Editor是什么?

#10


帮顶                        d

#11


backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));
这句话是什么意思

#12


引用 11 楼 markfeier 的回复:
backImage.SetPixel(i, j, Color.FromArgb(100, _reallyback.GetPixel(i, j)));
这句话是什么意思

是设置图片中的指定的像素(i,j)的颜色