#1 2023-04-26 11:27:04

nnmnnm
Member
Registered: 2023-04-26
Posts: 2

SynPdf - Select active page / canvas

Hello,

I'm using SynPdf and after adding a few pages and drawing some objects / text on each page canvas I would like to return to page number 1 for example and write some additional text (like page number / total pages). I cannot find any example how to do that. I'm doing something like that:

var
  pdf: TPdfDocumentGDI;
  page: TPdfPage;
begin
  pdf := TPdfDocumentGDI.Create;

  // page 1
  page := pdf.AddPage;

  pdf.VCLCanvas.Font.Name := 'Times New Roman';
  pdf.VCLCanvas.Font.Size := 24;
  pdf.VCLCanvas.TextOut(100, 100, 'some text on page 1');

  // page 2
  page := pdf.AddPage;

  pdf.VCLCanvas.Font.Name := 'Times New Roman';
  pdf.VCLCanvas.Font.Size := 24;
  pdf.VCLCanvas.TextOut(100, 100, 'some text on page 2');

  // write some text back to page 1
  ...
end;

I have tried something like this but it clears the entire page contents and obviously that's not the correct way to do it:

  ...
  //
  page := pdf.RawPages[0]; // select page 1

  //
  pdf.Canvas.SetPage(page); // set active canvas to page 1
   ...

Any help would be appreciated.

Offline

#2 2023-04-27 08:39:00

rvk
Member
Registered: 2022-04-14
Posts: 87

Re: SynPdf - Select active page / canvas

I don't think that's (directly) possible with TPdfDocumentGDI.
You would think that it should work with TPdfDocumentGDI.RawPages but in TPdfDocumentGDI.AddPage there is this line at the top:

if (FCanvas<>nil) and (FCanvas.FPage<>nil) then
  TPdfPageGdi(FCanvas.FPage).FlushVCLCanvas;

That line flushes and frees TPdfPage.fVCLCurrentCanvas of the previous page.
That's the pdf.VCLCanvas you want to use for the page.
When going back to page one with TPdfDocumentGDI.RawPages the needed TPdfPage.VCLCanvas is already freed and destroyed so when accessing it again, it will be created again, new but empty.
That's why the page is overwritten.

You could try to comment out the above line. Then you get the result you want.
The TPdfPage.fVCLCurrentCanvas is kept after TPdfDocumentGDI.AddPage.
The TPdfPage.VCLCanvas is flushed in that case at TPdfDocumentGDI.SaveToStream() and not at AddPage.

But I'm not sure if there are any problems with it down the road.

Offline

#3 2023-04-27 09:58:36

nnmnnm
Member
Registered: 2023-04-26
Posts: 2

Re: SynPdf - Select active page / canvas

Thank you for the answer, rvk! I have already checked some parts of the source code but since I believed there is a way to do that and I'm missing it, I preferred not to mess with it. If there are no other possibilities I guess I'll have to try and modify some parts according to my needs. Thank you once again!

Edit: Just tried commenting the aforementioned lines and at first look it works using RawPages and SetPage.

Last edited by nnmnnm (2023-04-27 12:30:41)

Offline

Board footer

Powered by FluxBB