|
|
(2 versioni intermedie di uno stesso utente non sono mostrate) |
Riga 1: |
Riga 1: |
− | Per mostrare un documento ''PDF'', bisognerà utilizzare le risorse del Componente <FONT Color=#B22222>''gb.poppler''</font>.
| + | #REDIRECT [[Codice essenziale per mostrare un file PDF con 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''.
| |
− | <BR>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
| |
− |
| |
− | <FONT color=gray>' ''Carica il file PDF del documento da mostrare:''</font>
| |
− | pd = New PdfDocument("<FONT color=gray>''/percorso/del/file.pdf''</font>")
| |
− |
| |
− | '''End'''
| |
− |
| |
− |
| |
− | <FONT color=gray>' ''Cliccando sulla "ImageView" viene mostrata la pagina corrente del documento PDF''
| |
− | ' ''Cliccando ancora si passa alla eventuale pagina successiva.''</font>
| |
− | '''Public''' Sub ImageView1_MouseUp()
| |
− |
| |
− | Dim im As Image
| |
− | Dim w, h As Short
| |
− |
| |
− | w = pd[0].Render().W
| |
− | h = pd[0].Render().H
| |
− |
| |
− | <FONT color=gray>' ''Modifica la risoluzione e dunque la dimensione della pagina corrente del documento PDF e la converte in una "Image":''</font>
| |
− | 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>
| |
− | With ImageView1
| |
− | .Resize(Me.w, Me.H)
| |
− | .Image = im
| |
− | End With
| |
− |
| |
− | Me.Caption = "Pagina: " & CStr(c + 1)
| |
− |
| |
− | Inc c
| |
− |
| |
− | <FONT color=gray>' ''Se si clicca sull'immagine dell'ultima pagina del PDF, si torna a mostrare la prima pagina:''</font>
| |
− | 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("<FONT color=gray>''/percorso/del/file.pdf''</font>")
| |
− |
| |
− | 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, Height, 0, doc.Resolution * 2), 0, 0, Paint.Width, Paint.Height)
| |
− |
| |
− | '''End'''
| |
− | | |
− | | |
− | Mostriamo un secondo esempio: <SUP>[[[#Note|Nota 1]]]</sup>
| |
− | 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("<FONT Color=gray>''/percorso/del/file.pdf''</font>", 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)
| |
− |
| |
− | <FONT color=gray>' ''Adatta le dimensioni dell'Oggetto "DocumentView" a quelle zoommate della pagina mostrata:''</font>
| |
− | 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 [https://www.gambas-it.org/smf/index.php?action=profile;u=1249 Gianluigi] del forum-it.org
| |