First of all I already asked a question with this same code but the question was different. Anyway, I have this code for printing files (it's a class derived from the PrintDocument
class of C#
) it receives a parameter that is the name of the file that contains the data to be printed and prints.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
namespace WindowsApplication1
{
class CPrint : PrintDocument
{
private Font printFont;
private TextReader printStream;
private string fileToPrint;
private Bitmap imgWatermark;
public bool Watermark = false;
public CPrint()
{
imgWatermark = null;
}
public CPrint(string fileName) : this()
{
this.FileToPrint = fileName;
}
public string FileToPrint
{
get
{
return fileToPrint;
}
set
{
if (File.Exists(value))
{
CPrint p = new CPrint();
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("C39HrP36DlTt", 26);
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 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;
}
}
}
What I want to do is print a file and have this print out the provisions of the Pimaco 6287 label sheet (the sheet contains four columns each with 20 lines totaling 80 labels per sheet) and that's how I want the data this read file is printed. I have never played with GDI
, in fact I have been playing with programming for a very short time and can say that I am the real beginner.
What I observed in this code are the values of margins, lines per page, etc. The parameters sent when drawing the printout. What happens is that it draws only one column and I wanted to be able to place 4 columns of 20 rows each, like the layout of the label sheet.
Could anyone help me with this? If you can do it in detail so I can learn it is a good idea, too. Thank you in advance!