
Beispiel-Programm PDF-Viewer in GB3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

' Gambas class file

'***************************************************************************
'
'  FMain.class
'
'  PdfViewer gb.pdf component example
'
'  (C) 2007 Daniel Campos Fernández <dcamposf@gmail.com>
'
'
'  This program is free software; you can redistribute it and/or modify
'  it under the terms of the GNU General Public License as published by
'  the Free Software Foundation; either version 1, or (at your option)
'  any later version.
'
'  This program is free software; you can redistribute it and/or modify
'  it under the terms of the GNU General Public License as published by
'  the Free Software Foundation; either version 1, or (at your option)
'  any later version.
'
'  This program is distributed in the hope that it will be useful,
'  but WITHOUT ANY WARRANTY; without even the implied warranty of
'  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'  GNU General Public License for more details.
'
'  You should have received a copy of the GNU General Public License
'  along with this program; if not, write to the Free Software
'  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
'
'***************************************************************************

Public hPdf As New PdfDocument
Public cIndex As Integer 
Public CurrentPage As Integer 
Public CurrentZoom As Float  

Public Sub BtOpen_Click()
  
  Dialog.Filter = ["*.pdf", ("Pdf documents")]
  If Dialog.OpenFile(False) Then Return 
  
  hPdf.Close()
  Try hPdf.Open(Dialog.Path)
  If Error Then 
    Message.Error(Error.Text)
    Return 
  End If

  CurrentZoom = 1
  CurrentPage = 1
  RenderPage()
  BtPrev.Enabled = False 
  If hPdf.Count > 1 Then 
    BtNext.Enabled = True 
  Else 
    BtNext.Enabled = False 
  End If
  btZoomIn.Enabled = True 
  btZoomOut.Enabled = True 
  btRotate.Enabled = True 
  pBox.Visible = True 
  TxtFind.Enabled = True 

  tvIndex.Clear()
  If hPdf.HasIndex Then 
    tvIndex.Visible = True 
    AddIndex(0, "")
  Else 
    tvIndex.Visible = False 
  End If
  
End

Public Function AddIndex(nItem As Integer, pItem As String) As Integer 
  
  Dim pR As String  
  Dim iPage As Integer
  
  Do
    iPage = 1
    Try iPage = hPdf.Index.Data.Page
    If Error Then Print Error.Text
    pR = nItem & "-" & iPage
    tvIndex.Add(nItem & "-" & iPage, hPdf.Index.Title, Null, pItem)
    Inc nItem
    If hPdf.Index.HasChildren Then 
     hPdf.Index.MoveChild()
     nItem = AddIndex(nItem + 1, pR)
     hPdf.Index.MoveParent()
    End If
  Loop Until hPdf.Index.MoveNext()
  
  Return nItem
  
End

Public Sub tvIndex_Click()

  CurrentPage = Mid(tvIndex.Current.Key, InStr(tvIndex.Current.Key, "-") + 1)
  BtPrev.Enabled = True 
  BtNext.Enabled = True 
  If CurrentPage = 1 Then BtPrev.Enabled = False 
  If CurrentPage = hPdf.Count Then BtNext.Enabled = False 
  RenderPage() 

End

Public Sub RenderPage()
  
  Dim hPic As Picture 
  Dim Bc As Integer 
  
  hPdf.Zoom = CurrentZoom
  LblInfo.Text = CurrentPage & " " & ("From") & " " & hPdf.Count  
  
  ViewPort.Scroll(0, 0)
  hPic = hPdf[CurrentPage].Image.Picture

  If TxtFind.Text <> "" Then 
  
    hPdf[CurrentPage].Find(TxtFind.Text)
    Paint.Begin(hPic)
    Paint.Brush = Paint.Color(Color.RGB(0, 0, 255, 192))
    For Bc = 0 To hPdf[CurrentPage].Result.Count - 1
      Paint.Rectangle(hPdf[CurrentPage].Result[Bc].Left, hPdf[CurrentPage].Result[Bc].Top, hPdf[CurrentPage].Result[Bc].Width, hPdf[CurrentPage].Result[Bc].Height)
    Next 
    Paint.Fill
    Paint.End
  
  End If

  PBox.Picture = hPic 
  PBox.Resize(hPdf[CurrentPage].Width, hPdf[CurrentPage].Height)
  Form_Resize()
  
Catch

  Balloon.Error(Error.Text, TxtFind)
  
End

Public Sub Form_Resize()

  If CurrentPage = 0 Then Return 
  pBox.Left = (ViewPort.Width - hPdf[CurrentPage].Width) / 2

End


Public Sub HSplit1_Resize()

  Form_Resize()  

End

Public Sub BtNext_Click()

  Inc CurrentPage
  If CurrentPage = hPdf.Count Then BtNext.Enabled = False 
  BtPrev.Enabled = True 
  RenderPage()

End

Public Sub BtPrev_Click()

  Dec CurrentPage
  If CurrentPage = 1 Then BtPrev.Enabled = False 
  BtNext.Enabled = True 
  RenderPage()

End

Public Sub btZoomIn_Click()

  If CurrentZoom < 3 Then CurrentZoom += 0.1
  If CurrentZoom = 3 Then btZoomIn.Enabled = False 
  btZoomOut.Enabled = True 
  RenderPage()

End

Public Sub btZoomOut_Click()

  If CurrentZoom > 0.5 Then CurrentZoom -= 0.1
  If CurrentZoom = 0.5 Then btZoomOut.Enabled = False 
  btZoomIn.Enabled = True 
  RenderPage()

End

Public Sub Form_Close()

  hPdf.Close()

End

Public Sub Button1_Click()

  Fabout.ShowDialog()

End

Public Sub btRotate_Click()

  Select Case hPdf.Orientation
    Case PdfDocument.Normal
      hPdf.Orientation = PdfDocument.Sideways
    Case PdfDocument.Sideways
      hPdf.Orientation = PdfDocument.Inverted 
    Case PdfDocument.Inverted
      hPdf.Orientation = PdfDocument.SidewaysInverted
    Case PdfDocument.SidewaysInverted
      hPdf.Orientation = PdfDocument.Normal 
  End Select 
  
  RenderPage()
  
End

Public Sub TxtFind_Change()

  RenderPage()

End


