Codice essenziale per mostrare un file PDF

Da Gambas-it.org - Wikipedia.

Per mostrare un documento PDF, bisognerà utilizzare le risorse del Componente gb.poppler.

Mostriamo un semplice esempio, nel quale bisognerà porre sul Form una ImageView e attivare l'indispensabile Componente gb.poppler.
Ci serviremo, quindi, della Classe PdfDocument, la quale consente di leggere il contenuto di un documento PDF e di mostrarlo.

Private pd As PdfDocument
Private c As Short


Public Sub Form_Open()
 
' Carica il file PDF del documento da mostrare:
 pd = New PdfDocument("/percorso/del/file.pdf")

End


' Cliccando sulla "ImageView" viene mostrata la pagina corrente del documento PDF
' Cliccando ancora si passa alla eventuale pagina successiva.
Public Sub ImageView1_MouseUp()

 Dim im As Image
 Dim w, h, r As Short
 
 w = pd[0].Render().W
 h = pd[0].Render().H
 r = pd.Resolution / 72
 
' Modifica la risoluzione e dunque la dimensione della pagina corrente del documento PDF e la converte in una "Image":
 im = pd[c].Render(0, 0, w / r, h / r, 0, pd.Resolution / r)
 
' Adatta l'oggetto "ImmageView" alla dimensione dell'immagine del documento PDF e carica l'immagine nella "ImageView":
 Me.Resize(w / r, h / r)
 ImageView1.Resize(w / r, h / r)
 ImageView1.Image = im
 
 Me.Caption = "Pagina: " & CStr(c + 1)
 
 Inc c
 
' Se si clicca sull'immagine dell'ultima pagina del PDF, si torna a mostrare la prima pagina:
 If c == pd.Count Then c = 0
 
End


Mostrando il documento nell'Oggetto DocumentView

Mostriamo un primo esempio [Nota 1] per mostrare un file pdf mediante un Oggetto DocumentView posto sul Form:

Private $hPdf As PdfDocument

Public Sub Form_Open()
 
 Dim sPath As String
 
 Dialog.Title = ("Open a new pdf file")
 Dialog.Filter = ["*.pdf", "PDF Files"]
 Dialog.Path = User.Home
 If Dialog.OpenFile() Then
   Me.Close
   Return
 Endif
 sPath = Dialog.Path
 If Right(sPath, 3) <> "pdf" Then
   Message.Warning(("You need to open a file <b>\".pdf\"  "))
   Me.Close
   Return
 Endif
 Try $hPdf = New PdfDocument(sPath)
 If Error Then
   Message.Warning(("Cannot open the file, it may be encrypted  "))
   Me.Close
   Return
 Endif
 DocumentView1.Count = $hPdf.Count
  
End

Public Sub DocumentView1_Layout(Page As Integer)
 
 Last.Layout.Height = $hPdf[Page].Render().H
 Last.Layout.Width = $hPdf[Page].Render().W
  
End

Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
 Dim fRes As Float
 Dim himg As Image
 
 fRes = $hPdf.Resolution / 72
 himg = $hPdf[Page].Render(0, 0, Width / fRes, Height / fRes, 0, $hPdf.Resolution / fRes)
 Draw.Image(himg, 0, 0, Width, Height)
 
End

Public Sub btnZoomIn_Click()
 
 Dim f As Float = DocumentView1.Zoom
 
 f += 0.05
 If f > 1.5 Then f = 1
 DocumentView1.Zoom = f
 DocumentView1.Count = $hPdf.Count
 DocumentView1.Refresh
 Wait
  
End


Mostriamo un secondo esempio:

Private doc As PdfDocument


Public Sub Form_Open()
 
 doc = New PdfDocument("/percorso/del/file.pdf", Null, Null)
 
 With Me
   .Center
   .W = doc[0].W + 400
   .H = Screen.AvailableHeight * 0.95
 End With
 
 With DocumentView1
   .X = 0
   .Y = 0
   .W = Me.W * 0.9
   .H = Me.H
   .Count = 1
   .Zoom = 2
 End With
 
 With Slider1
   .X = Me.W * 0.95
   .Y = Me.H * 0.3
   .MinValue = 1
   .MaxValue = 10
   .Value = 2
 End With
  
End

Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
 Paint.DrawImage(doc[0].Render(0, 0, Width, Height, 0, (doc.Resolution / 3) * DocumentView1.Zoom), 0, 0, Paint.Width, Paint.Height)
 
End

Public Sub Slider1_Change()
 
 DocumentView1.Zoom = Slider1.Value
 
 Me.Caption = "ZOOM: " & CStr(Slider1.Value)
  
End


Note

[1] Questo codice è stato proposto dal membro Gianluigi del forum-it.org