Estrarre immagini da un file PDF con le risorse del API di libpoppler-glib e di libcairo

Da Gambas-it.org - Wikipedia.

Se un file PDF possiede delle immagini, queste possono essere estratte.

E' necessario avere installate nel sistema e richiamare nel programma Gambas le librerie condivise: "libpoppler-glib.so.8.26.0 " e "libcairo.so.2.11800.0 ".

Mostriamo un semplice esempio:

Private Const URI As String = "file://"


Library "libpoppler-glib:8.26.0"

Private poppler_error As String[] = [Null, "INVALID", "ENCRYPTED", "OPEN_FILE", "BAD_CATALOG", "DAMAGED"]

' popplerDocument *poppler_document_new_from_file (const char *uri, const char *password, GError **error)
' Creates a new PopplerDocument.
Private Extern poppler_document_new_from_file(uri As String, password As String, GError As Pointer) As Pointer

' PopplerPage * poppler_document_get_page (PopplerDocument *document, int index)
' Returns the PopplerPage indexed at index.
Private Extern poppler_document_get_page(document As Pointer, index As Integer) As Pointer

' cairo_surface_t * poppler_page_get_image (PopplerPage *page, gint image_id)
' Returns a cairo surface for the image of the page.
Private Extern poppler_page_get_image(page As Pointer, image_id As Integer) As Pointer

' void g_object_unref (gpointer object)
' Decreases the reference count of object.
Private Extern g_object_unref(gobject As Pointer)


Library "libcairo:2.11800.0"

Private cairo_format As String[] = ["CAIRO_FORMAT_ARGB32", "CAIRO_FORMAT_RGB24", "CAIRO_FORMAT_A8",
                                    "CAIRO_FORMAT_A1", "CAIRO_FORMAT_RGB16_565", "CAIRO_FORMAT_RGB30"]

' int cairo_image_surface_get_width (cairo_surface_t *surface)
' Get the width of the image surface in pixels.
Private Extern cairo_image_surface_get_width(surface As Pointer) As Integer

' int cairo_image_surface_get_height (cairo_surface_t *surface)
' Get the height of the image surface in pixels.
Private Extern cairo_image_surface_get_height(surface As Pointer) As Integer

' cairo_format_t cairo_image_surface_get_format (cairo_surface_t *surface)
' Get the format of the surface.
Private Extern cairo_image_surface_get_format(surface As Pointer) As Integer

' cairo_status_t cairo_surface_write_to_png(cairo_surface_t *surface, const char *filename)
' Writes the contents of surface to a new file filename as a PNG image.
Private Extern cairo_surface_write_to_png(cairo_surface As Pointer, filename As String) As Integer

' void cairo_surface_destroy (cairo_surface_t *surface)
' Decreases the reference count on surface by one.
Private Extern cairo_surface_destroy(cairo_surface As Pointer)


Public Sub Main()

 Dim percorso, en As String
 Dim p, doc, pag, cai As Pointer
 Dim w, h, frm, err As Integer
 
 percorso = "/percorso/del/file.pdf"
  
' Carica il file PDF:
 doc = poppler_document_new_from_file(URI & percorso, Null, VarPtr(p))
 If doc == 0 Then Error.Raise("Errore: " & poppler_error[Val(String@(p))])
  
' Prende in considerazione la prima pagina (indice 0) del file pdf:
 pag = poppler_document_get_page(doc, 0)
 If pag == 0 Then Error.Raise("Errore !")
  
' Estrae la prima immagine (indice 0) presente nella pagina del file pdf:
 cai = poppler_page_get_image(pag, 0)
 If cai == 0 Then Error.Raise("Errore !")
  
''''''''''''''''
   
' Ottiene alcune informazioni sull'immagine estratta:
 w = cairo_image_surface_get_width(cai)
 h = cairo_image_surface_get_height(cai)
 Print "Informazioni sull'immagine estratta"
 Print "Dimensioni: "; w; " x "; h; " pixel"
 frm = cairo_image_surface_get_format(cai)
 If frm = -1 Then
   en = "CAIRO_FORMAT_INVALID"
 Else
   en = cairo_format[frm]
 Endif
 Print "Formato:    "; en
  
' Salva i dati dell'immagine estratta in un'immagine di tipo PNG:
 err = cairo_surface_write_to_png(cai, "/tmp/immagine.png")
 If err > 0 Then Error.Raise("Errore nella creazione del file immagine .png !")
 
' Al termine libera la memoria precedentemente occupata:
 cairo_surface_destroy(cai)
 g_object_unref(pag)
 g_object_unref(doc)
  
End


Riferimenti