#1 2016-04-07 17:29:27

leus
Member
Registered: 2012-09-05
Posts: 79

Screenshot to PDF

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

#2 2016-04-07 17:42:01

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

Re: Screenshot to PDF

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

#3 2016-04-07 22:06:46

leus
Member
Registered: 2012-09-05
Posts: 79

Re: Screenshot to PDF

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

#4 2016-04-08 07:50:57

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

Re: Screenshot to PDF

Take a new look at the sample.
It exports the form screnshot to a pdf.
Use a TBitmap would allow to scale the picture to the expected size.

Offline

Board footer

Powered by FluxBB