Platforms to show: All Mac Windows Linux Cross-Platform

NewCPMPrintSessionMBS as CPMPrintSessionMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
global method Printing MBS MacCG Plugin 2.7 ✅ Yes ❌ No ❌ No ❌ No Desktop only
Creates a new print session object.
Example
'get a print session

// print this PDF
dim pathPrinted as FolderItem=GetFolderItem("test.pdf")

dim thePrintSession as CPMPrintSessionMBS = NewCPMPrintSessionMBS
if thePrintSession = nil then Return

'get default page format and print settings and attach it to the print settings
dim thePageFormat as CPMPageFormatMBS = NewCPMPageFormatMBS
dim thePrintSettings as CPMPrintSettingsMBS = NewCPMPrintSettingsMBS
thePrintSession.DefaultPageFormat thePageFormat
thePrintSession.DefaultPrintSettings thePrintSettings

'show the print dialog
if not thePrintSession.PrintDialog(thePrintSettings,thePageFormat) then return

'open the file which will be printed
dim thePdfDocument as CGPDFDocumentMBS = pathPrinted.OpenAsCGPDFDocumentMBS

' limit page counts to the one we have
dim LastPage as Integer = thePdfDocument.PageCount
if thePrintSettings.LastPage<lastpage then
lastpage=thePrintSettings.LastPage
end if

' you get better progress bar if you tell how many pages will come
thePrintSettings.LastPage=lastpage

'begin the printing
thePrintSession.BeginDocument(thePrintSettings, thePageFormat)

'loop over the number of copies
for currentCopy as Integer = 1 to thePrintSettings.Copies

'loop over the pages
for currentPage as Integer = thePrintSettings.FirstPage to LastPage

'prepage the page
dim PrintRect as CPMRectMBS =thePageFormat.AdjustedPageSize
dim CGRect as CGRectMBS =CGMakeRectMBS(PrintRect.left, PrintRect.top, PrintRect.Width, PrintRect.Height)
thePrintSession.BeginPage(thePageFormat, nil)
dim thePrintContext as CGContextMBS = thePrintSession.PageContext
if thePrintContext = Nil then return

'print the page
thePrintContext.DrawCGPDFDocument thePdfDocument, CGRect, currentPage

'end the page
thePrintContext = nil
thePrintSession.EndPage
next

next

'end the printing
thePrintSession.EndDocument

Returns nil on any error.

Some examples using this global method:

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


The biggest plugin in space...