Codice essenziale per mostrare un file PDF con le risorse del Componente gb.poppler
Da Gambas-it.org - Wikipedia.
(Reindirizzamento da Codice essenziale per mostrare un file PDF)
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