Objective-C get list of all custom papers

561 views Asked by At

I'm working on a little framework and I need to get a list of all available custom papers

In the Core Printing Reference there's only a method called PMPrinterGetPaperList, but it doesn't return custom paper:

This function obtains a list of the papers that a given printer claims to support.
The paper list does not include any custom paper sizes that may be available

If I open a pdf-document and then open the print-dialog I can create and select custom papers.

As summary I want to get a/the list of custom papers which I can create with print-dialog and with Objective-C method PMPaperCreateCustom

It has to be in Objective-C.

Any ideas? Thanks

Edit1:

Here is a snippet based on the first answer:

        for (int i = 0; i < printerCount; i++)
    {
        CFStringRef currentPrinterName;
        currentPrinter = (PMPrinter) CFArrayGetValueAtIndex(printerList, i);
        currentPrinterName = PMPrinterGetName(currentPrinter);

        if ([(NSString *) currentPrinterName caseInsensitiveCompare:printerName] == NSOrderedSame)
        {
            error = PMSessionCreatePageFormatList([[NSPrintInfo sharedPrintInfo] PMPrintSession], currentPrinter, &pageFormatList);

            if (error != noErr)
            {
                // TODO
            }

            NSUInteger pageCount = CFArrayGetCount(pageFormatList);

            for (int n = 0; n < pageCount; n++)
            {
                currentPage = (PMPageFormat) CFArrayGetValueAtIndex(pageFormatList, n);

                error = PMGetPageFormatPaper(currentPage, &currentPaper);

                if (error != noErr)
                {
                    // TODO
                }

                if (PMPaperIsCustom(currentPaper))
                {
                    NSLog(@"It's custom");
                }
            }

            break;
        }

        currentPrinter = NULL;
    }

But I only get the list of 'normal' papers and no custom ones.

On print dialog I created a custom paper (see screenshot) and this should be in the list.

Screenshot:

Screenshot xcode

2

There are 2 answers

0
marc-medley On

Since the custom papers list is apparently not (currently) accessible via Core Printing and Cocoa Printing …

One could use Objective-C to read the plist which stores a dictionary of the custom papers created by the user:

~/Library/Preferences/com.apple.print.custompapers.plist

Then, use subsequently use PMPaperCreateCustom as needed.

0
Ken Thomases On

It might work to use PMSessionCreatePageFormatList() to get all of the page formats, enumerate them, and, for each, call PMGetPageFormatPaper() to get its paper. You can use PMPaperIsCustom() to find out if each is a custom paper.