首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.net 2.0 PrintDocument问题

.net 2.0 PrintDocument问题
EN

Stack Overflow用户
提问于 2010-12-05 17:11:34
回答 1查看 541关注 0票数 1

我正在尝试通过虚拟PDF打印机驱动程序(FoxitPDF Writer、doPDF、NovaPDF等)打印一些页面。

它很少打印。大多数情况下,页面是空白的。当我试图从MS Word中打印文档时,情况并非如此。

为什么?我应该寻找什么来解决这个问题?

这是我的代码:

代码语言:javascript
复制
void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            if (_studentsList != null)
            {
                Graphics g = e.Graphics;
                PointF point = new PointF(0, yStudentTopMargin);

                int count = 2;

                while (true)
                {
                    if ((count == 0) || (_listItemCount > _studentsList.Count - 1))
                    {
                        break;
                    }

                    StudentDrawer.DrawStudent(g, point, _studentsList[_listItemCount], xStudentBoxDistance, yStudentBoxDistance, xLineDistance, yLineDistance);

                    point.Y += (yStudentBoxDistance * 10);

                    count--;
                    _listItemCount++;
                }

                if (_listItemCount < _studentsList.Count)
                {
                    e.HasMorePages = true;
                }
                else
                {
                    e.HasMorePages = false;
                }
            }
            else
            {
                return;
            }
        }

public static void DrawStudent(Graphics g, PointF point, Student std, int xBoxDistance, int yBoxDistance, int xLineDistance, int yLineDistance)
        {
            SolidBrush brush = new SolidBrush(Color.Black);
            Font boldFont = new Font("Times New Roman", yLineDistance * 2 / 3, FontStyle.Bold);
            Font normalFont = new Font("Arial", yLineDistance * 2/3);
            Pen pen = new Pen(brush);

            const int fontSize = 10;

            float x = point.X;
            float y = point.Y;

            float leftMargin = xBoxDistance;//175;
            //const float rightMargin = 100;
            float topMargin = yBoxDistance;//60;
            //const float bottomMargin = 100;

            StringDrawer.DrawRoll(g, "Roll No. :", std.RollNo.ToString(), fontSize + 2, x + leftMargin, y + topMargin , xLineDistance, yLineDistance);
            StringDrawer.DrawHeading(g, "SUBJECT", "MARKS", "L. GRADE", "GP", fontSize, x + leftMargin, y + topMargin + (1 * yLineDistance), xLineDistance, yLineDistance);
                ......   ... ... ...
        ......   ... ... ...
            StringDrawer.DrawSubject(g, "Total", std.Bangla1stPaper_Marks.ToString(), std.Bangla1stPaper_GradeLetter, std.Bangla1stPaper_GradePoint.ToString(), fontSize, x + leftMargin, y + topMargin + (15 * yLineDistance), xLineDistance, yLineDistance);
        }

public static void DrawHeading(Graphics g, string subject, string marks, string letterGrade, string gp, int fontSize, float x, float y, int xDistance, int yDistance)
        {
            SolidBrush brush = new SolidBrush(Color.Black);
            Font boldFont = new Font("Times New Roman", fontSize, FontStyle.Bold);
            Pen pen = new Pen(brush);

            g.DrawRectangle(pen, x, y, xDistance, fontSize + 8);
            g.DrawString(subject, boldFont, brush, x + (0 * xDistance * 1.2f), y);

            g.DrawRectangle(pen, x + (1 * xDistance), y, xDistance, fontSize + 8);
            g.DrawString(marks, boldFont, brush, x + (1 * xDistance * 1.3f), y);

            g.DrawRectangle(pen, x + (2 * xDistance), y, xDistance, fontSize + 8);
            g.DrawString(letterGrade, boldFont, brush, x + (2 * xDistance * 1.1f), y);

            g.DrawRectangle(pen, x + (3 * xDistance), y, xDistance, fontSize + 8);
            g.DrawString(gp, boldFont, brush, x + (3 * xDistance * 1.14f), y);
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2010-12-05 23:02:08

代码看起来没问题。但是,缺少BeginPrint事件处理程序。需要将_listItemCount变量设置回零。如果没有它,则只有第一次打印输出才有效,因为_listItemCount已经超过_studentsList.Count,所以任何后续的打印尝试都只会产生一个空页

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4358108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档