Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Could anybody help solve this problem?

enter image description here

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

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.

Where is my mistake?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
165 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.

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.

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.

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).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...