How many ways to we have to come from a picture to a PDF in Xojo with MBS Xojo Plugins?
Let's see how many we find:
GraphicsMagick
We use GMImageMBS class to load the picture and save it to a file.
Sub test1(pic as Picture, file as FolderItem)
// load picture into GraphicsMagick
Dim g As New GMImageMBS(pic)
// have it write to a PDF file
g.write(file)
End Sub
This should work cross platform all platforms (macOS, iOS, Windows and Linux).
NSImageView print to PDF
If we load the picture into a NSImageViewMBS object. Without having the control on a window, we can still use NSPrintOperationMBS class to print to a PDF file.
Sub test2(pic as Picture, file as FolderItem)
// let's create an image view to hold our image
Dim imageView As New NSImageViewMBS
// with right size
imageView.frame = New NSRectMBS(0, 0, pic.width, pic.height)
// and load the picture into it
imageView.image = New NSImageMBS(pic)
// now we let it print to the PDF file
Dim pi As NSPrintInfoMBS = NSPrintInfoMBS.sharedPrintInfo
pi.SetSaveDestination(file)
Dim p As NSPrintOperationMBS = NSPrintOperationMBS.printOperationWithView(imageView)
p.showsPrintPanel = False
p.showsProgressPanel = False
Dim r As Boolean = p.runOperation
End Sub
This is macOS only.
CGPDFDocument class
CoreGraphics comes with a CGPDFDocumentMBS class, which can be used to draw to PDF documents on macOS.
Sub test3(pic as Picture, file as FolderItem)
// create new CoreGraphics PDF Document
Dim r As New CGRectMBS(0,0,pic.Width,pic.Height)
Dim output As CGContextMBS = file.NewCGPDFDocumentMBS(r,"SomeTitle","SomeAuthor","SomeCreator")
// Create a new page
output.BeginPage r
// draw picture to PDF page
Dim image As CGImageMBS = CGImageMBS.CreateImage(pic)
output.DrawPicture(image, r)
// cleanup
output.EndPage
output.Flush
End Sub
This should work on macOS and iOS.
PDFDocument class
There is a Cocoa class PDFDocumentMBS in PDFKit framework to create PDF documents. We can use it with PDFPageMBS class to add picture page to our new document.
Sub test4(pic as Picture, file as FolderItem)
// load image into NSImage
Dim image As New NSImageMBS(pic)
// now make a PDF Page with the image
Dim page As New PDFPageMBS(image)
// and create PDF document, so we can add page and write it to disk
Dim doc As New PDFDocumentMBS
doc.appendPage page
Call doc.write(file)
End Sub
This is macOS only. We could do iOS, but haven't yet.
ChartDirector
Since MBS Xojo ChartDirector Plugin can output PDF documents, we can just load the picture as background picture into a new chart.
Sub test5(pic as Picture, file as FolderItem)
// create a new chart
Dim c As New CDXYChartMBS(pic.Width, pic.Height)
// get the draw area
Dim d As CDDrawAreaMBS = c.getDrawArea
// load picture data into it
Call d.loadData(pic.ToData(Picture.Formats.PNG))
// and output chart as PDF
Call c.makeChart(file)
End Sub
This should work cross platform all platforms (macOS, iOS, Windows and Linux).
DynaPDF native
Of course our MBS Xojo DynaPDF Plugin can create a PDF with a picture. DynaPDF has the best options available, where you can control the filter used to store the image within the PDF document. Also you can control compression quality, colorspace and whether it gets scaled down.
Sub test6(pic as Picture, file as FolderItem)
// create new DynaPDF workspace
Dim d As New DynaPDFMBS
// create PDF with destination file
Call d.CreateNewPDF file
// add a page
Call d.Append
// change page size to picture size
Call d.SetPageWidth(pic.Width)
Call d.SetPageHeight(pic.Height)
// ask for JPEG compression at 80%
Call d.SetCompressionFilter(d.kcfJPEG)
Call d.SetJPEGQuality(80)
// and insert picture
Call d.InsertPicture(pic, 0, 0, pic.Width, pic.Height)
// close page and file
Call d.EndPage
Call d.CloseFile
End Sub
This will work cross platform on all platforms and just requires the Starter license, which was in OmegaBundle earlier this year.
DynaPDF with Graphics class
Since we got a Graphics class integration, we can also use that to draw the picture:
Sub test7(pic as Picture, file as FolderItem)
// new DynaPDF environment
Dim d As New DynaPDFMBS
// create new PDF and append a page
Call d.CreateNewPDF file
Call d.Append
// page size should match picture size
Call d.SetPageWidth(pic.Width)
Call d.SetPageHeight(pic.Height)
// ask for Graphics object, so we can draw pictue there
Dim g As Graphics = d.PageGraphics
g.DrawPicture pic, 0, 0, pic.Width, pic.Height, 0, 0, pic.Width, pic.Height
// cleanup
Call d.EndPage
Call d.CloseFile
End Sub
CGImageDestination class
And it turns out the CGImageDestinationMBS class can also output PDF, so here we go:
Sub test8(pic as Picture, file as FolderItem)
// we load picture into CGImage
Dim image As CGImageMBS = CGImageMBS.CreateImage(pic)
// and create a PDF writer
Dim dest As CGImageDestinationMBS = CGImageDestinationMBS.CreateWithFile(file, "com.adobe.pdf", 1)
// add image
dest.AddImage(image, Nil)
// now write file
If dest.Finalize Then
// okay
Else
MsgBox "Failed to save."
End If
End Sub
This should on macOS and iOS.
What is left? Well, we did leave away loading picture into a HTMLviewer to render PDF from there.
Which way is your preferred one?