#1 2014-07-30 18:28:15

lobokoch
Member
Registered: 2014-07-11
Posts: 6

Render a metafile with change its size in the PDF page

I need to render a metafile with the same size that the page, but with margins (the metafile is without margins). The bounds of the metafile needs to be reduced to can fit it with margins in the page (something like TCanvas.StreachDraw)

I tried to use the RenderMetaFile routine but without success. I changed the R.Right and   R.Bottom but don't worked.

Anybody has some sugestion about how to solve this?

Kind regards,
Márcio.

Offline

#2 2014-07-30 18:31:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,504
Website

Re: Render a metafile with change its size in the PDF page

You can take a look at mORMotReport.pas or even the HTML2PDF sample.

Offline

#3 2014-07-31 13:42:28

lobokoch
Member
Registered: 2014-07-11
Posts: 6

Re: Render a metafile with change its size in the PDF page

This library is awesome, I replaced the TPdfDocument by TPdfDocumentGDI and I used  code like bellow:

//...
xPDFGDI := TPdfDocumentGDI.Create(False, 0, True, nil); //With PDF/A
try
  xPDFGDI.ScreenLogPixels := 72;
  for i := 0 to PagesCount - 1 do
  begin
    //Get the width and height with resolution 72 dpi.
    xWidth := MulDiv(Round(xPageWidth), 72, Screen.PixelsPerInch);
    xHeight := MulDiv(Round(xPageHeight), 72, Screen.PixelsPerInch);

    //The width and height properties must be set before to call AddPage.
    xPDFGDI.DefaultPageWidth := xWidth;
    xPDFGDI.DefaultPageHeight := xHeight;

    //Adjust the resolution from 96 dpi to 72 dpi
    xLeft := MulDiv(xLeft, 72, Screen.PixelsPerInch);
    xTop := MulDiv(xTop, 72, Screen.PixelsPerInch);

    xRight := MulDiv(xRight, 72, Screen.PixelsPerInch);
    xBottom := MulDiv(xBottom, 72, Screen.PixelsPerInch);

    //Calculates de printable area
    xWidth := xWidth - (xLeft + xRight);
    xHeight := xHeight - (xTop + xBottom);

    //Calculate the final coordinates
    xRight := xLeft + xWidth;
    xBottom := xTop + xHeight;

    //Add a new page to PDF
    xPDFGDI.AddPage;

    //Render the metafile on PDF canvas with StretchDraw.
    xPDFGDI.VCLCanvas.StretchDraw(Rect(xLeft, xTop, xRight, xBottom), Pages[i].Meta); 
  end;
  //Saves the PDF/A file in the disk.
  xPDFGDI.SaveToFile(aFileName);
finally
  xPDFGDI.Free;
end;

Worked perfectly, now I can change de margins as I need and the metafile is drawn correct into the remaining drawing area.

Kind regards,
Márcio Koch.

Offline

#4 2014-07-31 20:13:21

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 13,504
Website

Re: Render a metafile with change its size in the PDF page

I did stick the topic since it may help other users to work with margins.
smile

Offline

#5 2017-04-27 13:36:43

alex
Member
Registered: 2017-04-27
Posts: 1

Re: Render a metafile with change its size in the PDF page

hi, here i found a problem, when embed font , call TPdfFontTrueType.PrepareForSaving frequently , it appears out of memory; use vmmap tools find that virtual Memory leaks(private data). can any one help me, thanks!

Offline

Board footer

Powered by FluxBB