Print Page | Close Window

Lazarus: Aspect Ratio is off

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=3723
Printed Date: 05 May 24 at 1:50AM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Lazarus: Aspect Ratio is off
Posted By: RedOctober2018
Subject: Lazarus: Aspect Ratio is off
Date Posted: 22 Jun 19 at 4:43PM
I have successfully installed QuickPDF 10.13 Active X in Lazarus 1.8.4.  I am able to display .Pdf documents.  Everything I need to do is "mostly" working, however I have an annoying problem.  The displayed aspect ratio is not correct.  The displayed image of the Pdf page is always a little fuzzy, unlike when the same .Pdf file is displayed in Adobe or Nuance, which is always clear.  

Also, when I display in zooms above 100%, the right edge is cut off.  As I zoom higher and higher (like 400% for example, more and more of the right edge is cut off).  Unlike in the Delphi version, the ActiveX version of QuickPDF does not have the very convenient and simple "LoadFromStream" which takes care of all the aspect ratio, zoom, and rotation etc. In the ActiveX version I have to do all this manually.  Here is the code I am using.  Can some generous person correct it so I can get nice clear .Pdf renderings, with the full image showing regardless of rotation or zoom?

<CODE>

function RenderPDFPageAX(const fnm: String; const img: TImage; const pg: Integer; var pc: Integer; const zm: Integer; var err_msg: String): Boolean;
var
  vch: Variant;  // Variant containing the Canvas Handle
  bmp: TBitmap;
  dpi, pg_rt, zdvi: Integer;   zdvd: Double;
  wdtd, hgtd, wdtn, hgtn, wdts, hgts: Double;
  wdti, hgti: Integer;
  rnd_md: TFPURoundingMode;
  sbx: TScrollBox;
  obj: TObject;
  tmp_fnm: String;
begin
  Result := True;
  err_msg := '';
  sbx := nil;

  ReleasePdfImg(img);

  obj := img.Parent;
  if not Assigned(obj) then
    Exit;
  if (obj is TScrollBox) then
    sbx := (obj as TScrollBox);
  if not Assigned(sbx) then
    Exit;

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

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

  bmp := nil;
  try
    if Assigned(GlobalVars.QwkPdf.axl) then
      begin
        inc(GlobalVars.RenderID);
        tmp_fnm := GlobalVars.AppProps.pdf_tmp_fdr + IntToStr(GlobalVars.RenderID) + '.dat';
        GlobalVars.QwkPdf.axl.OleServer.SetTempFile(tmp_fnm);

        Result := (GlobalVars.QwkPdf.axl.OleServer.LoadFromFile(fnm, '') = 1);
        if Result then
          begin
            pc := GlobalVars.QwkPdf.axl.OleServer.PageCount;
            GlobalVars.QwkPdf.axl.OleServer.SelectPage(pg);
            pg_rt := GlobalVars.QwkPdf.axl.OleServer.PageRotation;

            // Page Points
            wdtd := GlobalVars.QwkPdf.axl.OleServer.PageWidth;
            hgtd := GlobalVars.QwkPdf.axl.OleServer.PageHeight;

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

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

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

            bmp := TBitmap.Create;
            bmp.Canvas.Brush.Color := clWhite;

            if (pg_rt = 0) or (pg_rt = 180) then
              begin
                bmp.Width := wdti;
                bmp.Height := hgti;
              end
            else
              begin
                bmp.Width := hgti;
                bmp.Height := wdti;
              end;

            // Adjust for Zoom
            dpi := (zm * GlobalVars.QwkPdf.sppi) div 100;
            if not (zm = 100) then
              begin
                if (zm > 100) then
                  begin
                    zdvi := (zm div 100);
                    bmp.Width := bmp.Width * zdvi;
                    bmp.Height := bmp.Height * zdvi;
                  end;
                if (zm < 100) then
                  begin
                    zdvd := (zm / 100);
                    bmp.Width := Trunc(bmp.Width * zdvd);
                    bmp.Height := Trunc(bmp.Height * zdvd);
                  end;
              end;

            bmp.Canvas.FillRect(Rect(0, 0, bmp.Width, bmp.Height));
            img.Picture.Assign(bmp);
            vch := bmp.Canvas.Handle;
          end
        else
          begin
            err_msg := 'Unable to load PDF Viewer from file: ' + fnm + #13 + 'ErrorCode: ' + IntToStr(GlobalVars.QwkPdf.axl.OleServer.LastErrorCode);
            Exit;
          end;

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


</CODE>



Replies:
Posted By: RedOctober2018
Date Posted: 30 Jun 19 at 7:44PM
For those who are interested, here is the corrected code.  I set the color of the BitMap to blue, so you can tell if the document is contained within an exact same sized image as the BitMap, or if the BitMap is a tiny bit bigger.  This code fixes the problem I had above, and appears to make an exact same sized BitMap (which is perfect) or one that is just a few pixels larger (which is ok).  In any case, the document is no longer being cut off, and rotation works as well.

