#1 2011-02-06 05:22:44

HNRSoftware
Member
Registered: 2011-02-06
Posts: 1

Need some basic "Hello World" examples

I appear to be the only person on this forum that needs more documentation.  I took some example code on another thread and attempted to get it to run.  (Sorry that I don't use standard indent - I was taught this way on my mother's knee and I can't shake it.)  I'm using Delphi 6 and have been coding in Turbo Pascal/Delphi since the early 1990s.

I do get the "Hello World" text (at the lower right corner of the page??), but I can't find the right combination to get either the line or the rectangle - no crash, but no lines either.

I would also like a meaningful example of placing a TBitmap on a page.  I can imagine passing it through a TMetaFile object but the exact process is not obvious to me.

Function SynHelloWorldTest(ShowAfter : boolean = true; PDFFN : string = 'HelloWorld.pdf') : String;
var xPdf   : TPdfDocument;
    xPage  : TPdfPage;
     begin
     xPdf := TPdfDocument.Create;
     try
        xPdf.Info.Author        := 'Tester';
        xPdf.Info.CreationDate  := Now;
        xPdf.Info.Creator       := 'Tester';
        xPdf.DefaultPaperSize   := psA4;
        xPage := xPDF.AddPage;                    // forum implies that this is unnecessary, but gives access violation if omitted

        xPDF.Canvas.SetRGBFillColor(clRed);            // affects text color
        xPDF.Canvas.SetRGBStrokeColor(clGreen);    // why isn't this one the text color? -- still no lines or rects

        xPDF.Canvas.SetLineWidth(0.25);               // thought maybe the line was just too thin to see
        xPDF.Canvas.MoveTo(100,100); xPDF.Canvas.LineTo(500,500); // nothing shows!!??
        xPdf.Canvas.Rectangle(10,10,600,600);       // nothing shows!!??

        xPDF.Canvas.SetFont('Arial',12.0,[fsBold]);  // comment this out and we get and access violation
        //xPdf.Canvas.BeginText;                         // these don't appear to do anything
        xPdf.Canvas.TextOut(1,1,'Hello World.');     // (0,0) appears at bottom left of the page
        //xPdf.Canvas.EndText;                           // these don't appear to do anything

        xPdf.SaveToFile(PDFFN);
     finally
        xPdf.Free;
        end; // of try
     //result := 'SynHelloWorldTest ['+PDFFN+']  '+fileinfostr(PDFFN);           // verification
     //if fileexists(PDFFN) and (ShowAfter) then WINSHELLOpenFile(PDFFN);  // verification - invokes the default pdf viewer
     end;

Offline

#2 2011-02-07 07:51:34

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Need some basic "Hello World" examples

Using the TPdfCanvas directly is to be used only if you did read the official PDF reference document.
Because it does map the PDF content generation directly.

For instance, the coordinates system is inverted from the standard TCanvas property.
And you'll have to use the commands in a dedicated order.
Two examples:
- MoveTo+LineTo have to be followed by a Stroke method call, or you'll have nothing drawn on the pdf!
- TextOut() is a wrapped around BT Td ....Tj ET so is calling BeginText+ShowText+EndText
Bitmap generation is also a bit technical using the TPDFCanvas...

So to sum up: a TPDFCanvas is NOT a TCanvas, but a PDF canvas!

What I would recommend for "normal" usage is to use a TPdfDocumentGDI instance, and its associated VCLCanvas, which is a plain TCanvas method, and can be used as usual.
You can either create a TMetaFileCanvas then render it using TPdfCanvas.RenderMetaFile()

Take a look at our TGDIPages class, in the SQLite3Pages unit: it's a reporting engine with an optional preview component, which can be used to generate PDF files. It's used by our framework, but can be used without it.

If you really want to use TPdfCanvas directly, you'll have most of its methods used in the TPdfEnum class and the EnumEMFFunc function.
Use it as reference sample code.
There is all necessary reference documentation embedded in the SynPdf unit, as comments above each class and method.

Offline

Board footer

Powered by FluxBB