Xojo Conferences
XDCMay2019MiamiUSA

Platforms to show: All Mac Windows Linux Cross-Platform

/MacCG/CoreGraphics PDF/Create PDF with Picture
Function:
Required plugins for this example: MBS MacCG Plugin, MBS Main Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /MacCG/CoreGraphics PDF/Create PDF with Picture
This example is the version from Sun, 17th Mar 2012.
Project "Create PDF with Picture.rbp"
Class App Inherits Application
Const kEditClear = "&Löschen"
Const kFileQuit = "Beenden"
Const kFileQuitShortcut = ""
End Class
Class Window1 Inherits Window
EventHandler Sub Open() dim w,h as integer const size=500.0 dim picFile as folderitem=CreatePicture // source picture file // open picture for dim p as picture=picfile.OpenAsPicture if p=nil then MsgBox "Failed to load picture." end if dim PDFFile as folderitem=SpecialFolder.Desktop.Child("Create PDF with Picture.pdf") dim r as new CGRectMBS // Create the context dim c as CGContextMBS=CGNewPDFDocumentMBS(PDFFile,r,"Picture","","") if c<>nil then w=p.width h=p.height if w>size or h>size then dim factor as Double factor=min(size/p.Height,size/p.Width) w=p.width*factor h=p.Height*factor end if r=new CGRectMBS(0,0,w,h) // We must begin a new page before drawing to a PDF context c.BeginPage(r) dim data as CGDataProviderMBS dim i as CGImageMBS data=CGDataProviderMBS.CreateWithFile(picFile) if data<>nil then // a CGImage based on the JPEG data i=CGCreateImageFromJPEGDataProviderMBS(data,nil,false,0) end if if i=nil then // a CGImage based on the uncompressed image data i=CGCreateImageMBS(p) end if c.DrawPicture(i, r) // We've finished rendering the page c.EndPage c.Flush c=nil // to force the file to be written as we want to launch it pdffile.Launch else MsgBox "Failed to creator file!" end if End EventHandler
Function CreatePicture() As FolderItem dim p as Picture = LogoMBS(500) dim f as FolderItem=SpecialFolder.Desktop.Child("logo.jpg") if f.Exists=false then f.SaveAsJPEG p end if Return f End Function
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
End Project

See also:

Feedback, Comments & Corrections

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




Links
MBS Xojo blog