Do you own a Debenu Quick PDF Library 12/11/10/9/8/7? Upgrade to Debenu Quick PDF Library 13!

Foxit Quick PDF Library

Frequently Asked Question:

Return to FAQ Index

How do I mimic the Adobe zoom options: Actual Size, Fit Page, Fit Width, Fit Visible ?

Question

Development Environment: Delphi 7 on XP Pro.

At this point, I am not creating any .Pdf files, I'm just trying to display them to my users.

I am new to Quick PDF Library. I have looked at the example delphi-dcu-demo and in there I have seen the "RenderPage" procedure, and the line: ViewPrintQP.RenderPageToStream(DPI, ViewPrintPageNum, 0, MS); But this doesn't help me much. All I can use it for is to apply the zooms in the RenderPage example (multiples of 25%)

I am trying to give my program the same abilities as the Adobe PDF reader which includes the subject zoom options. Can you give me some example code how to accomplish this?

Answer

Here is set of functions that allows to render PDF pages using four zoom types: Actual size, Fit Page, Fit Width, Fit Height. The page is rendered into simple Image that is fitted accordingly on any parent control Window like ScrollBox, Panel, etc.

const
  DefaultDPI = 72;
  Zoom_ActualSize = 0;
  Zoom_FitPage = 1;
  Zoom_FitWidth = 2;
  Zoom_FitHeight = 3;

  procedure ClearImage(Image: TImage);
  var
    bm: TBitmap;
  begin
    bm := TBitmap.Create;
    try
      Image.Picture.Assign(bm);
    finally
      bm.Free;
    end;
    Image.Width := 1;
    Image.Height := 1;
  end;

  function IsPageOrtoRotated(QPL: TQuickPDF; Page: Integer): Boolean;
  begin
    Result := False;
    QPL.SelectPage(Page);
    if (QPL.PageRotation = 90) or (QPL.PageRotation = 270) then
      Result := True;
  end;

  procedure RenderPageToImage(QPL: TQuickPDF; Page: Integer; Image: TImage;
    ZoomPercentage: Integer);
  var
    ms: TMemoryStream;
    bm: TBitmap;
    zoomdpi: Integer;
  begin
    zoomdpi := Round(ZoomPercentage*DefaultDPI/100);
    ms := TMemoryStream.Create;
    try
      QPL.RenderPageToStream(zoomdpi, Page, 0, ms);
      ms.Seek(0, soFromBeginning);
      bm := TBitmap.Create;
      try
        bm.LoadFromStream(ms);
        Image.Picture.Assign(bm);
      finally
        bm.Free;
      end;
    finally
      ms.Free;
    end;
  end;

  procedure ZoomPageImageToWindow(QPL: TQuickPDF; Page: Integer; Image: TImage;
    ZoomPercentage: Integer; ZoomType: Integer;
    WindowWidth: Integer; WindowHeight: Integer);
  var
    zoom: Integer;
  begin
    if ZoomType = Zoom_ActualSize then
    begin
      zoom := ZoomPercentage;
    end;
    if ZoomType = Zoom_FitPage then
    begin
      if IsPageOrtoRotated(QPL, Page) then
        if (QPL.PageHeight > QPL.PageWidth) and
            (WindowWidth/WindowHeight <= QPL.PageHeight/QPL.PageWidth) then
          zoom := Round(WindowWidth*100/QPL.PageHeight)
        else
          zoom := Round(WindowHeight*100/QPL.PageWidth)
      else
        if (QPL.PageWidth > QPL.PageHeight) and
            (WindowWidth/WindowHeight <= QPL.PageWidth/QPL.PageHeight) then
          zoom := Round(WindowWidth*100/QPL.PageWidth)
        else
          zoom := Round(WindowHeight*100/QPL.PageHeight);
    end;
    if ZoomType = Zoom_FitWidth then
    begin
      if IsPageOrtoRotated(QPL, Page) then
        zoom := Round(WindowWidth*100/QPL.PageHeight)
      else
        zoom := Round(WindowWidth*100/QPL.PageWidth);
    end;
    if ZoomType = Zoom_FitHeight then
    begin
      if IsPageOrtoRotated(QPL, Page) then
        zoom := Round(WindowHeight*100/QPL.PageWidth)
      else
        zoom := Round(WindowHeight*100/QPL.PageHeight);
    end;
    RenderPageToImage(QPL, Page, Image, zoom);
  end;

  procedure FitImageToWindow(Image: TImage;
    Left, Top, Width, Height: Integer);
  begin
    if Image.Width < Width then
      Image.Left := Left + (Width - Image.Width) shr 1
    else
      Image.Left := Left;
    if Image.Height < Height then
      Image.Top := Top + (Height - Image.Height) shr 1
    else
      Image.Top := Top;
  end;

And here is a sample how to use this functions. So it takes just to put sveral components:

  • ZoomScrollBox (TScrollBox) on the work Form;
  • ZoomImage (TImage) on ZoomScrollBox;
  • ZoomEdit (TEdit) for cutom zoom values;
  • ZoomCombobox (TComboBox) that contains four items in the following order:

Actual Size, Fit Page, Fit Width, Fit Height.

var
  WindowWidth, WindowHeight, WindowTop, WindowLeft: Integer;
  VertScrollBarSize, HorzScrollBarSize: Integer;
  ZoomValue: Integer;
  QPL: TQuickPDF;

begin
  // Create QPL and load some pdf here

  ClearImage(ZoomImage);   
  ZoomImage.Autosize := True;

  ZoomValue := StrToInt(ZoomEdit.Text); 
  VertScrollBarSize := 18; // if parent Window has no scrollbars
  HorzScrollBarSize := 18; // this values can be set to zero
  WindowLeft := 12;
  WindowTop := 12;
  WindowWidth := ZoomScrollBox.ClientWidth-VertScrollBarSize;
  WindowHeight := ZoomScrollBox.ClientHeight-HorzScrollBarSize;

  ZoomPageImageToWindow(PDFLibrary, MainForm.CurrentPage, ZoomImage,
    ZoomValue, ZoomCombobox.ItemIndex,
    WindowWidth, WindowHeight);
  FitImageToWindow(ZoomImage, WindowLeft, WindowTop, WindowWidth, WindowHeight);
end;

© 2015 Debenu & Foxit. All rights reserved. AboutBuyContactBlogNewsletterSupportFAQProduct UpdatesForum