export easy using synpdf (synopse) (free)

General TRichView support forum. Please post your questions here
Post Reply
RobertoVG
Posts: 40
Joined: Fri Mar 15, 2013 8:35 pm

export easy using synpdf (synopse) (free)

Post by RobertoVG »

For those who are tired of trying to solve bugs with eDocEngine to export to pdf like me, here below is a solution using the synopsis that appears to be simple and only with time we will have the solution (final - I hope).

_without having to worry about header and footer

_the first tests this unit is work fine in Delphi 7 and Windows 7

I got some references here on this forum in http://www.trichview.com/forums/viewtop ... t=metafile

(work with SRichViewEdit)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Code: Select all

unit UnExpParaPDF;

interface

  Uses Windows,Types,Graphics,Forms,SclRView,RVStyle,SysUtils,Dialogs,
  ShellAPI,
  Synpdf,SynGdiPlus; //synopse

                           //Formxx.Handle
  procedure ExportaParaPdf ( handle:HWND; srve :TSRichViewEdit);

implementation

function MakePageMetafile(srve:TSRichViewEdit;aPageNo, aWidth, aHeight: Integer): TMetafile;
var
  savTextDraw: Boolean;
begin
  savTextDraw := ScaleRichViewTextDrawAlwaysUseGlyphs;
  ScaleRichViewTextDrawAlwaysUseGlyphs := False;

  Result := TMetafile.Create;
  Result.Width := aWidth;
  Result.Height := aHeight;
  srve.CanUpdate := False;
  srve.UseDrawHyperlinksEvent := True;
  srve.DrawMetafile(aPageNo, Result, False, True, False);
  ScaleRichViewTextDrawAlwaysUseGlyphs := savTextDraw;
  srve.CanUpdate := True;
  srve.UseDrawHyperlinksEvent := False;
end;


procedure ExportaParaPdf ( handle:HWND; srve :TSRichViewEdit);
const
  OutputFileName = 'c:\teste\testepdf.pdf';

var
  i: Integer;
  Metafile: TMetafile;
  RVUnit: TRVUnits;
  R : TRect;
  //synopse
  PdfDoc : TPDFDocumentGDI;
  PdfPage : TPdfpage;

begin
  srve.Update; //srve is ScaleRichViewComponent
  RVUnit := srve.UnitsProgram; //Get the current value
  srve.UnitsProgram := rvuPixels; //Change DBSRichviewEdit to pixels for PDF Conversion

  PdfDoc := TPdfDocumentGDI.Create;

  for i := 1 to srve.PageCount do
  begin

    Metafile := MakePageMetafile(srve,i, Round(srve.PageProperty.PageWidth), Round(srve.PageProperty.PageHeight));

    try

      PdfPage := PdfDoc.AddPage;
      if i=1 then begin
        PdfDoc.CompressionMethod := cmFlateDecode;
        pdfdoc.UseFontFallBack:=true;
        PdfDoc.UseSetTextJustification := False;
        PdfDoc.DefaultPaperSize := psA4;
        PdfDoc.DefaultPageLandscape := False;
        PdfDoc.VCLCanvas.Font.Charset:=ANSI_CHARSET;
        PdfDoc.UseUniscribe:=False;
      end;
//      PdfDoc.VCLCanvas.StretchDraw( Rect(0, 0, Round(Screen.PixelsPerInch * Metafile.MMWidth / 3600), Round(Screen.PixelsPerInch * Metafile.MMHeight / 3600)), Metafile);
        PdfDoc.VCLCanvas.StretchDraw( Rect(0, 0, Round(srve.PageProperty.PageWidth), Round(srve.PageProperty.PageHeight)), Metafile);
    finally
      Metafile.Free;
    end;
  end;
  srve.UnitsProgram := RVUnit; //Change back to the previous value before converted to pixels


  PdfDoc.SaveToFile(OutputFileName);
  PdfDoc.Free;

  ShellExecute(Handle, nil, PChar(OutputFileName), nil,  nil, SW_SHOWNORMAL);


end;

end.
Thanks
RobertoVG
Posts: 40
Joined: Fri Mar 15, 2013 8:35 pm

new updade

Post by RobertoVG »

In previous routine, there was problem when printing landscape.

To solve this modified routine as below and there was no inconsistency in both landscape or portrait

Code: Select all

procedure ExportaParaPdf ( handle:HWND; srve :TSRichViewEdit; Arquivo:String);
var
  i: Integer;
  Metafile: TMetafile;
  RVUnit: TRVUnits;
  R : TRect;
  //synopse
  PdfDoc : TPDFDocument;

begin
  srve.Update; //srve is ScaleRichViewComponent
  RVUnit := srve.UnitsProgram; //Get the current value
  srve.UnitsProgram := rvuPixels; //Change DBSRichviewEdit to pixels for PDF Conversion

  PdfDoc := TPdfDocument.Create;

  for i := 1 to srve.PageCount do
  begin

    Metafile := MakePageMetafile(srve,i, Round(srve.PageWidth100Pix), Round(srve.PageHeight100Pix));

    try

        PdfDoc.DefaultPageWidth := MulDiv(srve.PageWidth100Pix,72,PdfDoc.ScreenLogPixels);
        PdfDoc.DefaultPageHeight := MulDiv(srve.PageHeight100Pix,72,PdfDoc.ScreenLogPixels);
        PdfDoc.AddPage;
        // draw the page content
        PdfDoc.Canvas.RenderMetaFile(MetaFile,1,0,0);


    finally
      Metafile.Free;
    end;
  end;

  PdfDoc.SaveToFile(Arquivo);
  PdfDoc.Free;

  ShellExecute(Handle, nil, PChar(Arquivo), nil,  nil, SW_SHOWNORMAL);

  srve.UnitsProgram := RVUnit; //Change back to the previous value before converted to pixels

end;
ccr
Posts: 8
Joined: Sat Nov 13, 2010 12:47 am

Post by ccr »

hello,
i'm very interested in your code.
i downloaded the latest stable version from synpdf and used your source.
the program compiles with delphi xe2 but it does not start. instead i get a runtime error 217 (OS: Win XP SP3). when i use gnostice again (i have a compiler switch for changing the libraries) everything is fine ... so it probably has to do with synpdf/syngdiplus

do you have an idea what could be the reason for this runtime error?
the program does not even goto FormCreate of my mainform.

i am definitely fed up with gnostice as well ... it takes ages for fixing the issues
RobertoVG
Posts: 40
Joined: Fri Mar 15, 2013 8:35 pm

Post by RobertoVG »

the above routine works in Delphi 7 and I think that should be reviewed for XE2 using debug. Error 217 often report before startup and after finalizing cases.
Post Reply