// // KMPDFPosterPrintWindowController.m // PDF Reader Pro Edition // // Created by 丁林圭 on 2018/4/3. // #import "PDF_Reader_Pro-Swift.h" #import "KMPDFPosterPrintWindowController.h" #import "KMPosterPrintManager.h" //#import "TextFieldFormatter.h" //#import "KMFileAttribute.h" //#import "KMBookletMaskView.h" //#import "PasswordWindowController.h" //#import "KMSavePanelAccessoryController.h" //#import "NSWindowController_SKExtensions.h" //#import "KMPageSizeTool.h" #define maxPrintWidth 300 #define kPrintFolderPath [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] #define kPosterPrintFolderPath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"PosterPrint"] #define kPosterPrintFileSavePath [[[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier] stringByAppendingPathComponent:@"PosterPrint.pdf"] #pragma mark KMPDFPosterPrintContentView @interface KMPDFPosterPrintContentView : NSView @property (nonatomic,retain) NSArray *horizontals; @property (nonatomic,retain) NSArray *verticals; @property (nonatomic,assign) CGFloat overlap; @property (nonatomic,assign) CGFloat spaceWidth; @property (nonatomic,assign) CGFloat spaceHeight; @property (nonatomic,assign) CGFloat scanle; @end @implementation KMPDFPosterPrintContentView -(void)dealloc { } -(void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];// 获取画布 CGFloat width = self.bounds.size.width; CGFloat height = self.bounds.size.height; if (_horizontals.count> 0 || _verticals.count > 0) { NSArray *hourArray = _horizontals; NSArray *vertArray = _verticals; [NSGraphicsContext saveGraphicsState]; if (context) { [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]]; } CGContextSetStrokeColorWithColor(context, [NSColor colorWithRed:51.0/255.0 green:186.0/255.0 blue:234.0/255.0 alpha:1.0].CGColor); CGContextSetLineWidth(context, 1.0); for (NSUInteger i =0; i< vertArray.count -1; i ++) { CGContextMoveToPoint(context, 0,height - ([vertArray[i] floatValue] /_scanle + _spaceHeight)); CGContextAddLineToPoint(context,width, height - ([vertArray[i] floatValue] /_scanle +_spaceHeight)); } for (NSUInteger j =0; j< hourArray.count - 1; j ++) { CGContextMoveToPoint(context, [hourArray[j] floatValue]/_scanle + _spaceWidth, 0); CGContextAddLineToPoint(context,[hourArray[j] floatValue]/_scanle + _spaceWidth, height); } CGFloat arr[] = {8,1}; CGContextSetLineDash(context, 0, arr, 1); CGContextDrawPath(context, kCGPathStroke); [NSGraphicsContext restoreGraphicsState]; } } @end #pragma mark -KMPDFPosterPrintWindowController @interface KMPDFPosterPrintWindowController () @property (nonatomic,retain) PDFDocument *PDFDocument; @property (nonatomic,retain) NSString *password; @property (nonatomic,retain) KMPDFPosterPrint *PDFPrint; @property (nonatomic,assign) IBOutlet NSTextField *currentPageLabel; @property (nonatomic,assign) IBOutlet NSTextField *totalPageLabel; @property (assign) IBOutlet NSTextField *scaleTextField; @property (assign) IBOutlet NSButton *cutMarksButton; @property (assign) IBOutlet NSButton *labelButton; @property (assign) IBOutlet NSButton *portraitButton; @property (assign) IBOutlet NSButton *landscapeButton; @property (nonatomic,assign) IBOutlet NSTextField *pageRangeLabel; @property (nonatomic,assign) IBOutlet NSComboBox *pageRangeComboBox; @property (nonatomic,assign) IBOutlet NSTextField *splitSizeLabel; @property (nonatomic,assign) IBOutlet NSComboBox *splitSizeComboBox; @property (nonatomic,assign) IBOutlet NSTextField *overlapLabel; @property (nonatomic,assign) IBOutlet NSTextField *overlapUnit; @property (assign) IBOutlet NSTextField *overlapTextField; @property (assign) IBOutlet NSTextField *sizeLabel; @property (assign) IBOutlet NSTextField *zoomLabel; @property (nonatomic,assign) IBOutlet NSComboBox *unitComboBox; @property (nonatomic,assign) IBOutlet NSImageView *thumImageView; @property (assign) IBOutlet NSLayoutConstraint *PDFHeight; @property (assign) IBOutlet NSLayoutConstraint *PDFWidth; @property (nonatomic,assign) IBOutlet NSView *PDFContentView; @property (assign) IBOutlet NSLayoutConstraint *contentHeight; @property (assign) IBOutlet NSLayoutConstraint *contentWidth; @property (assign) IBOutlet NSButton *cancelButton; @property (assign) IBOutlet NSButton *saveButton; @property (assign) IBOutlet NSButton *printButton; @property (assign,nonatomic) IBOutlet NSTextField *orientationLabel; @property (assign,nonatomic) IBOutlet NSTextField *unitLabel; @property (assign,nonatomic) IBOutlet NSTextField *titleScanlLabel; @property (assign,nonatomic) IBOutlet NSTextField *labelTextField; @property (assign,nonatomic) IBOutlet NSButton *reverseButton; @property (nonatomic,assign) IBOutlet NSTextField *commentsFormsLabel; @property (nonatomic,assign) IBOutlet NSComboBox *commentsFormsComboBox; @property (nonatomic,assign) IBOutlet NSTextField *pagesPerLabel; @property (nonatomic,assign) IBOutlet NSComboBox *selectPerComboBox; @property (assign,nonatomic) IBOutlet NSTextField *lineCountText; @property (assign,nonatomic) IBOutlet NSTextField *columnCountText; @property (assign,nonatomic) IBOutlet NSTextField *filePageSizeLabel; @property (assign,nonatomic) IBOutlet NSComboBox *filePageSizeBox; @property (assign) IBOutlet NSTextField *pageWidthTextField; @property (assign) IBOutlet NSTextField *pageHeightTextField; @property (assign) IBOutlet NSTextField *unitTLabel; @property (assign) IBOutlet NSTextField *paperWidthTextField; @property (assign) IBOutlet NSTextField *paperHeightTextField; @property (assign) IBOutlet NSTextField *unitT1Label; @property (assign) IBOutlet NSTabView *tableView; @property (nonatomic,retain) KMPDFPosterPrintContentView *preview; @property (nonatomic, retain) KMBookletMaskView *posterMaskView; @property (nonatomic,assign) NSUInteger currentPageIndex; @property (nonatomic,retain) NSArray * pages;; @end @implementation KMPDFPosterPrintWindowController - (void)dealloc { NSFileManager *manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath:kPosterPrintFileSavePath]) { [manager removeItemAtPath:kPosterPrintFileSavePath error:nil]; } } - (id)initWithPDFDocument:(PDFDocument *)pdfDocument { if (self = [super initWithWindowNibName:@"KMPDFPosterPrintWindowController"]) { [self showWaitting]; NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[pdfDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]]; [self.labelTextField.cell setPlaceholderString:contextString]; dispatch_async(dispatch_get_global_queue(0, 0), ^{ if (![[NSFileManager defaultManager] fileExistsAtPath:kPrintFolderPath]) { if (![[NSFileManager defaultManager] createDirectoryAtPath:kPrintFolderPath withIntermediateDirectories:NO attributes:nil error:nil]) { return; } } [pdfDocument writeToURL:[NSURL fileURLWithPath:kPosterPrintFileSavePath]]; self.PDFDocument = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:kPosterPrintFileSavePath]]; dispatch_async(dispatch_get_main_queue(), ^{ [self hideWaitting]; if (self.PDFDocument) { [self getDrawPages]; [self reloadPDFData]; [self reloadPDFView]; }else{ [self close]; } }); }); } return self; } - (id)initWithPDFFilePath:(NSString *)filePath password:(nullable NSString *)password{ if (self = [super initWithWindowNibName:@"KMPDFPosterPrintWindowController"]) { PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:filePath]]; self.PDFDocument = pdf; self.password = password; } return self; } - (BOOL)windowShouldClose:(id)sender { [self close]; return YES; } - (void)close { if (self.window.isSheet) { [self.window.sheetParent endSheet:self.window]; // [self dismissSheet:nil]; } else { [super close]; } } - (void)windowDidLoad { [super windowDidLoad]; [self localizedString]; _PDFContentView.wantsLayer = YES; _PDFContentView.layer.borderWidth = 2.0; _PDFContentView.layer.borderColor = [NSColor blackColor].CGColor; _PDFContentView.layer.backgroundColor = [NSColor whiteColor].CGColor; self.currentPageIndex = 0; self.scaleTextField.stringValue = @"100"; [self.scaleTextField setFormatter:[[TextFieldFormatter alloc] init]]; self.columnCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.columnCount]?:@""; self.lineCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.lineCount]?:@""; self.overlapTextField.stringValue = @"0"; [self.overlapTextField setFormatter:[[TextFieldFormatter alloc] init]]; [self.labelTextField setEditable:NO]; if (!self.PDFDocument) { return; } if (![self.PDFDocument isLocked] && [self.PDFDocument unlockWithPassword:self.password]) { [self getDrawPages]; [self reloadPDFData]; [self reloadPDFView]; NSDate *date = [NSDate date]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[self.PDFDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]]; [self.labelTextField.cell setPlaceholderString:contextString]; } else { // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // PasswordWindowController *com = [[PasswordWindowController alloc] initWithWindowNibName:@"PasswordWindowController"]; // com.fileURL = self.PDFDocument.documentURL; // [com beginSheetModalForWindow:self.window completionHandler:^(NSString *password) { // if (password) { // [self.PDFDocument unlockWithPassword:password]; // self.password = password; // [self getDrawPages]; // [self reloadPDFData]; // [self reloadPDFView]; // // NSDate *date = [NSDate date]; // NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease]; // [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"]; // NSString *contextString = [NSString stringWithFormat:@"(column,row) %@ %@",[self.PDFDocument.documentURL.path lastPathComponent],[formatter stringFromDate:date]]; // // [self.labelTextField.cell setPlaceholderString:contextString]; // // } else { // [self close]; // // } // }]; // [com release]; // }); } } - (void)localizedString { self.window.title = NSLocalizedString(@"Poster", nil); self.pageRangeLabel.stringValue = NSLocalizedString(@"Page Range", nil); self.orientationLabel.stringValue = NSLocalizedString(@"Orientation:", nil); self.unitLabel.stringValue = NSLocalizedString(@"Unit:", nil); self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"mm", nil); self.titleScanlLabel.stringValue = NSLocalizedString(@"Tile Scale:", nil); self.overlapLabel.stringValue = NSLocalizedString(@"Overlap:",nil); self.commentsFormsLabel.stringValue = NSLocalizedString(@"Comments & Forms:",nil); [self.pageRangeComboBox addItemsWithObjectValues:@[NSLocalizedString(@"All Pages", nil), NSLocalizedString(@"Odd Pages Only", nil), NSLocalizedString(@"Even Pages Only",nil), NSLocalizedString(@"e.g. 1,3-5,10",nil)]]; [self.pageRangeComboBox.cell setPlaceholderString:NSLocalizedString(@"e.g. 1,3-5,10", nil)]; [self.commentsFormsComboBox addItemsWithObjectValues:@[NSLocalizedString(@"Document", nil), NSLocalizedString(@"Document and Markups", nil), NSLocalizedString(@"Document and Stamps",nil) ]]; [self.commentsFormsComboBox selectItemAtIndex:1]; [self.commentsFormsComboBox setEditable:NO]; self.splitSizeLabel.stringValue = NSLocalizedString(@"Page size:", nil); [self.cancelButton setTitle:NSLocalizedString(@"Cancel", nil)]; [self.saveButton setTitle:NSLocalizedString(@"Save as PDF", nil)]; [self.printButton setTitle:NSLocalizedString(@"Print", nil)]; [self.portraitButton setTitle:NSLocalizedString(@"Portrait",nil)]; [self.landscapeButton setTitle:NSLocalizedString(@"Landscape",nil)]; [self.cutMarksButton setTitle:NSLocalizedString(@"Cut Marks",nil)]; [self.labelButton setTitle:NSLocalizedString(@"Label",nil)]; [self.reverseButton setTitle:NSLocalizedString(@"Reverse pages",nil)]; self.pagesPerLabel.stringValue = NSLocalizedString(@"Pages per sheet:", nil); self.filePageSizeLabel.stringValue = NSLocalizedString(@"Destination paper Size:", nil); [[self.tableView tabViewItemAtIndex:0] setLabel:NSLocalizedString(@"Tile", nil)]; [[self.tableView tabViewItemAtIndex:1] setLabel:NSLocalizedString(@"Split", nil)]; [self.pageRangeComboBox selectItemAtIndex:0]; [self.pageRangeComboBox setEditable:NO]; [self.unitComboBox selectItemAtIndex:1]; NSArray *paperArray = [KMPageSizeTool paperSize]; NSMutableArray *tPaperArray = [NSMutableArray arrayWithArray:paperArray]; [tPaperArray addObject:NSLocalizedString(@"Custom",nil)]; [self.splitSizeComboBox addItemsWithObjectValues:tPaperArray]; [self.splitSizeComboBox selectItemAtIndex:1]; [self.splitSizeComboBox setEditable:NO]; self.pageWidthTextField.enabled = NO; self.pageHeightTextField.enabled = NO; NSMutableArray *tFileArray = [NSMutableArray arrayWithArray:paperArray]; [tFileArray insertObject:NSLocalizedString(@"Actual Size", nil) atIndex:0]; [tFileArray addObject:NSLocalizedString(@"Custom", nil)]; [self.filePageSizeBox addItemsWithObjectValues:tFileArray]; [self.filePageSizeBox selectItemAtIndex:0]; [self.filePageSizeBox setEditable:NO]; self.paperWidthTextField.enabled = NO; self.paperHeightTextField.enabled = NO; [self.selectPerComboBox addItemsWithObjectValues:@[@"2", @"4", @"6", @"9", @"16", NSLocalizedString(@"Custom", nil)]]; [self.selectPerComboBox selectItemAtIndex:5]; [self.selectPerComboBox setEditable:NO]; } - (KMPDFPosterPrint *)PDFPrint { if (!_PDFPrint) { _PDFPrint = [[KMPDFPosterPrint alloc] init]; _PDFPrint.scale = 1.0; _PDFPrint.direction = kKMPDFPosterPrintDirection_Portrait; _PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentMarkups; _PDFPrint.splitType = kKMPDFPosterSplitType_PageSize; _PDFPrint.lineCount = 2; _PDFPrint.columnCount = 2; _PDFPrint.isCut = NO; _PDFPrint.isLabel = NO; _PDFPrint.isOriginalPageSize = YES; _PDFPrint.fullPageSize = _PDFPrint.splitSize = CGSizeMake(595, 842); _PDFPrint.edgeInsets = NSEdgeInsetsMake(50, 60, 50, 60); _PDFPrint.fullPageEdgeInsets = NSEdgeInsetsMake(50, 60, 50, 60); } return _PDFPrint; } -(void)reloadPDFView { NSInteger indexPage = self.currentPageIndex; if (_PDFPrint.isReversePage) { indexPage = self.pages.count - self.currentPageIndex - 1; } PDFPage *page = self.pages[indexPage]; CGRect rect = [page boundsForBox:kPDFDisplayBoxCropBox]; [[KMPosterPrintManager defaultManager] posterPrintPage:page posterPrint:self.PDFPrint completionHandler:^(NSArray *horizontals, NSArray *verticals) { NSData *pageData = page.dataRepresentation; NSImage *img = [[NSImage alloc] initWithData:pageData]; self.thumImageView.image = img; CGFloat printWidth = 0; CGFloat printHeight = 0; if (kKMPDFPosterPrintDirection_Landscape == self.PDFPrint.direction) { printHeight = self.PDFPrint.splitSize.width - self.PDFPrint.edgeInsets.top - self.PDFPrint.edgeInsets.bottom; printWidth = self.PDFPrint.splitSize.height- self.PDFPrint.edgeInsets.left - self.PDFPrint.edgeInsets.right; } else { printWidth = self.PDFPrint.splitSize.width - self.PDFPrint.edgeInsets.left - self.PDFPrint.edgeInsets.right; printHeight = self.PDFPrint.splitSize.height- self.PDFPrint.edgeInsets.top - self.PDFPrint.edgeInsets.bottom; } printWidth /= self.PDFPrint.scale; printHeight /= self.PDFPrint.scale; CGFloat contentWidth = printWidth + (printWidth - self.PDFPrint.overlap) * (horizontals.count -1); //裁剪Page的总宽度 CGFloat contentHeight = printHeight + (printHeight- self.PDFPrint.overlap) * (verticals.count - 1); //裁剪Page的总高度 CGFloat realityPDFW = rect.size.width; CGFloat realityPDFH = rect.size.height; NSInteger rotate = page.rotation; if (90 == rotate || 270 == rotate) { realityPDFW = rect.size.height; realityPDFH = rect.size.width; } if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) { contentWidth = realityPDFW; contentHeight = realityPDFH; } CGFloat max = MAX((contentWidth/maxPrintWidth), (contentHeight/maxPrintWidth)); self.contentWidth.constant = contentWidth/max; self.contentHeight.constant = contentHeight/max; self.PDFWidth.constant = realityPDFW/max; self.PDFHeight.constant = realityPDFH/max; self.preview.frame = CGRectMake(0, 0, self.contentWidth.constant, self.contentHeight.constant); self.preview.horizontals = horizontals; self.preview.verticals = verticals; self.preview.overlap = self.PDFPrint.overlap / max; self.preview.scanle = max; self.preview.spaceWidth = (self.contentWidth.constant - self.PDFWidth.constant)/2; self.preview.spaceHeight = (self.contentHeight.constant - self.PDFHeight.constant)/2; CGSize paperSize = CGSizeZero; NSInteger tScale = 0; if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) { CGFloat w = (realityPDFW + self.PDFPrint.overlap * (horizontals.count -1))/self.PDFPrint.columnCount; CGFloat h = (realityPDFH + self.PDFPrint.overlap * (verticals.count -1))/self.PDFPrint.lineCount; if (self.PDFPrint.isOriginalPageSize) { tScale = 100; paperSize = CGSizeMake(realityPDFW/self.PDFPrint.columnCount, realityPDFW/self.PDFPrint.lineCount); }else { tScale = MIN(self.PDFPrint.fullPageSize.width/w, self.PDFPrint.fullPageSize.height/h) * 100; paperSize = self.PDFPrint.fullPageSize; } } else { paperSize = self.PDFPrint.splitSize; tScale = (NSInteger)(((realityPDFW + self.PDFPrint.overlap * (horizontals.count -1))/realityPDFW) *self.PDFPrint.scale*100); } NSInteger tPagesCount = (NSInteger)(verticals.count* horizontals.count); self.zoomLabel.stringValue = [NSString stringWithFormat:@"%@ %ld%% %@ %ld", NSLocalizedString(@"Scale:", nil), tScale, NSLocalizedString(@"Pages:", nil), tPagesCount]; CGFloat W = (self.PDFPrint.splitSize.width *horizontals.count)/(595/210.0); CGFloat H = (self.PDFPrint.splitSize.height *verticals.count)/(842/297.0); paperSize.width = paperSize.width/(595/210.0); paperSize.height = paperSize.height/(842/297.0); self.sizeLabel.stringValue = [NSString stringWithFormat:@"%@ x %@ %@",[KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:W],[KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:H],self.unitComboBox.stringValue] ?:@""; self.pageWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.width/595 * 210)]?:@""; self.pageHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.height/842 * 297)]?:@""; if ([self.filePageSizeBox indexOfSelectedItem] == 0) { self.paperWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.fullPageSize.width/585.0 * 210)]; self.paperHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:(self.PDFPrint.splitSize.height/842 * 297)]?:@""; } else { self.paperWidthTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:paperSize.width]; self.paperHeightTextField.stringValue = [KMPageSizeTool conversionWithUnit:self.unitComboBox.stringValue value:paperSize.height]?:@""; } [self.preview setNeedsDisplay:YES]; }]; } - (KMPDFPosterPrintContentView *)preview { if (!_preview) { _preview = [[KMPDFPosterPrintContentView alloc] init]; [self.PDFContentView addSubview:self.preview]; } return _preview; } -(void)bringSubviewToFront:(NSView*)view { [view removeFromSuperview]; [self.PDFContentView addSubview:view]; } - (void)reloadPDFData { NSInteger pageCount = self.pages.count; self.currentPageLabel.stringValue = [NSString stringWithFormat:@"%@",@(self.currentPageIndex+1)]; self.totalPageLabel.stringValue = [NSString stringWithFormat:@" / %@",@(pageCount)]; } - (void)viewFileAtFinder:(NSString *)fileName { NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; NSURL *url = [NSURL fileURLWithPath:fileName]; [workspace activateFileViewerSelectingURLs:[NSArray arrayWithObject:url]]; } - (void)getDrawPages { NSMutableArray * pages = [NSMutableArray array]; if (0 == self.pageRangeComboBox.indexOfSelectedItem) { for (NSUInteger i = 0; i self.currentPageIndex + 1) { self.currentPageIndex ++; [self reloadPDFData]; [self reloadPDFView]; } } - (IBAction)buttonItemClick_Previous:(id)sender { if (self.currentPageIndex != 0) { self.currentPageIndex --; [self reloadPDFData]; [self reloadPDFView]; } } - (IBAction)comboBoxItemClick_PageRange:(id)sender { if (0 == [self.pageRangeComboBox indexOfSelectedItem] || 1 == [self.pageRangeComboBox indexOfSelectedItem] || 2 == [self.pageRangeComboBox indexOfSelectedItem]) { self.pageRangeComboBox.delegate = nil; [self.window makeFirstResponder:self]; [self.pageRangeComboBox setEditable:NO]; [self getDrawPages]; if (self.pages.count > 0) { self.currentPageIndex = 0; [self reloadPDFView]; [self reloadPDFData]; } } else if (3 == [self.pageRangeComboBox indexOfSelectedItem]) { self.pageRangeComboBox.delegate = self; [self.pageRangeComboBox setStringValue:@""]; [self.pageRangeComboBox setEditable:YES]; [self.window makeFirstResponder:self.pageRangeComboBox]; } } - (IBAction)comboBoxItemClick_SplitSize:(id)sender { self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; self.pageWidthTextField.enabled = NO; self.pageHeightTextField.enabled = NO; if ([_splitSizeComboBox indexOfSelectedItem] == _splitSizeComboBox.numberOfItems - 1) { self.PDFPrint.splitSize = CGSizeMake(595, 842); self.pageWidthTextField.enabled = YES; self.pageHeightTextField.enabled = YES; } else { CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:self.splitSizeComboBox.stringValue]; self.PDFPrint.splitSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297.0); } CGFloat multiple = (MAX(self.PDFPrint.splitSize.height, self.PDFPrint.splitSize.width)/842); _PDFPrint.edgeInsets = NSEdgeInsetsMake(50 * multiple, 60 * multiple, 50 * multiple, 60 * multiple); [self reloadPDFView]; } - (IBAction)comboBoxItemClick_fullPageSize:(id)sender { self.PDFPrint.isOriginalPageSize = NO; self.paperWidthTextField.enabled = NO; self.paperHeightTextField.enabled = NO; if ([self.filePageSizeBox indexOfSelectedItem] == 0) { self.PDFPrint.isOriginalPageSize = YES; } else if ([self.filePageSizeBox indexOfSelectedItem] == _splitSizeComboBox.numberOfItems - 1){ self.PDFPrint.fullPageSize = CGSizeMake(595, 842); self.paperWidthTextField.enabled = YES; self.paperHeightTextField.enabled = YES; } else { CGSize tSize = [KMPageSizeTool getPaperSizeWithPaperName:self.filePageSizeBox.stringValue]; self.PDFPrint.fullPageSize = CGSizeMake(tSize.width * 595.0/210, tSize.height * 842.0/297); } self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; CGFloat multiple = (MAX(self.PDFPrint.fullPageSize.height, self.PDFPrint.fullPageSize.width)/842); _PDFPrint.fullPageEdgeInsets = NSEdgeInsetsMake(50 * multiple, 60 * multiple, 50 * multiple, 60 * multiple); [self reloadPDFView]; } - (IBAction)buttonItemClick_ReversePage:(id)sender { if (NSOnState == self.reverseButton.state) { self.PDFPrint.isReversePage = YES; } else { self.PDFPrint.isReversePage = NO; } [self reloadPDFView]; } - (IBAction)comboBoxItemClick_Unit:(id)sender { if ([self.unitComboBox indexOfSelectedItem] == 1) { self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"mm", nil); } else if([self.unitComboBox indexOfSelectedItem] == 0){ self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"cm", nil); } else { self.unitTLabel.stringValue = self.unitT1Label.stringValue = self.overlapUnit.stringValue = NSLocalizedString(@"in", nil); } self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = self.overlapTextField.integerValue; [self reloadPDFView]; } - (IBAction)comboBoxItemClick_CommentsForms:(id)sender { if (0 == [self.commentsFormsComboBox indexOfSelectedItem]) { self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_Documents; } else if(1 == [self.commentsFormsComboBox indexOfSelectedItem]){ self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentMarkups; } else if(2 == [self.commentsFormsComboBox indexOfSelectedItem]){ self.PDFPrint.commentsForms = kKMPDFPosterCommentsForms_DocumentStamps; } [self reloadPDFView]; } - (IBAction)comboBoxItemClick_PagesPer:(id)sender { if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) { self.columnCountText.editable = NO; self.lineCountText.editable = NO; if (0 == [self.selectPerComboBox indexOfSelectedItem]) { self.PDFPrint.columnCount = 1; self.PDFPrint.lineCount = 2; } else if(1 == [self.selectPerComboBox indexOfSelectedItem]){ self.PDFPrint.columnCount = 2; self.PDFPrint.lineCount = 2; } else if(2 == [self.selectPerComboBox indexOfSelectedItem]){ self.PDFPrint.columnCount = 2; self.PDFPrint.lineCount = 3; } else if(3 == [self.selectPerComboBox indexOfSelectedItem]){ self.PDFPrint.columnCount = 3; self.PDFPrint.lineCount = 3; } else if(4 == [self.selectPerComboBox indexOfSelectedItem]){ self.PDFPrint.columnCount = 4; self.PDFPrint.lineCount = 4; } self.columnCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.columnCount]; self.lineCountText.stringValue = [NSString stringWithFormat:@"%ld",self.PDFPrint.lineCount]; self.columnCountText.selectable = NO; self.lineCountText.selectable = NO; } else { self.PDFPrint.columnCount = 2; self.PDFPrint.lineCount = 2; self.columnCountText.stringValue = @"2"; self.lineCountText.stringValue = @"2"; self.columnCountText.editable = YES; self.lineCountText.editable = YES; self.columnCountText.selectable = YES; self.lineCountText.selectable = YES; } [self reloadPDFView]; } - (void)tabView:(NSTabView *)tabView willSelectTabViewItem:(nullable NSTabViewItem *)tabViewItem { self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; NSString * identifier = (NSString *)tabViewItem.identifier; if ([identifier isEqualToString:@"1"]) { _PDFPrint.splitType = kKMPDFPosterSplitType_PageSize; } else { _PDFPrint.splitType = kKMPDFPosterSplitType_PageNumber; } [self reloadPDFView]; } #pragma mark - NSTextFieldDelegate Methods - (void)controlTextDidEndEditing:(NSNotification *)notification { NSTextField *textField = (NSTextField *)[notification object]; if (textField == self.currentPageLabel) { NSInteger index = [[self.currentPageLabel stringValue] integerValue]; NSInteger pageCount = self.pages.count; if (index > 0 && index <= pageCount) { self.currentPageIndex = index - 1; [self reloadPDFView]; } else { self.currentPageLabel.stringValue = [NSString stringWithFormat:@"%@",@(1)]; } } else if (textField == self.scaleTextField) { if (self.scaleTextField.integerValue <= 0) { self.scaleTextField.stringValue = @"100"; } self.PDFPrint.scale = self.scaleTextField.integerValue / 100.0; [self reloadPDFView]; } else if (textField == self.pageRangeComboBox) { [self getDrawPages]; if (self.pages.count > 0) { self.currentPageIndex = 0; [self reloadPDFView]; [self reloadPDFData]; } } else if (textField == self.columnCountText) { if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) { return; } if (self.columnCountText.integerValue < 1) { self.columnCountText.stringValue = @"1"; } else if (self.columnCountText.integerValue > 99){ self.columnCountText.stringValue = @"99"; } self.PDFPrint.columnCount = self.columnCountText.integerValue; [self reloadPDFView]; } else if (textField == self.lineCountText) { if ( 5!= [self.selectPerComboBox indexOfSelectedItem]) { return; } if (self.lineCountText.integerValue < 1) { self.lineCountText.stringValue = @"1"; } else if (self.lineCountText.integerValue > 99){ self.lineCountText.stringValue = @"99"; } self.PDFPrint.lineCount = self.lineCountText.integerValue; [self reloadPDFView]; } else if (textField == self.pageWidthTextField || textField == self.paperWidthTextField) { self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; CGFloat over = [[textField stringValue] floatValue]; if ([self.unitComboBox indexOfSelectedItem] == 0) { over *= 595.0/21; } else if([self.unitComboBox indexOfSelectedItem] == 3){ over *= 595.0/210 * 25.4; } else { over *= 595.0/210; } CGSize size = CGSizeZero; if (textField == self.pageWidthTextField ) { size = self.PDFPrint.splitSize; size.width = over; self.PDFPrint.splitSize = size; } else { size = self.PDFPrint.fullPageSize; size.width = over; self.PDFPrint.fullPageSize = size; } [self reloadPDFView]; } else if (textField == self.pageHeightTextField || textField == self.paperHeightTextField) { self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; CGFloat over = [[textField stringValue] floatValue]; if ([self.unitComboBox indexOfSelectedItem] == 0) { over *= 842.0/29.7; } else if([self.unitComboBox indexOfSelectedItem] == 3){ over *= 842.0/297 * 25.4; } else { over *= 842.0/297; } CGSize size = CGSizeZero; if (textField == self.pageHeightTextField ) { size = self.PDFPrint.splitSize; size.height = over; self.PDFPrint.splitSize = size; } else { size = self.PDFPrint.fullPageSize; size.height = over; self.PDFPrint.fullPageSize = size; } [self reloadPDFView]; } } - (void)controlTextDidChange:(NSNotification *)notification { NSTextField *textField = (NSTextField *)[notification object]; if (textField == self.currentPageLabel || textField == self.columnCountText || textField == self.lineCountText) { NSString *string = [textField.formatter stringFromNumber:[NSNumber numberWithFloat:[textField.stringValue integerValue]]]; textField.stringValue = string; } else if (textField == self.overlapTextField){ CGFloat maxOverlap = MIN(self.PDFPrint.splitSize.width, self.PDFPrint.splitSize.height); if (self.PDFPrint.splitType == kKMPDFPosterSplitType_PageNumber) { maxOverlap = MIN(self.PDFPrint.fullPageSize.width, self.PDFPrint.fullPageSize.height); } CGFloat over = [[textField stringValue] floatValue]; if ([self.unitComboBox indexOfSelectedItem] == 0) { over *= 28.4; } else if([self.unitComboBox indexOfSelectedItem] == 3){ over *= 72.0; } else { over *= 2.84; //595/210 } if (over <= 0 || over > maxOverlap/2) { self.overlapTextField.stringValue = @"0"; self.PDFPrint.overlap = 0; } else { self.PDFPrint.overlap = over; } [self reloadPDFView]; } } #pragma mark - show / hide Waiting - (void)showWaitting { if (!_posterMaskView) { _posterMaskView = [[KMBookletMaskView alloc] initWithFrame:NSMakeRect(0, 0, self.window.frame.size.width, self.window.frame.size.height)]; } [self.window.contentView addSubview:self.posterMaskView]; } - (void)hideWaitting { [self.posterMaskView removeFromSuperview]; } @end