Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

last line of the page not to cut into halves

Status
Not open for further replies.

Ngai88

Programmer
Sep 8, 2004
56
US
Hello,
Every time I print the page, I noticed the last line of the page is always cut into halves literally, leaving the top half on the 1st page and the 2nd half at the top of the next page. I tried to adjust the linebreaks but have not helped. Can anyone tell me how to go about resolving this? Is there some calculations involve with the margins so that it knows that there is not enough room to print the last line and so move the whole line to the next page instead? Thanks for any help.

Ngai
 
Hello,
Presently when the print setup dialogue box pops up, the Left, Right, Top and Bottom all come with a default margin of 1" . I would like to change the Bottom margin to 0.82'. I looked up the ..........

//private const double anInch = 14.4;

So...I calculated to make it 0.82 inches, it is equal to 11.808...

private const double lessAnInch = 11.808;

yet when I go to compile and run it, the setups are still showing 1" for all. What else am I not doing right here?

Thanks,
Ngai
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



using System;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;

namespace PrintingDemo
{
public class TextPrintDocument : PrintDocument
{
private Font printFont;
private TextReader printStream;
private string fileToPrint;
//private Bitmap imgWatermark;
//private const double anInch = 14.4;

private const double lessAnInch = 11.808;

public bool Watermark = false;

public TextPrintDocument()
{
//imgWatermark = new Bitmap(GetType(), "Watermark.gif");
}

public TextPrintDocument(string fileName) : this()
{
this.FileToPrint = fileName;
}

public string FileToPrint
{
get
{
return fileToPrint;
}
set
{
if (File.Exists(value))
{
fileToPrint = value;
this.DocumentName = value;
}
else
throw(new Exception("File not found."));
}
}

public Font Font
{
get { return printFont; }
set { printFont = value; }
}

protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
printFont = new Font("Verdana", 10);
printStream = new StreamReader(fileToPrint);
}

protected override void OnEndPrint(PrintEventArgs e)
{
base.OnEndPrint(e);
printFont.Dispose();
printStream.Close();
}

protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);

// Slow down printing for demo.
System.Threading.Thread.Sleep(200);

Graphics gdiPage = e.Graphics;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
//float botMargin = e.MarginBounds.Bottom;
//float botMar = .82f;

float botMargin = (int)((e.MarginBounds.Bottom) * lessAnInch);
float lineHeight = printFont.GetHeight(gdiPage);
float linesPerPage = e.MarginBounds.Height / lineHeight;
int lineCount = 0;
string lineText = null;

// Watermark?
// if (this.Watermark)
// {
// int top = Math.Max(0, (e.PageBounds.Height - imgWatermark.Height) / 2);
// int left = Math.Max(0, (e.PageBounds.Width - imgWatermark.Width) / 2);
// gdiPage.DrawImage(imgWatermark, left, top, imgWatermark.Width, imgWatermark.Height);
// }

// Print each line of the file.
while (lineCount < linesPerPage &&
((lineText = printStream.ReadLine()) != null))
{
gdiPage.DrawString(lineText, printFont, Brushes.Black,
leftMargin, (topMargin + (lineCount++ * lineHeight)));
}

// If more lines exist, print another page.
if(lineText != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
}
}





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top