打印多页边距未设置 [英] Printing Multiple pages margin not set

查看:34
本文介绍了打印多页边距未设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮忙解决这个问题吗?

Could anybody help solve this problem?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace notepad_demo
{
    public partial class Form1 : Form
    {
        private StringReader myReader;

        public Form1()
        {
            InitializeComponent();
        }
        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {

            printDialog1.Document = printDocument1;
            string strText = this.richTextBox1.Text;
            myReader = new StringReader(strText);
            if (printDialog1.ShowDialog() == DialogResult.OK)
            {

                printDocument1.Print();
            }
        }

        private void printPrieviewToolStripMenuItem_Click(object sender, EventArgs e)
        {

            string strText = this.richTextBox1.Text;//read text for richtextbox
            myReader = new StringReader(strText);
            printPreviewDialog1.Document = printDocument1;
            printPreviewDialog1.ShowDialog();


        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {

            string strDisplay = "Header";
            System.Drawing.Font fntString = new Font("Times New Roman", 28, FontStyle.Bold);
            e.Graphics.DrawString(strDisplay, fntString, Brushes.Black, 100, 100);
            string strDisplay1 = "Company name";
            System.Drawing.Font fntString1 = new Font("Times New Roman", 28, FontStyle.Bold);
            e.Graphics.DrawString(strDisplay1, fntString1, Brushes.Black, 100, 150);

            float linesPerPage = 0;
            float yPosition = 590;
            int count = 0;
            float leftMargin = 70;
            float topMargin = 590;
            string line = null;
            Font printFont = new System.Drawing.Font("Times New Roman", 8, FontStyle.Regular);
            SolidBrush myBrush = new SolidBrush(Color.Black);               
            linesPerPage = e.MarginBounds.Height / printFont.GetHeight(e.Graphics);
            while (count < linesPerPage && ((line = myReader.ReadLine()) != null))
            {
                yPosition = topMargin + (count * printFont.GetHeight(e.Graphics));
                e.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());
                count++;
            }

            if (line != null)
            {
                e.HasMorePages = true;

            }
            else
            {
                e.HasMorePages = false;

            }
            myBrush.Dispose();
        }
    }    
}

在附图中,第一页没问题,但第二、第三和第四页的开始也与第一页相同.我只想在第一页上显示标题和公司名称,而在第二页上边距处打印RichTextBox.text".

In the attached image, the first page is ok but the 2nd, 3rd and 4th pages are also beginning the same as per the first page. I want the header and company name only on the first page and the `RichTextBox.text`` is printed on the second page, at top margin.

我的错误在哪里?

推荐答案

要解决每页打印页眉的问题,请按照 Hans Passant 在评论部分给您的建议进行操作,关于您在此处提出的问题.

To resolve the problem of your headers printing on each page, follow the advice given to you by Hans Passant in the comments section, on your question asked here.

简而言之,它正在创建一个名为 pageCount 的类变量,在您开始打印时将其设置为 0,并在每打印一页时将其递增.
将页眉部分写入页面的部分应封装在条件语句中,当您在第一页以外的任何内容上打印时,该语句不会执行.

In short it is creating a class variable called pageCount, setting that to 0 when you begin printing and incrementing it with each page printed.
The section that is writing your header section to your pages, should then be encapsulated within a conditional statement that will not be executed when you are printing on anything other than the first page.

为了解决第二个问题,即使在第二页上,您的文本也被打印到页面下方很远的地方,这是因为您将 topMargin 变量在 PagePrint 中始终初始化为 590 事件.

To address the second issue of your text being printed very far down the page even when on the second page, that is because you are initializing your topMargin variable always to 590 inside the PagePrint event.

您应该做的是将其初始化到页面顶部(打印标题的位置),然后根据您当前正在打印的页面进行调整.
例如.对于第一页,从上边距打印标题,并在打印不同的标题和标签时增加 topMargin 变量.然后,当您到达第二页时,您只需将 topMargin 变量增加为您正在打印的每个标签/行的高度(就像您当前在循环中所做的一样).

What you should be doing is initializing it to the top of the page (where the headers are printed) and then adjusting it according to what page you are currently printing.
Eg. for the first page print the headers from the top margin and incrementing the topMargin variable as you print the different headers and labels. Then when you reach your second page, you just increment your topMargin variable with the height of each label/line that you are printing (as what you are currently doing inside your loop).

您已经对变量 yPosition 有了这个想法,您只需要为您正在打印的所有东西完全实现它,而不仅仅是标签.

You already have the idea of this with your variable yPosition, you just need to fully implement it for all things that you are printing, and not just the labels.

这篇关于打印多页边距未设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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