You are not logged in.
Pages: 1
Consider the following program:
program PdfTest;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.Classes,
Winapi.Windows,
Vcl.Graphics,
SynCommons in 'Synopse\SynCommons.pas',
SynLZ in 'Synopse\SynLZ.pas',
SynPdf in 'Synopse\SynPdf.pas';
var
Doc: TPdfDocument;
Page: TPdfPage;
Metafile: TMetafile;
Canvas: TMetafileCanvas;
begin
// create test metafile
Metafile := TMetafile.Create;
Metafile.SetSize(700, 700);
Canvas := TMetafileCanvas.Create(Metafile, GetDC(0));
Canvas.MoveTo(0, 0);
Canvas.LineTo(700, 700);
Canvas.MoveTo(0, 700);
Canvas.LineTo(700, 0);
Canvas.Free;
// create page in portrait orientation, and render metafile to it
Doc := TPdfDocument.Create;
Doc.DefaultPaperSize := psA4;
Page := Doc.AddPage;
Page.PageLandscape := False;
Doc.Canvas.RenderMetaFile(Metafile);
Doc.SaveToFile('_portrait.pdf');
// same again, but with landscape orientation
Doc := TPdfDocument.Create;
Doc.DefaultPaperSize := psA4;
Page := Doc.AddPage;
Page.PageLandscape := True;
Doc.Canvas.RenderMetaFile(Metafile);
Doc.SaveToFile('_landscape.pdf');
end.
This emits two PDF files that are meant to contain a simple cross. The portrait orientation version comes out correctly. The landscape orientation does not.
I tracked this down to the fact that TPdfCanvas.SetPage is called from AddPage. And this caches the height of the the page:
procedure TPdfCanvas.SetPage(APage: TPdfPage);
begin
FPage := APage;
FPageFontList := FPage.GetResources('Font');
FContents := TPdfStream(FPage.ValueByName('Contents'));
FHeight := FPage.GetPageHeight; <----- UH OH!
FFactor := 72/FDoc.FScreenLogPixels; // PDF expect 72 pixels per inch
end;
Then when we subsequently change orientation, the page height is changed, but FHeight is not updated.
I guess I can solve my problem by throwing in a call to TPdfCanvas.SetPage immediately before I call RenderMetafile. Is that what I am supposed to do? Is there a better way to solve my problem? Is the library behaving as designed? Do you mean to cache FPage.GetPageHeight and then not update it later?
Thanks!
Offline
No, this is a bug, not feature!
We have fixed mixed portrait/landscape page rendering within a same document - including associated regression tests.
See http://synopse.info/fossil/info/be15825916
Hope it works as expected now.
Offline
Thanks! I'll check this all out tomorrow.
Offline
Thanks Arnaud, that sorts the problem out and I can now remove my workaround.
Offline
Pages: 1