Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Extract images

Required plugins for this example: MBS DynaPDF Plugin, MBS Util Plugin, MBS Picture Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Extract images

This example is the version from Sun, 6th Mar 2021.

Project "Extract images.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open() Dim f As FolderItem = GetOpenFolderItem(MyFileTypes.Pdf) if f=nil then quit end if dim p as new MyDynapdfMBS p.SetLicenseKey "Pro" // For this example you can use a Pro or Enterprise License call p.CreateNewPDF(nil) // Skip anything that is not required call p.SetImportFlags p.kifContentOnly+p.kifImportAsPage // From which PDF file do you want to extract the images? call p.OpenImportFile(f, p.kptOpen, "") // Comment this out if you want to extract the images from specific pages only call p.ImportPDFFile(1, 1.0, 1.0) // Uncomment this section if you want to extract images from specific pages only 'dim pageCount as integer = p.GetInPageCount 'for i as integer = 1 to pageCount ' call p.ImportPDFPage(i) 'next call p.CloseImportFile dim stack as new MyDynaPDFParseInterfaceMBS stack.dest=SpecialFolder.desktop // destination folder stack.pdf=p 'If you want to create a multipage TIFF then create the output image here 'and call AddImage() only in the callback function. After the loop 'returns call CloseImage() to close the image file. //CreateImage(f, p.kifmTIFF); dim pageCount as integer = p.GetPageCount for i as integer=1 to pageCount call p.EditPage i 'If you want to convert the images into a specific color space then set 'one of the folowing flags (see also TParseFlags in dynapdf.h): ' 'p.kpfDitherImagesToBW // Floyd-Steinberg dithering. 'p.kpfConvImagesToGray 'p.kpfConvImagesToRGB 'p.kpfConvImagesToCMYK ' 'Only one color space conversion flag must be set at time. call p.ParseContent(stack, p.kpfDecomprAllImages) call p.EndPage next // p.CloseImage // Call this function here if you create a multipage TIFF. End EventHandler
End Class
Class Window1 Inherits Window
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyDynaPDFParseInterfaceMBS Inherits DynaPDFParseInterfaceMBS
EventHandler Function BeginTemplate(ObjectPtr as integer, Handle as integer, BBox as DynaPDFRectMBS, Matrix as DynaPDFMatrixMBS) As integer System.DebugLog "Begin Template" End EventHandler
EventHandler Function InsertImage(image as DynaPDFImageMBS) As integer Dim ImageFile As FolderItem = ProcessImage(image) Dim MaskFile As FolderItem // check if we have soft mask to extract: If image.ISoftMaskHandle >= 0 Then Dim img As DynaPDFImageMBS = pdf.GetImageObj(image.ISoftMaskHandle, pdf.kpfDecomprAllImages) If img <> Nil Then MaskFile = ProcessImage(img) end if End If // check if we have mask to extract: If image.IMaskImageHandle >= 0 Then Dim img As DynaPDFImageMBS = pdf.GetImageObj(image.IMaskImageHandle, pdf.kpfDecomprAllImages) If img <> Nil Then MaskFile = ProcessImage(img) End If End If If MaskFile <> Nil And ImageFile <> Nil Then // let's try to apply mask Dim pic As Picture = Picture.Open(ImageFile) Dim mask As Picture = Picture.Open(MaskFile) If pic <> Nil And mask <> Nil Then mask = mask.InvertMBS // combine picture and mask Dim newPicture As New Picture(pic.Width, pic.Height, 32) newPicture.Graphics.DrawPicture pic, 0, 0, pic.Width, pic.Height, 0, 0, pic.Width, pic.Height newPicture.mask.Graphics.DrawPicture mask, 0, 0, pic.Width, pic.Height, 0, 0, mask.Width, mask.Height // save it Dim FileName As String = ImageFile.NameWithoutExtensionMBS + ".png" Dim File As FolderItem = ImageFile.Parent.Child(FileName) newPicture.Save(File, newPicture.SaveAsPNG, newPicture.QualityHigh) End If End If End EventHandler
Function ProcessImage(Image as DynaPDFImageMBS) As FolderItem Dim b As BinaryStream Dim f As FolderItem System.DebugLog "Insert Image" counter = counter + 1 Select Case Image.filter Case DynapdfMBS.kdfDCTDecode // JPEG f=dest.Child(Str(Counter)+".jpg") b=f.CreateBinaryFile("") If b<>Nil Then b.Write image.Buffer b.Close Return f End If Case DynapdfMBS.kdfJPXDecode // JPEG 2000 f=dest.Child(Str(Counter)+".jp2") b=f.CreateBinaryFile("") If b<>Nil Then b.Write image.Buffer b.Close Return f End If Else // anything else f=dest.Child(Str(Counter)+".tif") If pdf.CreateImage(f, pdf.kifmTIFF) Then Call pdf.addimage(pdf.kcfLZW, pdf.kicNone, image) Call pdf.CloseImage Return f End If End Select End Function
Property Counter As Integer
Property dest As folderitem
Property pdf As dynaPDFmbs
End Class
Filetype application/pdf
End MyFileTypes
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer // output all messages on the console: System.DebugLog str(ErrorCode)+": "+ErrorMessage // and display dialog: Dim d as New MessageDialog //declare the MessageDialog object Dim b as MessageDialogButton //for handling the result d.icon=MessageDialog.GraphicCaution //display warning icon d.ActionButton.Caption="Continue" d.CancelButton.Visible=True //show the Cancel button // a warning or an error? if BitAnd(ErrorType, me.kE_WARNING) = me.kE_WARNING then // if user decided to ignore, we'll ignore if IgnoreWarnings then Return 0 d.Message="A warning occurred while processing your PDF code." // we add a third button to display all warnings d.AlternateActionButton.Caption = "Ignore warnings" d.AlternateActionButton.Visible = true else d.Message="An error occurred while processing your PDF code." end if d.Explanation = str(ErrorCode)+": "+ErrorMessage b=d.ShowModal //display the dialog Select Case b //determine which button was pressed. Case d.ActionButton Return 0 // ignore Case d.AlternateActionButton IgnoreWarnings = true Return 0 // ignore Case d.CancelButton Return -1 // stop End select End EventHandler
Property IgnoreWarnings As Boolean
End Class
End Project

See also:

The items on this page are in the following plugins: MBS DynaPDF Plugin.

The biggest plugin in space...