Differenze tra le versioni di "Codice essenziale per mostrare un file PDF con le risorse del Componente gb.poppler"

Da Gambas-it.org - Wikipedia.
 
(2 versioni intermedie di uno stesso utente non sono mostrate)
Riga 7: Riga 7:
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
    
 
    
 
   With Me
 
   With Me
Riga 15: Riga 15:
 
    
 
    
 
  <FONT color=gray>' ''Carica il file PDF del documento da mostrare:''</font>
 
  <FONT color=gray>' ''Carica il file PDF del documento da mostrare:''</font>
   pd = New PdfDocument("<FONT color=gray>''/percorso/del/file.pdf''</font>")
+
   pd = New PdfDocument("<FONT color=darkgreen>''/percorso/del/file.pdf''</font>")
 
   
 
   
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  <FONT color=gray>' '''''Cliccando sulla "ImageView" viene mostrata la pagina corrente del documento PDF.''
+
  <FONT color=gray>' '''''Cliccando sulla "ImageView" viene mostrata la pagina corrente del documento PDF.'''''
  ' ''Cliccando ancora si passa alla eventuale pagina successiva.'''''</font>
+
  ' '''''Cliccando ancora si passa alla eventuale pagina successiva.'''''</font>
  '''Public''' Sub ImageView1_MouseUp()
+
  Public Sub ImageView1_MouseUp()
 
   
 
   
 
   Dim im As Image
 
   Dim im As Image
Riga 33: Riga 33:
 
   im = pd[c].Render(0, 0, w * 2, h * 2, 0, pd.Resolution * 2)
 
   im = pd[c].Render(0, 0, w * 2, h * 2, 0, pd.Resolution * 2)
 
    
 
    
  <FONT color=gray>' ''Adatta l'oggetto "ImmageView" alla dimensione dell'immagine del documento PDF e carica l'immagine nella "ImageView":''</font>
+
  <FONT color=gray>' ''Adatta l'oggetto "ImageView" alla dimensione dell'immagine del documento PDF e carica l'immagine nella "ImageView":''</font>
 
   With ImageView1
 
   With ImageView1
 
     .Resize(Me.w, Me.H)
 
     .Resize(Me.w, Me.H)
Riga 39: Riga 39:
 
   End With
 
   End With
 
    
 
    
   Me.Caption = "Pagina: " & CStr(c + 1)
+
   Me.Title = "Pagina: " & CStr(c + 1)
 
    
 
    
 
   Inc c
 
   Inc c
Riga 46: Riga 46:
 
   If c == pd.Count Then c = 0
 
   If c == pd.Count Then c = 0
 
    
 
    
  '''End'''
+
  End
  
  
Riga 55: Riga 55:
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
    
 
    
 
   With Me
 
   With Me
Riga 63: Riga 63:
 
   End With
 
   End With
 
    
 
    
   doc = New PdfDocument("<FONT color=gray>''/percorso/del/file.pdf''</font>")
+
   doc = New PdfDocument("<FONT color=darkgreen>''/percorso/del/file.pdf''</font>")
 
    
 
    
 
   With DocumentView1 = New DocumentView(Me) As "DocumentView1"
 
   With DocumentView1 = New DocumentView(Me) As "DocumentView1"
Riga 71: Riga 71:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 
   
 
   
 
   
 
   
  '''Public''' Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
+
  Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
    
 
    
 
   Paint.DrawImage(doc[Page].Render(0, 0, Width * 2, Height * 2, 0, doc.Resolution * 4), 0, 0, Paint.Width, Paint.Height)
 
   Paint.DrawImage(doc[Page].Render(0, 0, Width * 2, Height * 2, 0, doc.Resolution * 4), 0, 0, Paint.Width, Paint.Height)
 
    
 
    
  '''End'''
+
  End
 
+
Mostriamo un secondo esempio: <SUP>&#091;[[#Note|nota 1]]&#093;</sup>  
 
 
Mostriamo un secondo esempio: <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>  
 
 
  Private $hPdf As PdfDocument
 
  Private $hPdf As PdfDocument
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
    
 
    
 
   Dim sPath As String
 
   Dim sPath As String
Riga 109: Riga 107:
 
   DocumentView1.Count = $hPdf.Count
 
   DocumentView1.Count = $hPdf.Count
 
    
 
    
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub DocumentView1_Layout(Page As Integer)
+
  Public Sub DocumentView1_Layout(Page As Integer)
 
    
 
    
 
   Last.Layout.Height = $hPdf[Page].Render().H
 
   Last.Layout.Height = $hPdf[Page].Render().H
 
   Last.Layout.Width = $hPdf[Page].Render().W
 
   Last.Layout.Width = $hPdf[Page].Render().W
 
    
 
    
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
+
   
 +
Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
    
 
    
 
   Dim fRes As Float
 
   Dim fRes As Float
