1. #import "AppDelegate.h"
  2.  
  3. @implementation AppDelegate
  4.  
  5. @synthesize window = _window;
  6.  
  7. @synthesize textView;
  8. @synthesize pageButton;
  9.  
  10. - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  11. {
  12.     [pageButton setState:NSOffState];
  13.     pageIt = FALSE;
  14. }
  15.  
  16. - (IBAction)switchPage:(id)sender
  17. {
  18.     if (pageIt)
  19.         pageIt = FALSE;
  20.     else
  21.         pageIt = TRUE;
  22.    
  23.     NSLog(@"%d",pageIt);
  24.    
  25. }
  26.  
  27. - (IBAction)exportPDF:(id)sender
  28. {
  29.     NSArray *ext = [NSArray arrayWithObjects: @"pdf",nil];
  30.     NSSavePanel *panel = [NSSavePanel savePanel];
  31.     [panel setAllowedFileTypes:ext];
  32.     [panel beginSheetModalForWindow:_window
  33.                   completionHandler:^(NSInteger result) {
  34.                       if (result == NSOKButton){
  35.                          
  36.                           if (pageIt) {
  37.                              
  38.                               NSPrintInfo *printInfo;
  39.                               NSPrintInfo *sharedInfo;
  40.                               NSPrintOperation *printOp;
  41.                               NSMutableDictionary *printInfoDict;
  42.                               NSMutableDictionary *sharedDict;
  43.                              
  44.                               sharedInfo = [NSPrintInfo sharedPrintInfo];
  45.                               sharedDict = [sharedInfo dictionary];
  46.                               printInfoDict = [NSMutableDictionary dictionaryWithDictionary: sharedDict];
  47.                              
  48.                               [printInfoDict setObject:NSPrintSaveJob
  49.                                                 forKey:NSPrintJobDisposition];
  50.                              
  51.                               [printInfoDict setObject:[panel URL] forKey:NSPrintSavePath];
  52.                              
  53.                               printInfo = [[NSPrintInfo alloc] initWithDictionary:printInfoDict];
  54.                               [printInfo setHorizontalPagination: NSAutoPagination];
  55.                               [printInfo setVerticalPagination: NSAutoPagination];
  56.                               [printInfo setVerticallyCentered:NO];
  57.                              
  58.                               printOp = [NSPrintOperation printOperationWithView:textView
  59.                                                                        printInfo:printInfo];
  60.                              
  61.                               [printOp setShowsProgressPanel:NO];
  62.                               [printOp runOperation];
  63.  
  64.                           } else {
  65.                              
  66.                               NSRect r = [textView bounds];
  67.                               NSData *data = [textView dataWithPDFInsideRect:r];
  68.                               [data writeToURL:[panel URL] atomically:YES];
  69.                              
  70.                           }
  71.                          
  72.                          
  73.                          
  74.                           NSLog(@"We actually saved");
  75.                          
  76.                           NSURL *fileURL = [panel URL];
  77.                          
  78.                           NSString *saveName = [fileURL path];
  79.                           NSLog(@"Saved as filename = %@",saveName);
  80.                          
  81.                       } else {
  82.                           NSLog(@"Did not save");
  83.                       }
  84.  
  85.                   }];
  86. }
  87. @end
  88.  
  89.