123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020 |
- //
- // 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 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 ()<NSComboBoxDelegate>
- @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), ^{
- [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.PDFDocument.pageCount; i++) {
- PDFPage * page = [self.PDFDocument pageAtIndex:i];
- [pages addObject:page];
- }
- } else if (1 == self.pageRangeComboBox.indexOfSelectedItem) {
- for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
- if (i % 2 == 0) {
- PDFPage * page = [self.PDFDocument pageAtIndex:i];
- [pages addObject:page];
- }
- }
- } else if (2 == self.pageRangeComboBox.indexOfSelectedItem) {
- for (NSUInteger i = 0; i < self.PDFDocument.pageCount; i ++) {
- if (i % 2 != 0) {
- PDFPage * page = [self.PDFDocument pageAtIndex:i];
- [pages addObject:page];
- }
- }
- } else {
- KMFileAttribute *fileAttribute = [[KMFileAttribute alloc] init];
- fileAttribute.filePath = [self.PDFDocument.documentURL path];
- fileAttribute.bAllPage = NO;
- fileAttribute.pagesString = _pageRangeComboBox.stringValue;
-
- if (![fileAttribute fetchSelectPages]) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[fileAttribute.filePath lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
- [alert runModal];
- return;
- }
-
- for (NSNumber * num in [fileAttribute fetchSelectPages]) {
- PDFPage * page = [self.PDFDocument pageAtIndex:(num.integerValue - 1)];
- [pages addObject:page];
- }
- }
-
- self.pages = pages;
- }
- #pragma mark - Button Event
- - (IBAction)buttonItemClick_Done:(id)sender
- {
- [self.window makeFirstResponder:self.preview];
- if (self.pages.count < 1) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[[self.PDFDocument.documentURL path]lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
- [alert runModal];
- return;
- }
- self.PDFPrint.cropPages = self.pages;
-
- if (self.PDFPrint.isLabel) {
- self.PDFPrint.labelString = self.labelTextField.stringValue;
- }
- NSString * fileName = [NSString stringWithFormat:@"%@_Poster",self.PDFDocument.documentURL.lastPathComponent.stringByDeletingPathExtension];
- KMSavePanelAccessoryController *panelAccessoryController = [[KMSavePanelAccessoryController alloc] init];
- NSSavePanel *savePanel = [NSSavePanel savePanel];
- savePanel.nameFieldStringValue = fileName;
- savePanel.allowedFileTypes = @[@"pdf"];
- savePanel.accessoryView = panelAccessoryController.view;
- [savePanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) {
- if (result) {
- [self showWaitting];
- [[KMPosterPrintManager defaultManager] savePath:savePanel.URL.path posterPrint:self.PDFPrint completionHandler:^(BOOL success) {
- [self hideWaitting];
- if (success) {
- if (panelAccessoryController.openAutomaticButton.state == NSControlStateValueOn) {
- [self close];
- [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:savePanel.URL display:YES completionHandler:^(NSDocument * _Nullable document, BOOL documentWasAlreadyOpen, NSError * _Nullable error) {
-
- }];
- } else {
- [self viewFileAtFinder:savePanel.URL.path];
- }
- }
- }];
- }
- }];
- }
- - (IBAction)buttonItemClick_Cancel:(id)sender
- {
- [self close];
- }
- - (IBAction)buttonItemClick_Print:(id)sender
- {
- [self.window makeFirstResponder:self.preview];
- if (self.pages.count < 1) {
- NSAlert *alert = [[NSAlert alloc] init];
- [alert setAlertStyle:NSCriticalAlertStyle];
- [alert setMessageText:[NSString stringWithFormat:@"%@ %@",[[self.PDFDocument.documentURL path]lastPathComponent],NSLocalizedString(@"Invalid page range or the page number is out of range. Please try again.", nil)]];
- [alert runModal];
- return;
- }
-
- self.PDFPrint.cropPages = self.pages;
-
- if (self.PDFPrint.isLabel) {
- self.PDFPrint.labelString = self.labelTextField.stringValue;
- }
- NSString * fileName = nil;
- if (![[NSFileManager defaultManager] fileExistsAtPath:kPosterPrintFolderPath]) {
- if (![[NSFileManager defaultManager] createDirectoryAtPath:kPosterPrintFolderPath withIntermediateDirectories:NO attributes:nil error:nil]) {
- return;
- }
- }
- fileName = @"Poster.pdf";
- fileName = [kPosterPrintFolderPath stringByAppendingPathComponent:fileName];
- [self showWaitting];
- [[KMPosterPrintManager defaultManager] savePath:fileName posterPrint:self.PDFPrint completionHandler:^(BOOL success) {
- [self hideWaitting];
- if (success) {
- PDFDocument * pdf = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:fileName]];
- NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
-
- NSPrintOperation *printOperation = nil;
- if ([pdf respondsToSelector:@selector(printOperationForPrintInfo:scalingMode:autoRotate:)]){
- printOperation = [pdf printOperationForPrintInfo:printInfo scalingMode:kPDFPrintPageScaleNone autoRotate:YES];
- }
-
- [printOperation runOperationModalForWindow:self.window delegate:self didRunSelector:nil contextInfo:NULL];
- }
- }];
- }
- - (IBAction)buttonItemClick_CutMarks:(id)sender
- {
- if ( NSOnState == self.cutMarksButton.state) {
- self.PDFPrint.isCut = YES;
- } else {
- self.PDFPrint.isCut = NO;
- }
- }
- - (IBAction)buttonItemClick_Labels:(id)sender
- {
- if ( NSOnState == self.labelButton.state) {
- self.PDFPrint.isLabel = YES;
- [self.labelTextField setEditable:YES];
- [self.labelTextField becomeFirstResponder];
- } else {
- self.PDFPrint.isLabel = NO;
- [self.labelTextField setEditable:NO];
- self.labelTextField.stringValue = @"";
- }
- }
- - (IBAction)buttonItemClick_Orientation:(id)sender
- {
- if ( NSOnState == self.landscapeButton.state) {
- self.PDFPrint.direction = kKMPDFPosterPrintDirection_Landscape;
- } else if(NSOnState == self.portraitButton.state){
- self.PDFPrint.direction = kKMPDFPosterPrintDirection_Portrait;
- }
- [self reloadPDFView];
- }
- - (IBAction)buttonItemClick_Next:(id)sender
- {
- if (self.pages.count > 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
|