Estrarre immagini da un file PDF con le risorse del API di libpoppler-glib e di libcairo
Da Gambas-it.org - Wikipedia.
Versione del 1 ago 2018 alle 09:27 di Vuott (Discussione | contributi) (Creata pagina con "Se un file PDF possiede delle immagini, queste possono essere estratte. E' necessario avere installata nel sistema e richiamare nel programma Gambas le librerie dinamiche con...")
Se un file PDF possiede delle immagini, queste possono essere estratte.
E' necessario avere installata nel sistema e richiamare nel programma Gambas le librerie dinamiche condivise: "libpoppler-glib.so" e " libcairo:2"
Mostriamo un semplice esempio:
Library "libpoppler-glib" 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 ' char * poppler_page_get_text (PopplerPage *page) ' Retrieves the text of page. Private Extern poppler_page_get_text(page As Pointer) 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" Private cairo_format As String[] = ["CAIRO_FORMAT_INVALID", "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 As String Dim p, doc, pag, cai As Pointer Dim w, h, err As Integer percorso = "/percorso/del/file.pdf" ' Carica il file PDF: doc = poppler_document_new_from_file("file://" & percorso, Null, VarPtr(p)) If doc = 0 Then Error.Raise("Error: " & poppler_error[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("Error !") ' 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("Error !") '''''''''''''''' ' Estrae 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" Print "Formato: "; cairo_format[cairo_image_surface_get_format(cai)] ' 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