Print Page | Close Window

ActiveX Library in Lazarus always freezes

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: I need help - I can help
Forum Description: Problems and solutions while programming with the Debenu Quick PDF Library and Debenu PDF Viewer SDK
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=3711
Printed Date: 29 Mar 24 at 5:40AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: ActiveX Library in Lazarus always freezes
Posted By: RedOctober2018
Subject: ActiveX Library in Lazarus always freezes
Date Posted: 14 May 19 at 5:43PM
I must be doing something obviously simple and wrong but I can't see it.  
My platform is:  Lazarus 1.8.4 and FPC 3.0.4 QuickPDF, ActiveX, version 10.13.

First Load always works.  Second load always freezes. I must be freeing something I shdn't or not freeing something I shd. 

What you are looking at is the second attempt where I create and free the QuickPDF object each time.  Before I was just using the QuickPDF ActiveX wrapper component.  Both methods have exactly the same problem.  


function UnlockedQwkPdfOK(const axl: TAxcPDFLibrary): Boolean;
begin
  Result := (axl.OleServer.UnlockKey(quick_pdf_license_key) = 1);
end;

function RenderPDFPageAX(const fnm: String; const img: TImage; const sbx: TScrollBox; const sppi, pg: Integer; const zm: Integer; var err_msg: String): Boolean;
var
  axl: TAxcPDFLibrary;
  vch: Variant;  // Variant containing the Canvas Handle
  bmp: TBitmap;
  dpi: Integer;
  wdtd, hgtd, wdtn, hgtn, wdts, hgts: Double;
  wdti, hgti: Integer;
begin
  Result := True;
  err_msg := '';

  ReleasePdfImg(img);

  sbx.HorzScrollBar.Position := 0;
  sbx.VertScrollBar.Position := 0;

  img.AutoSize := True;
  img.Left := 0;
  img.Top := 0;

  bmp := nil;
  axl := nil;
  try
    axl := TAxcPDFLibrary.Create(nil);

    if Assigned(axl) then
      begin
        Result := UnlockedQwkPdfOK(axl);
        if not Result then
          Exit;

        Result := (axl.OleServer.LoadFromFile(fnm, '') = 1);  // <--- First time through, file loads and displays correctly.  Second time through program freezes at this point.  100% of the time guaranteed freezeup.
        if Result then
          begin
            // Page Points
            wdtd := axl.OleServer.PageWidth;
            hgtd := axl.OleServer.PageHeight;

            // Page Points to Inches
            wdtn := wdtd / 72;
            hgtn := hgtd / 72;

            // Inches to Screen pixels
            wdts := wdtn * sppi;
            hgts := hgtn * sppi;

            Math.SetRoundMode(rmUp);
            wdti := Round(wdts);
            hgti := Round(hgts);

            dpi := (zm * sppi) div 100;

            bmp := TBitmap.Create;
            bmp.Width := wdti;
            bmp.Height := hgti;
            img.Picture.Assign(bmp);
            vch := bmp.Canvas.Handle;
          end
        else
          begin
            err_msg := 'Unable to load PDF Viewer from file: ' + fnm + #13 + 'ErrorCode: ' + IntToStr(axl.OleServer.LastErrorCode);
            Exit;
          end;

        Result := (axl.OleServer.RenderPageToDC(dpi, pg, vch) = 1);
        if not Result then
          begin
            err_msg := 'Unable to render image from QuickPDF ActiveX' + #13 + axl.OleServer.LastRenderError;
            Exit;
          end;
      end;
  finally
    axl.Free;
    bmp.Free;
  end;
end;

procedure ReleasePdfImg(const img: TImage);
begin
  if not Assigned(img) then
    Exit;
  img.Picture.Bitmap := nil;
  img.Picture.Graphic := nil;
end;




Replies:
Posted By: Ingo
Date Posted: 14 May 19 at 9:34PM
Hi Red and anybody who's reading here,

i always like to help but ActiveX isn't mine - so if there is anybody out there and like to give as well as to take... please advice ;-)



-------------
Cheers,
Ingo



Posted By: RedOctober2018
Date Posted: 14 May 19 at 9:43PM
I have rebuilt the function to use a variant, but the exact same problem happens upon file load.  This fails in the exact same spot (on file load)

Here is the code:

function RenderPDFPageDLL(const fnm: String; const img: TImage; const sbx: TScrollBox; const sppi, pg: Integer; const zm: Integer; var err_msg: String): Boolean;
var
  qwk: Variant;
  vch: Variant;  // Variant containing the Canvas Handle
  bmp: TBitmap;
  dpi: Integer;
  wdtd, hgtd, wdtn, hgtn, wdts, hgts: Double;
  wdti, hgti: Integer;
begin
  Result := True;
  err_msg := '';

  ReleasePdfImg(img);

  sbx.HorzScrollBar.Position := 0;
  sbx.VertScrollBar.Position := 0;

  img.AutoSize := True;
  img.Left := 0;
  img.Top := 0;

  bmp := nil;
  qwk := nil;
  try
    qwk := CreateOleObject('DebenuPDFLibraryAX1013.PDFLibrary');

    if not VarIsEmpty(qwk) then
      begin
        Result := qwk.UnlockKey(quick_pdf_license_key);
        if not Result then
          Exit;

        Result := (qwk.LoadFromFile(fnm, '') = 1);
        if Result then
          begin
            // Page Points
            wdtd := qwk.PageWidth;
            hgtd := qwk.PageHeight;

            // Page Points to Inches
            wdtn := wdtd / 72;
            hgtn := hgtd / 72;

            // Inches to Screen pixels
            wdts := wdtn * sppi;
            hgts := hgtn * sppi;

            Math.SetRoundMode(rmUp);
            wdti := Round(wdts);
            hgti := Round(hgts);

            dpi := (zm * sppi) div 100;

            bmp := TBitmap.Create;
            bmp.Width := wdti;
            bmp.Height := hgti;
            img.Picture.Assign(bmp);
            vch := bmp.Canvas.Handle;
          end
        else
          begin
            err_msg := 'Unable to load PDF Viewer from file: ' + fnm + #13 + 'ErrorCode: ' + IntToStr(qwk.LastErrorCode);
            Exit;
          end;

        Result := (qwk.RenderPageToDC(dpi, pg, vch) = 1);
        if not Result then
          begin
            err_msg := 'Unable to render image from QuickPDF ActiveX' + #13 + qwk.LastRenderError;
            Exit;
          end;
      end;
  finally
    qwk := null;
    bmp.Free;
  end;
end;



Posted By: RedOctober2018
Date Posted: 14 May 19 at 10:44PM
The problem was caused by my setting the Math.SetRoundMode.

This fixes the file loading problem:

var
...
  rnd_md: TFPURoundingMode;

begin

            rnd_md := Math.GetRoundMode;
            Math.SetRoundMode(rmUp);
            wdti := Round(wdts);
            hgti := Round(hgts);
            Math.SetRoundMode(rnd_md);
          





Posted By: Ingo
Date Posted: 14 May 19 at 10:49PM
Mmmmh... but you told us that the problem always raise with the second Load...?



-------------
Cheers,
Ingo



Posted By: RedOctober2018
Date Posted: 14 May 19 at 11:00PM
Correct.  The line of code that sets the rounding comes after the first file load.  So the second file load occurs with the rmUp still in effect.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk