#1 2013-07-12 13:39:35

davidheffernan
Member
Registered: 2013-05-16
Posts: 22

SaveToStreamDirectBegin and metafiles/VCLCanvas

I'm having problems trying to use SaveToStreamDirectBegin etc. with VCLCanvas and metafiles.  For example, here's a simple demo program that produces an empty PDF file.  You'll recognise the code since I took it from here: http://blog.synopse.info/post/2010/05/0 … PDF-engine

If I switch from SaveToStreamDirectBegin to SaveToFile then the generated PDF file is as expected.

I'm using your code from this revision: http://synopse.info/fossil/vinfo?name=d … 0ec68b709b

Can I expect SaveToStreamDirectBegin to work in this setting, or should I give up on it?

program SaveToStreamDirectBeginQuery;

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes, Graphics, SynPdf;

procedure MakePDF(const FileName: string);
var
  i: Integer;
  Stream: TStream;
begin
  with TPdfDocumentGDI.Create do
    try
      Stream := TFileStream.Create(FileName, fmCreate);
      try
        SaveToStreamDirectBegin(Stream);
        for i := 1 to 9 do
        begin
          AddPage;
          with VCLCanvas do
          begin
            Font.Name := 'Times new roman';
            Font.Size := 150;
            Font.Style := [fsBold, fsItalic];
            Font.Color := clNavy;
            TextOut(100, 100, 'Page ' + IntToStr(i));
          end;
          SaveToStreamDirectPageFlush;
        end;
        SaveToStreamDirectEnd;
      finally
        Stream.Free;
      end;
    finally
      Free;
    end;
end;

begin
  MakePDF('C:\desktop\test.pdf');
end.

Offline

#2 2013-07-12 14:22:18

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

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

In fact, it could not work as such.

Direct content flushing was working not with TPdfDocumentGDI metafile content, only within SaveToStream.

I've introduced a new TPdfDocumentGDI.SaveToStreamDirectPageFlush overriden method.
It can be used to reduce the used memory even more, by-passing page content compression.
See http://synopse.info/fossil/info/a2cc7123b0

Now your sample code will work as expected.

Offline

#3 2013-07-12 15:45:13

davidheffernan
Member
Registered: 2013-05-16
Posts: 22

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

OK, that does a lovely job solving my problem.  Thank you kindly.

But the SynPdf unit does not compile at the moment when USE_PDFSECURITY is not defined.

Offline

#4 2013-07-13 09:23:41

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

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

You are rigvht.
I've fixed SynPdf compilation issue when USE_PDFSECURITY conditional is not defined.

See http://synopse.info/fossil/info/c9a7b43020

Offline

#5 2013-07-15 12:58:50

davidheffernan
Member
Registered: 2013-05-16
Posts: 22

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

Thank you very much for that. Much appreciated.

My next step is to try drawing a metafile onto a PDF doc that is using SaveToStreamDirectBegin.  I'm hoping that will work.  I'll get back to you if I have problems, but since I'm moving house at the moment, it may be a little while.

Offline

#6 2013-07-15 13:09:57

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

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

Internally, the VCLCanvas is already a metafile.
It should work as expected.
We used nested metafiles with success (including plain external EMF, or charts).
EMF support is pretty good, due to user feedback and patches.

In your case, using TPdfDocumentGDI is not even needed.
You can use a plain TPdfDocument, then render the metafile content on each page one after the other.
See how TGdiPages component in mORMotReport.pas is implemented.

Offline

#7 2013-07-15 19:45:25

davidheffernan
Member
Registered: 2013-05-16
Posts: 22

Re: SaveToStreamDirectBegin and metafiles/VCLCanvas

Thanks again. I'm sure it will work out!!

Offline

Board footer

Powered by FluxBB