Printing

You may need to produce reports as part of your coursework. This provides you with the opportunity for some programming to format text and to position headings and data precisely. The following program works in Delphi 7. If you have problems printing your output from Pascal, we suggest that you save the data to a csv (comma separated values) file or to an rtf (rich text format) file. You should load the csv file into a spreadsheet or the rtf file into a word processor, then print the document. We provide the demonstration programs SaveCSV and SaveRTF after program PrintRecordArray. See the following page for output to a Word Document from Lazarus.

program PrintRecordArray;
  {$APPTYPE CONSOLE}
uses
  SysUtils, Printers, Graphics;
type
  TStudent = record
    Forename, Surname : string[15];
    TheoryMark, PracticalMark : integer;
  end;
const
  MAX_RECS = 3;
  TAB1 = 1000;
  TAB2 = 2000;
  TAB3 = 3000;
  LEFT_MARGIN = 200;
  TOP_MARGIN = 200;
  LINE_SPACING = 150;
var
  Students : array[1 .. MAX_RECS] of TStudent;
  Count, Xpos, Ypos : integer;
begin
  //Hard code records for convenience.
  with Students[1] do
    begin
      Forename := 'Jo';
      Surname := 'Wood';
      TheoryMark := 55;
      PracticalMark := 66;
    end;
  with Students[2] do
    begin
      Forename := 'John';
      Surname := 'Bode';
      TheoryMark := 73;
      PracticalMark := 58;
    end;
  with Students[3] do
    begin
      Forename := 'Kapil';
      Surname := 'Shah';
      TheoryMark := 59;
      PracticalMark := 58;
    end;
  Printer.BeginDoc;
  //Format text for headings
  Printer.Canvas.Font.Size:= 12;
  Printer.Canvas.Font.Name:= 'Arial';
  Printer.Canvas.Font.Style := [fsBold];
  XPos := LEFT_MARGIN;
  YPos := TOP_MARGIN;
  //Print column headings
  Printer.Canvas.TextOut(Xpos, Ypos,'Forename');
  Printer.Canvas.TextOut(Xpos + TAB1, Ypos, 'Surname');
  Printer.Canvas.TextOut(Xpos + TAB2, Ypos, 'Theory Mark');
  Printer.Canvas.TextOut(Xpos + TAB3, Ypos, 'Practical Mark');
  //Format text for record data
  Printer.Canvas.Font.Style := [];
  Printer.Canvas.Font.Name:= 'Times New Roman';
  //Print records
  for Count:= 1 to MAX_RECS do
    begin
      Ypos := Ypos + LINE_SPACING;
      Printer.Canvas.TextOut(Xpos, Ypos, Students[Count].Forename);
      Printer.Canvas.TextOut(Xpos + TAB1, Ypos, Students[Count].Surname);
      Printer.Canvas.TextOut(Xpos + TAB2, Ypos, IntToStr(Students[Count].TheoryMark));
      Printer.Canvas.TextOut(Xpos + TAB3, Ypos, IntToStr(Students[Count].PracticalMark));
    end;
  Printer.EndDoc;
end.

You can also print shapes with code such as Printer.Canvas.Rectangle(X_TOP_LEFT, Y_TOP_LEFT, X_BOTTOM_RIGHT, Y_BOTTOM_RIGHT); where the four arguments are declared constants, or use variables for arguments and write code to calculate suitable values.

program SaveCSV;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
type
  TStudent = record
    Forename, Surname : string[15];
    TheoryMark, PracticalMark : integer;
  end;
const
  MAX = 3;
var
  Students : array[1 .. MAX] of TStudent;
  Count : integer;
  StudentFile : TextFile;
begin
  //Hard code records for convenience.
  with Students[1] do
    begin
      Forename := 'Jo';
      Surname := 'Wood';
      TheoryMark := 55;
      PracticalMark := 66;
    end;
  with Students[2] do
    begin
      Forename := 'John';
      Surname := 'Bode';
      TheoryMark := 73;
      PracticalMark := 58;
    end;
  with Students[3] do
    begin
      Forename := 'Kapil';
      Surname := 'Shah';
      TheoryMark := 59;
      PracticalMark := 58;
    end;
  assignFile(StudentFile, 'marks.csv');
  rewrite(StudentFile);
  writeln('Writing records to marks.csv');
  //Write column headings to csv file
  write(StudentFile, 'Forename,');
  write(StudentFile, 'Surname,');
  write(StudentFile, 'Theory Mark,');
  write(StudentFile, 'Practical Mark',#13#10);
  //Write records
  for Count := 1 to MAX do
    begin
      write(StudentFile, Students[Count].Forename, ',');
      write(StudentFile, Students[Count].Surname, ',');
      write(StudentFile, Students[Count].TheoryMark, ',');
      write(StudentFile, Students[Count].PracticalMark,#13#10);
    end;
  closeFile(StudentFile);
  readln;
end.

The web page that we used in writing the next program should help you to format your output to match your own report design.

program SaveRTF;
  {$APPTYPE CONSOLE}
uses
  SysUtils;
type
  TStudent = record
    Forename, Surname : string[15];
    TheoryMark, PracticalMark : integer;
  end;
const
  MAX = 3;
var
  Students : array[1 .. MAX] of TStudent;
  Count : integer;
  StudentFile : TextFile;
begin
  //Hard code records for convenience.
  with Students[1] do
    begin
      Forename := 'Jo';
      Surname := 'Wood';
      TheoryMark := 55;
      PracticalMark := 66;
    end;
  with Students[2] do
    begin
      Forename := 'John';
      Surname := 'Bode';
      TheoryMark := 73;
      PracticalMark := 58;
    end;
  with Students[3] do
    begin
      Forename := 'Kapil';
      Surname := 'Shah';
      TheoryMark := 59;
      PracticalMark := 58;
    end;
  assignFile(StudentFile, 'marks.rtf');
  rewrite(StudentFile);
  writeln('Writing records to marks.rtf');
  //Write document type and font table containing only Arial.
  writeln(StudentFile, '{\rtf1\ansi\deff0 {\fonttbl {\f0 Arial;}}');
  //Write tab positions
  writeln(StudentFile, '\tx1800\tx3600\tx5400');
  //Write column headings to csv file
  //Set font size to 12 with fs24 (double what you might expect) then turn on bold. 
  write(StudentFile, '\fs24\b ');
  //Write headings and tabs
  write(StudentFile, 'Forename\tab ');
  write(StudentFile, 'Surname\tab ');
  write(StudentFile, 'Theory Mark\tab ');
  write(StudentFile, 'Practical Mark');
  //Turn off bold and end line.
  writeln(StudentFile, '\b0\line');
  //Enter data
  for Count := 1 to MAX do
    begin
      write(StudentFile, Students[Count].Forename, '\tab ');
      write(StudentFile, Students[Count].Surname, '\tab ');
      write(StudentFile, Students[Count].TheoryMark, '\tab ');
      writeln(StudentFile, Students[Count].PracticalMark, '\line');
    end;
  closeFile(StudentFile);
  readln;
end.
Programming - a skill for life!

Input from keyboard and file and output to monitor, file and printer