This HotPDF Component sample demonstrates how to create vector-style PDF drawings with the same TCanvas workflow Delphi developers already use in VCL applications. Instead of treating PDF output as a separate drawing model, the example writes text and shapes through HotPDF.Canvas and then saves the result as a PDF document.
The sample is useful when an application already has charting, diagram, preview, or custom control rendering code based on TCanvas. The core pattern is simple: create THotPDF, assign the output file, call BeginDoc, configure the canvas state, draw the content, and finish the file with EndDoc.
In the code below, the drawing loop renders three groups of colored shapes. Each group uses the same coordinate system and changes only the primitive being drawn: Rectangle, Ellipse, or RoundRect. This makes it a compact reference for testing canvas colors, fill behavior, text placement, and page layout in a generated PDF.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
program CanvasDraw; {$APPTYPE CONSOLE} uses {$IFDEF VER230} System.SysUtils, System.Classes, Vcl.Graphics, {$ELSE} SysUtils, Classes, Graphics, {$ENDIF} HPDFDoc; var I, Y: Integer; HotPDF: THotPDF; begin HotPDF:= THotPDF.Create(nil); try Randomize; HotPDF.AutoLaunch := true; HotPDF.FileName := 'CanvasDraw.pdf'; HotPDF.BeginDoc; // Create PDF file HotPDF.Canvas.Font.Size := 14; // Set canvas font size I :=20; Y := 60; HotPDF.Canvas.TextOut(10, 30, 'Canvas Rectangles'); // Print text while I <= 700 do begin // Draw color HotPDF.Canvas.Brush.Color := random($FFFFFF); // rectangles HotPDF.Canvas.Rectangle(I, Y, I+90, Y + 160); Inc(I, 30); Inc( Y, 3 ); end; I :=20; Y := 350; HotPDF.Canvas.TextOut(10, 320, 'Canvas Ellipses'); // Print text while I <= 700 do // Draw color begin // ellipses HotPDF.Canvas.Brush.Color := random($FFFFFF); HotPDF.Canvas.Ellipse(I, Y, I+90, Y + 160); Inc(I, 30); Inc( Y, 3 ); end; I :=20; Y := 680; HotPDF.Canvas.TextOut(10, 650, 'Canvas RoundRects'); // Print text while I <= 700 do begin HotPDF.Canvas.Brush.Color := random($FFFFFF); // Draw color HotPDF.Canvas.RoundRect(I, Y, I+90, Y + 160, 20, 20); // roundrects Inc(I, 30); Inc( Y, 3 ); end; HotPDF.EndDoc; // Close PDF file finally HotPDF.Free; end; end. |
What the sample shows
- How to initialize THotPDF for a console-style Delphi PDF generation task.
- How to use HotPDF.Canvas for text and basic geometric drawing.
- How brush colors affect filled shapes in the generated PDF.
- How repeated coordinates can build layered visual output without bitmap screenshots.
Implementation notes
The PDF page is finalized only after EndDoc is called, so production code should keep the try/finally block around the component lifetime and avoid leaving a partially written file if drawing fails. For deterministic test output, replace Randomize and random colors with fixed color values.
Canvas-based PDF drawing works best for reports, overlays, diagrams, and generated forms where the output should stay sharp at any zoom level. If the source drawing depends on screen DPI or device-specific fonts, verify the final PDF on a clean system before shipping it.