<CODE>
function RenderPDFPageAX(const fnm: String; const img: TImage; const pg: Integer; var pc: Integer; const zm: Integer; var err_msg: String): Boolean;
var
  vch: Variant;  // Variant containing the Canvas Handle
  bmp: TBitmap;
  dpi, pg_rt, zdvi: Integer;   zdvd: Double;
  wdtd, hgtd, wdtn, hgtn, wdts, hgts: Double;
  wdti, hgti: Integer;
  rnd_md: TFPURoundingMode;
  sbx: TScrollBox;
  obj: TObject;
  tmp_fnm: String;
begin
  Result := True;
  err_msg := '';
  sbx := nil;

  ReleasePdfImg(img);

  obj := img.Parent;
  if not Assigned(obj) then
    Exit;
  if (obj is TScrollBox) then
    sbx := (obj as TScrollBox);
  if not Assigned(sbx) then
    Exit;

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

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

  bmp := nil;
  try
    if Assigned(GlobalVars.QwkPdf.axl) then
      begin
        inc(GlobalVars.RenderID);
        tmp_fnm := GlobalVars.AppProps.pdf_tmp_fdr + IntToStr(GlobalVars.RenderID) + '.dat';
        GlobalVars.QwkPdf.axl.OleServer.SetTempFile(tmp_fnm);

        Result := (GlobalVars.QwkPdf.axl.OleServer.LoadFromFile(fnm, '') = 1);
        if Result then
          begin
            with GlobalVars.QwkPdf.axl.OleServer do
              begin
                SetMeasurementUnits(2);   //  2 = Inches.
                SetGDIPlusOptions(0, 1);
                SetGDIPlusOptions(1, 1);
                SetGDIPlusOptions(2, 1);
                SetGDIPlusOptions(3, 1);
                SetGDIPlusOptions(10, 2);
                SetGDIPlusOptions(11, 2);
              end;

            pc := GlobalVars.QwkPdf.axl.OleServer.PageCount;
            GlobalVars.QwkPdf.axl.OleServer.SelectPage(pg);
            pg_rt := GlobalVars.QwkPdf.axl.OleServer.PageRotation;

            // Page Size in inches
            wdtd := GlobalVars.QwkPdf.axl.OleServer.PageWidth;
            hgtd := GlobalVars.QwkPdf.axl.OleServer.PageHeight;

            bmp := TBitmap.Create;
            bmp.Canvas.Brush.Color := clBlue; //clWhite;

            // Inches to Screen pixels
            wdti := Trunc(wdtd * GlobalVars.QwkPdf.sppi);
            hgti := Trunc(hgtd * GlobalVars.QwkPdf.sppi);

            if (pg_rt = 0) or (pg_rt = 180) then
              begin
                bmp.Width := wdti;
                bmp.Height := hgti;
              end
            else
              begin
                bmp.Width := hgti;
                bmp.Height := wdti;
              end;

            // Adjust for Zoom
            zdvd := 1;
            if not (zm = 100) then
              begin
                if (zm > 100) then
                  begin
                    zdvd := (zm div 100);
                  end;
                if (zm < 100) then
                  begin
                    zdvd := (zm / 100);
                  end;
              end;
            wdti := Trunc(bmp.Width * zdvd);
            hgti := Trunc(bmp.Height * zdvd);
            bmp.Width := wdti;
            bmp.Height := hgti;

            bmp.Canvas.FillRect(Rect(0, 0, bmp.Width, bmp.Height));
            img.Picture.Assign(bmp);
            vch := bmp.Canvas.Handle;
          end
        else
          begin
            err_msg := 'Unable to load PDF Viewer from file: ' + fnm + #13 + 'ErrorCode: ' + IntToStr(GlobalVars.QwkPdf.axl.OleServer.LastErrorCode);
            Exit;
          end;

        Result := (GlobalVars.QwkPdf.axl.OleServer.RenderPageToDC(Trunc(GlobalVars.QwkPdf.sppi * zdvd), pg, vch) = 1);
        if not Result then
          begin
            err_msg := 'Unable to render image from QuickPDF ActiveX' + #13 + GlobalVars.QwkPdf.axl.OleServer.LastRenderError;
            Exit;
          end;

      end;
  finally
    bmp.Free;
  end;
end;

</CODE>


Posted By: RedOctober2018
Date Posted: 30 Jun 19 at 10:24PM
procedure ReleasePdfImg(const img: TImage);
begin
  if not Assigned(img) then
    Exit;
  img.Picture.Bitmap := nil;
  img.Picture.Graphic := nil;
end;




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