Riga 127: Riga 127:
 
   Draw.Image(himg, 0, 0, Width, Height)
 
   Draw.Image(himg, 0, 0, Width, Height)
 
    
 
    
  '''End'''
+
  End
 +
 +
 +
Public Sub btnZoomIn_Click()
 
   
 
   
'''Public''' Sub btnZoomIn_Click()
 
 
 
 
   Dim f As Float = DocumentView1.Zoom
 
   Dim f As Float = DocumentView1.Zoom
 
    
 
    
Riga 139: Riga 140:
 
   DocumentView1.Refresh
 
   DocumentView1.Refresh
 
   Wait
 
   Wait
 
+
 
  '''End'''
+
  End
 
 
 
 
 
Mostriamo un terzo esempio applicando lo ZOOM con uno "Slider":
 
Mostriamo un terzo esempio applicando lo ZOOM con uno "Slider":
 
  Private doc As PdfDocument
 
  Private doc As PdfDocument
 
   
 
   
 
   
 
   
  '''Public''' Sub Form_Open()
+
  Public Sub Form_Open()
 
    
 
    
   doc = New PdfDocument("<FONT Color=gray>''/percorso/del/file.pdf''</font>", Null, Null)
+
   doc = New PdfDocument("<FONT Color=darkgreen>''/percorso/del/file.pdf''</font>", Null, Null)
 
    
 
    
 
   With Me
 
   With Me
Riga 173: Riga 172:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 
   
 
   
  '''Public''' Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
+
   
 +
Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
    
 
    
 
   Paint.DrawImage(doc[Page].Render(0, 0, Width, Height, 0, (doc.Resolution / 3) * DocumentView1.Zoom), 0, 0, Paint.Width, Paint.Height)
 
   Paint.DrawImage(doc[Page].Render(0, 0, Width, Height, 0, (doc.Resolution / 3) * DocumentView1.Zoom), 0, 0, Paint.Width, Paint.Height)
Riga 185: Riga 185:
 
   End With
 
   End With
 
    
 
    
  '''End'''
+
  End
 +
 
   
 
   
  '''Public''' Sub Slider1_Change()
+
  Public Sub Slider1_Change()
 
    
 
    
 
   DocumentView1.Zoom = Slider1.Value
 
   DocumentView1.Zoom = Slider1.Value
Riga 193: Riga 194:
 
   Me.Caption = "ZOOM: " & CStr(Slider1.Value)
 
   Me.Caption = "ZOOM: " & CStr(Slider1.Value)
 
    
 
    
  '''End'''
+
  End
  
  

Versione attuale delle 15:46, 18 dic 2023

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()
 
 With Me
   .Width = Desktop.W
   .Height = Desktop.H
 End With
 
' 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 As Short
 
 w = pd[0].Render().W
 h = pd[0].Render().H
 
' 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 * 2, h * 2, 0, pd.Resolution * 2)
 
' Adatta l'oggetto "ImageView" alla dimensione dell'immagine del documento PDF e carica l'immagine nella "ImageView":
 With ImageView1
   .Resize(Me.w, Me.H)
   .Image = im
 End With
 
 Me.Title = "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 per mostrare un file pdf mediante un Oggetto DocumentView posto sul Form:

Private DocumentView1 As DocumentView
Private doc As PdfDocument


Public Sub Form_Open()
 
 With Me
   .Height = Desktop.H
   .Width = Desktop.W
   .Arrangement = Arrange.Vertical
 End With
 
 doc = New PdfDocument("/percorso/del/file.pdf")
 
 With DocumentView1 = New DocumentView(Me) As "DocumentView1"
   .column = 1
   .Count = doc.Count
   .Expand = True
 End With
  
End


Public Sub DocumentView1_Draw(Page As Integer, Width As Integer, Height As Integer)
 
 Paint.DrawImage(doc[Page].Render(0, 0, Width * 2, Height * 2, 0, doc.Resolution * 4), 0, 0, Paint.Width, Paint.Height)
  
End

Mostriamo un secondo esempio: [nota 1]

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 terzo esempio applicando lo ZOOM con uno "Slider":

Private doc As PdfDocument


Public Sub Form_Open()
 
 doc = New PdfDocument("/percorso/del/file.pdf", Null, Null)
 
 With Me
   .W = Screen.AvailableWidth
   .H = Screen.AvailableHeight * 0.95
 End With
 
 With DocumentView1
   .X = 0
   .Y = 0
   .W = Me.W * 0.9
   .H = Me.H
   .Count = doc.Count
   .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[Page].Render(0, 0, Width, Height, 0, (doc.Resolution / 3) * DocumentView1.Zoom), 0, 0, Paint.Width, Paint.Height)
 
' Adatta le dimensioni dell'Oggetto "DocumentView" a quelle zoommate della pagina mostrata:
 With DocumentView1
   .W = Width
   .H = Height
 End With
 
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