Codice essenziale per mostrare un file PDF con le risorse del Componente gb.poppler
Da Gambas-it.org - Wikipedia.
Versione del 26 lug 2024 alle 17:05 di Vuott (Discussione | contributi)
Per mostrare un documento PDF in Gambas, 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 Const RISOLUZIONE As Integer = 150 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 ' 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, pd[0].Render().W * (RISOLUZIONE / 25.4), pd[0].Render().H * (RISOLUZIONE / 25.4), 0, RISOLUZIONE) ' 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:
Private Const RISOLUZIONE As Integer = 150 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 * (RISOLUZIONE / 25.4), Height * (RISOLUZIONE / 25.4), 0, RISOLUZIONE), 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 Const RISOLUZIONE As Integer = 150 Private DocumentView1 As DocumentView Private doc As PdfDocument Private Slider1 As Slider 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 = New DocumentView(Me) As "DocumentView1" .X = 0 .Y = 0 .W = Me.W * 0.9 .H = Me.H .Count = doc.Count .Zoom = 2 End With With Slider1 = New Slider(Me) As "Slider1" .X = Me.W * 0.95 .Y = Me.H * 0.3 .H = Me.H * 0.5 .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 * (doc.Resolution / 3), Height * (doc.Resolution / 3), 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