Platforms to show: All Mac Windows Linux Cross-Platform

/DynaPDF/Image to PDF/Batch Image to PDF


Required plugins for this example: MBS DynaPDF Plugin

You find this example project in your Plugins Download as a Xojo project file within the examples folder: /DynaPDF/Image to PDF/Batch Image to PDF

This example is the version from Thu, 6th Apr 2016.

Project "Batch Image to PDF.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
EventHandler Sub Open() // pick some folder with images ' GetFolderItem("C:\Bilder\JPG", FolderItem.PathTypeShell) dim folder as FolderItem = SpecialFolder.Pictures dim x as double = 50.0 dim y as double = 50.0 dim p as new MyDynaPDFMBS dim destfile as FolderItem = SpecialFolder.Desktop.Child("Batch Image to PDF.pdf") call p.CreateNewPDF(destfile) call p.SetPageCoords(p.kpcTopDown) call p.SetJPEGQuality(70) call p.SetResolution(300) 'call p.SetCompressionFilter(p.kcfFlate) 'call p.SetSaveNewImageFormat(false) dim t as integer = ticks picCount = 0 Process p, folder, x, y t = ticks -t if piccount > 0 then call p.EndPage else MsgBox "No pictures found in your pictures folder." end if call p.CloseFile p = nil destfile.Launch(true) MsgBox str(t/60)+" seconds for "+str(picCount)+" picture files." quit End EventHandler
Sub AddPicture(p as DynaPDFMBS, file as FolderItem, byref x as double, byref y as double) // get file extension dim parts(-1) as string = split(file.name, ".") dim extension as string = parts(UBound(parts)) dim count as integer = 1 if extension = "tif" or extension = "tiff" then count = p.GetImageCount(file) if count<0 then Return end if for i as integer = 1 to count dim w,h,bits as integer dim zip as Boolean // check image format if not p.ReadImageFormat2(file, 1, w, h, bits, zip) then Return if x = 50 and y = 50 then call p.Append end if PlaceImageCentered p, x, y, 100, 100, file, i x = x + 130 if x > 450 then x = 50 y = y + 130 if y>700 then call p.EndPage y = 50 end if end if next End Sub
Sub PlaceImageCentered(p as DynaPDFMBS, PosX as double, PosY as Double, Width as double, Height as double, file as FolderItem, index as integer) dim x,y as Double dim bits as integer dim imgWidth, imgHeight as integer dim useZip as Boolean if not p.ReadImageFormat2(file, Index, imgWidth, imgHeight, bits, useZip) then Return if bits = 1 then call p.SetCompressionFilter(p.kcfCCITT4) elseif useZip then call p.SetCompressionFilter(p.kcfFlate) else call p.SetCompressionFilter(p.kcfJPEG) end if dim w as double = imgWidth dim h as double = imgHeight dim sx as double = Width / w if (h * sx <= Height) then h = h * sx x = PosX y = PosY + (Height - h) * 0.5 call p.InsertImageEx(x, y, Width, 0.0, File, Index) else sx = Height / h w = w * sx x = PosX + (Width - w) * 0.5 y = PosY call p.InsertImageEx(x, y, 0.0, Height, File, Index) end if picCount = picCount + 1 End Sub
Sub Process(p as DynaPDFMBS, folder as FolderItem, byref x as double, byref y as double) dim c as integer = folder.Count for i as integer = 1 to c dim file as FolderItem = folder.TrueItem(i) if file = nil then // ignore elseif file.Directory then Process p,file, x, y // recurively walk through all subfolders else AddPicture p, file, x, y end if next End Sub
Property picCount As Integer
End Class
MenuBar MenuBar1
MenuItem FileMenu = "&Ablage"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Bearbeiten"
MenuItem EditUndo = "&Rückgängig"
MenuItem UntitledMenu1 = "-"
MenuItem EditCut = "&Ausschneiden"
MenuItem EditCopy = "&Kopieren"
MenuItem EditPaste = "&Einfügen"
MenuItem EditClear = "#App.kEditClear"
MenuItem UntitledMenu0 = "-"
MenuItem EditSelectAll = "&Alles auswählen"
End MenuBar
Class MyDynaPDFMBS Inherits DynaPDFMBS
EventHandler Function Error(ErrorCode as integer, ErrorMessage as string, ErrorType as integer) As integer // ignore errors with unsupported formats if ErrorCode = 45 then Return 0 // ignore errors with image errors if ErrorCode = 46 then Return 0 // 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...