home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / kompon / d3456 / POWERPDF.ZIP / PowerPdf / Example / ConsoleProgramDemo / BatchPdf.dpr < prev    next >
Text File  |  2001-09-15  |  5KB  |  242 lines

  1. {*
  2.  *
  3.  *  BatchPdf.dpr
  4.  *
  5.  *  To run this program, open command-prompt window and run this program with
  6.  *  filename parameter
  7.  *
  8.  *  ex) BatchPdf.exe batchpdf.pdf
  9.  *}
  10.  
  11. program BatchPdf;
  12. {$APPTYPE CONSOLE}
  13. uses
  14.   SysUtils,
  15.   Classes,
  16.   PdfDoc,
  17.   PdfTypes,
  18.   PdfFonts,
  19.   DB,
  20.   DBTables;
  21.  
  22. var
  23.   FDoc: TPdfDoc;
  24.   FFileName: string;
  25.   FOutFile: TFileStream;
  26.   FPage: integer;
  27.   FQuery: TQuery;
  28.  
  29.   procedure TextOut(X, Y: Single; S: string);
  30.   begin
  31.     with FDoc.Canvas do
  32.     begin
  33.       BeginText;
  34.       MoveTextPoint(X, Y);
  35.       ShowText(S);
  36.       EndText;
  37.     end;
  38.   end;
  39.  
  40.   procedure DrawLine(X1, Y1, X2, Y2, Width: Single);
  41.   begin
  42.     with FDoc.Canvas do
  43.     begin
  44.       MoveTo(X1, Y1);
  45.       LineTo(X2, Y2);
  46.       Stroke;
  47.     end;
  48.   end;
  49.  
  50.   procedure WriteHeader;
  51.   var
  52.     s: string;
  53.     w: integer;
  54.   begin
  55.     // writing the headline of the pages
  56.     with FDoc.Canvas do
  57.     begin
  58.       // setting font
  59.       SetFont('Arial-BoldItalic', 16);
  60.  
  61.       // printing text.
  62.       TextOut(90, 770, 'Customer.DB');
  63.  
  64.       SetFont('Arial-BoldItalic', 9);
  65.       S := FormatDateTime('YYYY/MM/DD', Date);
  66.       w := Round(TextWidth(S));
  67.  
  68.       // writing header text.
  69.       TextOut(530 - w, 770, S);
  70.  
  71.       SetRGBStrokeColor($00008800);
  72.       DrawLine(90, 765, 530, 765, 1.5);
  73.     end;
  74.   end;
  75.  
  76.   procedure WriteFooter;
  77.   var
  78.     w: Single;
  79.     s: string;
  80.   begin
  81.     with FDoc.Canvas do
  82.     begin
  83.       // Setting font and print text with center align
  84.       SetFont('Times-Roman', 8);
  85.  
  86.       DrawLine(90, 70, 530, 70, 1.5);
  87.  
  88.       s := 'Page ' + IntToStr(FPage);
  89.       w := TextWidth(s);
  90.  
  91.       TextOut((PageWidth - w) / 2, 55, S);
  92.     end;
  93.   end;
  94.  
  95.   procedure WriteRow(YPos: Single);
  96.   var
  97.     i: integer;
  98.     s: string;
  99.   begin
  100.     // printing the detail lines
  101.     with FDoc.Canvas do
  102.     begin
  103.       if not FQuery.Eof then
  104.       begin
  105.         TextOut(95, YPos - 15, FQuery.FieldByName('CustNo').AsString);
  106.  
  107.         s := FQuery.FieldByName('Company').AsString;
  108.  
  109.         // calculate the number of the charactor which can be contained in the
  110.         // width of the frame.
  111.         i := MeasureText(s, 130);
  112.         TextOut(135, YPos - 15, Copy(s, 1, i));
  113.  
  114.         s := FQuery.FieldByName('State').AsString +
  115.              FQuery.FieldByName('City').AsString +
  116.              FQuery.FieldByName('Addr1').AsString;
  117.  
  118.         i := MeasureText(s, 175);
  119.         TextOut(275, YPos - 15, Copy(s, 1, i));
  120.  
  121.         TextOut(455, YPos - 15, FQuery.FieldByName('Phone').AsString);
  122.  
  123.         FQuery.Next;
  124.       end;
  125.     end;
  126.   end;
  127.  
  128.   procedure WritePage;
  129.   var
  130.     i: integer;
  131.     XPos, YPos: Single;
  132.   begin
  133.     with FDoc.Canvas do
  134.     begin
  135.       // writing the outline
  136.       SetLineWidth(1.5);
  137.       Rectangle(90, 80, 440, 680);
  138.       Stroke;
  139.  
  140.       // writing the horizontal lines.
  141.       YPos := 760;
  142.       SetLineWidth(0.75);
  143.       for i := 0 to 32 do
  144.       begin
  145.         YPos := YPos - 20;
  146.         MoveTo(90, YPos);
  147.         LineTo(530, YPos);
  148.         Stroke;
  149.       end;
  150.  
  151.       // writing the header of the text.
  152.       SetLineWidth(1);
  153.       SetFont('Times-Roman', 10.5);
  154.  
  155.       XPos := 90;
  156.       TextOut(XPos + 5, 745, 'NO.');
  157.  
  158.       XPos := 130;
  159.       DrawLine(XPos, 760, XPos, 80, 1);
  160.       TextOut(XPos + 5, 745, 'Company');
  161.  
  162.       XPos := 270;
  163.       DrawLine(XPos, 760, XPos, 80, 1);
  164.       TextOut(XPos + 5, 745, 'Address');
  165.  
  166.       XPos := 450;
  167.       DrawLine(XPos, 760, XPos, 80, 1);
  168.       TextOut(XPos + 5, 745, 'Phone');
  169.  
  170.       XPos := 530;
  171.       DrawLine(XPos, 760, XPos, 80, 1);
  172.  
  173.       // setting the font for the detail lines.
  174.       SetFont('Arial', 10.5);
  175.     end;
  176.  
  177.     // printing the detail lines with 20 dot height.
  178.     YPos := 740;
  179.     for i := 0 to 32 do
  180.     begin
  181.       WriteRow(YPos);
  182.       YPos := YPos - 20;
  183.     end;
  184.   end;
  185.  
  186. begin
  187.   if ParamCount > 0 then
  188.     FFileName := ParamStr(1)
  189.   else
  190.   begin
  191.     Writeln('Usage: bachpdf <pdf-filename>');
  192.     Halt(2);
  193.   end;
  194.  
  195.   // create output-filestream.
  196.   FOutFile := TFileStream.Create(FFileName, fmCreate);
  197.  
  198.   FPage := 1;
  199.  
  200.   // create TQuery object and set properties.
  201.   FQuery := TQuery.Create(nil);
  202.   with FQuery do
  203.   try
  204.     Writeln('BatchPdf opening database..');
  205.     DatabaseName := 'DBDEMOS';
  206.     SQL.Text := 'SELECT CustNo, Company, State, City, Addr1, Phone from Customer';
  207.     Open;
  208.  
  209.     // create TPdfDoc object.
  210.     Writeln('BatchPdf creating document..');
  211.     FDoc := TPdfDoc.Create;
  212.     with FDoc do
  213.     try
  214.       // create a new document.
  215.       NewDoc;
  216.  
  217.       // printind page from the result of the query.
  218.       while not FQuery.Eof do
  219.       begin
  220.         AddPage;
  221.         WriteHeader;
  222.         WritePage;
  223.         WriteFooter;
  224.         inc(FPage);
  225.       end;
  226.  
  227.       // save the pdf-document to the file-stream.
  228.       Writeln('BatchPdf saving document..');
  229.       FDoc.SaveToStream(FOutFile);
  230.     finally
  231.       FDoc.Free;
  232.     end;
  233.  
  234.     Close;
  235.     Writeln('BatchPdf end..');
  236.   finally
  237.     Free;
  238.     FOutFile.Free;
  239.   end;
  240.  
  241. end.
  242.