Created
January 21, 2014 20:24
-
-
Save lv10/8547663 to your computer and use it in GitHub Desktop.
Fetch Paper Names and Page Sizes for a Printer - Obj-C/C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================ | |
// COPrinterPaperSizes.h | |
// ================================ | |
#import <Foundation/Foundation.h> | |
#define PAPER_NAME @"paper_name" | |
#define PAPER_SIZE @"paper_size" | |
@interface COPrinterPaperSizes: NSObject | |
// returns an NSArray of NSDictionaries: see constants above. | |
+ (NSArray *)paperSizesAndNamesForPrinter: (NSString *)printerName; | |
+ (NSArray *)extractInfoFromPrinter: (PMPrinter)printerInfo; | |
@end | |
// =========================== | |
// COPrinterPaperSizes.m | |
// =========================== | |
#import "COPrinterPaperSizes.h" | |
@implementation COPrinterPaperSizes | |
+(NSArray*)paperSizesAndNamesForPrinter:(NSString *)thePrinterName; | |
{ | |
CFArrayRef printerList = NULL; | |
PMPrinter thisPrinter = NULL; | |
OSStatus err; | |
@try { | |
// get a list of all printers | |
err = PMServerCreatePrinterList(kPMServerLocal, &printerList); | |
if (err != noErr) | |
{ | |
[NSException raise:@"Exception: Can't create printer list" | |
format:@"An error ocurred while creating a list of printers"]; | |
} | |
int numPrinters = CFArrayGetCount(printerList); | |
for (int i=0; i < numPrinters; i++) | |
{ | |
CFStringRef thisPrinterName; | |
thisPrinter = (PMPrinter)CFArrayGetValueAtIndex(printerList, i); | |
thisPrinterName = PMPrinterGetName(thisPrinter); | |
if ([(NSString *)thisPrinterName caseInsensitiveCompare:thePrinterName] == NSOrderedSame) | |
{ | |
return [COPrinterPaperSizes extractInfoFromPrinter: thisPrinter]; | |
} | |
thisPrinter = NULL; | |
} | |
} | |
@finally | |
{ | |
// Clean up | |
if (printerList != NULL) | |
{ | |
CFRelease(printerList); | |
} | |
} | |
} | |
+ (NSArray *)extractInfoFromPrinter:(PMPrinter)printerInfo | |
{ | |
NSMutableArray *out = [NSMutableArray arrayWithCapacity:20]; | |
CFStringRef paperName = NULL;//, localized = NULL; | |
NSMutableDictionary *paperProps; | |
CFArrayRef paperList; | |
PMPaper thisPaper; | |
OSStatus err; | |
// Get a list of paper sizes and create an NSArray | |
err = PMPrinterGetPaperList(printerInfo, &paperList); | |
if (err != noErr) | |
{ | |
[NSException raise:@"Exception: Can't get paper list" | |
format:@"An error ocurred while getting a list of papers"]; | |
} | |
@try | |
{ | |
int numPapers = CFArrayGetCount(paperList); | |
for (int i=0; i < numPapers; i++) | |
{ | |
NSValue *paperSize; | |
double paperHeight, paperWidth; | |
thisPaper = (PMPaper)CFArrayGetValueAtIndex(paperList, i); | |
// Get the paper non-localized papername | |
PMPaperGetID(thisPaper, &paperName); | |
// Build up the size | |
err = PMPaperGetHeight(thisPaper, &paperHeight); | |
if (err != noErr) | |
{ | |
[NSException raise:@"Execption: Can't get paper height" | |
format:@"An error ocurred while getting the paper height"]; | |
} | |
err = PMPaperGetWidth(thisPaper, &paperWidth); | |
if (err != noErr) | |
{ | |
[NSException raise:@"Execption: Can't get paper width" | |
format:@"An error ocurred while getting the paper width"]; | |
} | |
paperSize = [NSValue valueWithSize:NSMakeSize(paperWidth, paperHeight)]; | |
// Create a dict with the values then add them to the array. | |
paperProps = [NSMutableDictionary dictionaryWithCapacity:2]; | |
[paperProps setValue:[NSString stringWithString:(NSString *)paperName] | |
forKey:PAPER_NAME]; | |
[paperProps setValue: paperSize forKey:PAPER_SIZE]; | |
[out addObject:paperProps]; | |
paperName = NULL; | |
} | |
} | |
@finally | |
{ | |
// Clean up | |
if (paperName) | |
{ | |
CFRelease(paperName); | |
} | |
} | |
return out; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment