You are not logged in.
Pages: 1
I'm trying to take a screenshot to PDF and it *almost* works. Saving a screenshot to a metafile works great (example code from Uwe Raabe: https://gist.github.com/UweRaabe/92021a … a0da28fbd):
procedure TForm77.Button1Click(Sender: TObject);
var
cnv: TMetafileCanvas;
emf: TMetafile;
notused: HWND;
begin
emf := TMetafile.Create;
try
emf.SetSize(ClientWidth, ClientHeight);
cnv := TMetafileCanvas.Create(emf, GetDeviceContext(notused));
try
Self.PaintTo(cnv, 0, 0);
finally
cnv.Free;
end;
emf.SaveToFile('c:\temp\Test.emf');
finally
emf.Free;
end;
end;
When I try to do something similar but drawing to a PDF, I got different results:
procedure ScreenshotPDF(const Form: TForm);
var
Stream: TFileStream;
pdfObj: TPdfDocumentGDI;
begin
pdfObj := TPdfDocumentGDI.Create(false, 0, True, nil);
try
Stream := TFileStream.Create('c:\temp\Test.pdf', fmCreate);
try
pdfObj.SaveToStreamDirectBegin(Stream);
pdfObj.AddPage;
Form.PaintTo(pdfObj.VCLCanvas, 0, 0);
pdfObj.SaveToStreamDirectPageFlush;
pdfObj.SaveToStreamDirectEnd;
finally
Stream.Free;
end;
finally
pdfObj.Free;
end;
end;
A zip file with both the resulting EMF and PDF files can be downloaded from here: http://www.filedropper.com/test_30
Is my code correct? Can it be improved?
Offline
Why not just use a bitmap?
It is not useful IMHO to use a TMetaFile.
Just get the bitmap of the screenshot, then draw it on the PDF.
See e.g. how TForm1.btn1Click does this in https://github.com/synopse/mORMot/tree/ … rom%20code
Offline
Because that's not what I need... anyways, do you see something wrong in the code I'm using for PDF? Any missing calls, ordering, flags?
Last edited by leus (2016-04-07 22:07:41)
Offline
Pages: 1