1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027 |
- //
- // 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 ()<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), ^{
- 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.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
|