// // KMWatermarkPDFView.m // PDF Reader Pro Edition // // Created by zhipeng jiang on 2020/11/17. // #import "KMWatermarkPDFView_OC.h" #import @interface KMWatermarkPDFView_OC () @property (nonatomic, strong) KMWatermarkModel *watermark; @property (nonatomic, strong) KMBackgroundModel *background; @property (nonatomic, strong) KMHeaderFooterObject *bates; @property (nonatomic, strong) KMHeaderFooterObject *headerFooter; @end @implementation KMWatermarkPDFView_OC - (void)setModel:(id)model { _model = model; if ([model isKindOfClass:[KMHeaderFooterObject class]]) { self.bates = model; } else if ([model isKindOfClass:[KMWatermarkModel class]]) { self.watermark = model; } else if ([model isKindOfClass:[KMBackgroundModel class]]) { self.background = model; } else if ([model isKindOfClass:[KMHeaderFooterObject class]]) { self.headerFooter = model; } else { self.bates = nil; self.watermark = nil; self.background = nil; self.headerFooter = nil; } [self setNeedsDisplayForVisiblePages]; } - (void)drawPage:(CPDFPage *)page toContext:(CGContextRef)context { if (self.watermark) { if (self.watermark.isFront) { [super drawPage:page toContext:context]; [self drawWatermarkPage:page toContext:context]; } else { [self drawWatermarkPage:page toContext:context]; [super drawPage:page toContext:context]; } } else if (self.background) { [self drawBackgroundPage:page toContext:context]; [super drawPage:page toContext:context]; } else if (self.headerFooter ){ [super drawPage:page toContext:context]; [self drawHeaderFooterPage:page toContext:context]; } else if (self.bates) { [super drawPage:page toContext:context]; [self drawBatesPage:page toContext:context]; } else { [super drawPage:page toContext:context]; } } - (void)drawWatermarkPage:(CPDFPage *)page toContext:(CGContextRef)context { NSRect pageBounds = [page boundsForBox:CPDFDisplayCropBox]; CGFloat w = pageBounds.size.width; CGFloat h = pageBounds.size.height; CGFloat width = sqrt(pageBounds.size.height * pageBounds.size.height + pageBounds.size.width * pageBounds.size.width); CGRect newRect = CGRectMake(-(width - pageBounds.size.width)/2, -(width - pageBounds.size.height)/2, width, width); CGFloat new_w = newRect.size.width; CGFloat new_h = newRect.size.height; if (context) { [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]]; } [NSGraphicsContext saveGraphicsState]; [page transformContext:context forBox:CPDFDisplayCropBox]; if(self.watermark.pagesString.length > 0) { NSArray *array = [self.watermark.pagesString componentsSeparatedByString:@","]; NSInteger index = [self.document indexForPage:page]; if(![array containsObject:[NSString stringWithFormat:@"%ld",index]]) { return; } } if (self.watermark.text && self.watermark.text.length > 0) { NSFont *font = [NSFont fontWithName:@"Helvetica" size:self.watermark.getTextFontSize]; NSColor *color = self.watermark.getTextColor ? : [NSColor blackColor]; CGFloat red,green,blue; [[color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:nil]; color = [NSColor colorWithCalibratedRed:red green:green blue:blue alpha:self.watermark.opacity]; NSSize size = NSZeroSize; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:NSTextAlignmentCenter]; [style setLineBreakMode:NSLineBreakByCharWrapping]; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:style forKey:NSParagraphStyleAttributeName]; [dictionary setObject:color forKey:NSForegroundColorAttributeName]; [dictionary setObject:font forKey:NSFontAttributeName]; size = [self.watermark.text boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat radian = self.watermark.rotation*(M_PI/180); CGAffineTransform t = CGAffineTransformRotate(CGAffineTransformIdentity, radian); CGRect rect = CGRectMake(0, 0, size.width, size.height); if(self.watermark.isTilePage) { CGContextTranslateCTM(context, w/2,h/2); CGContextConcatCTM(context, t); CGContextTranslateCTM(context, -(w/2), - (h/2)); CGFloat verticalWidth = size.width + self.watermark.tileHorizontalSpace; CGFloat horizontalHeight = size.height + self.watermark.tileVerticalSpace; NSInteger line = (new_h - self.watermark.tileVerticalSpace )/ horizontalHeight + 1 ; //多少行 NSInteger row = (new_w - self.watermark.tileHorizontalSpace) / verticalWidth + 1 ; CGPoint point = CGPointMake(w/2 - size.width/2+self.watermark.horizontalSpace, h/2 - size.height/2+self.watermark.verticalSpace); for (int i = 0; i < line; i ++) { for (int j = 0; j < row ; j ++) { [self.watermark.text drawInRect:CGRectMake(point.x + j * verticalWidth, point.y + i*horizontalHeight, size.width, size.height) withAttributes:dictionary]; } } for (int i = 1; i < line; i ++) { for (int j = 0; j < row ; j ++) { [self.watermark.text drawInRect:CGRectMake(point.x + j * verticalWidth, point.y - i*horizontalHeight, size.width, size.height) withAttributes:dictionary]; } } for (int i = 0; i < line; i ++) { for (int j = 1; j < row ; j ++) { [self.watermark.text drawInRect:CGRectMake(point.x -j * verticalWidth, point.y + i*horizontalHeight, size.width, size.height) withAttributes:dictionary]; } } for (int i = 1; i < line; i ++) { for (int j = 1; j < row ; j ++) { [self.watermark.text drawInRect:CGRectMake(point.x - j * verticalWidth, point.y - i*horizontalHeight, size.width, size.height) withAttributes:dictionary]; } } } else { if (self.watermark.verticalMode == 0) { rect.origin.y = pageBounds.size.height-rect.size.height; } else if (self.watermark.verticalMode == 1) { rect.origin.y = (pageBounds.size.height-rect.size.height)/2.0; } else { rect.origin.y = 0; } if (self.watermark.horizontalMode == 0) { rect.origin.x = 0; } else if (self.watermark.horizontalMode == 1) { rect.origin.x = (pageBounds.size.width-rect.size.width)/2.0; } else { rect.origin.x = pageBounds.size.width-rect.size.width; } rect.origin.x += self.watermark.horizontalSpace; rect.origin.y += self.watermark.verticalSpace; CGPoint contextCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextTranslateCTM(context, contextCenter.x, contextCenter.y); CGContextRotateCTM(context, radian); CGContextTranslateCTM(context, -contextCenter.x, -contextCenter.y); [self.watermark.text drawInRect:rect withAttributes:dictionary]; } } else if (self.watermark.image) { NSData * tiffData = [self.watermark.image TIFFRepresentation]; NSBitmapImageRep * bitmap; bitmap = [NSBitmapImageRep imageRepWithData:tiffData]; CIImage * ciImage = [[CIImage alloc] initWithBitmapImageRep:bitmap]; NSSize size = [ciImage extent].size; size.width *= self.watermark.scale; size.height *= self.watermark.scale; CGFloat radian = self.watermark.rotation*(M_PI/180); CGAffineTransform t = CGAffineTransformRotate(CGAffineTransformIdentity, radian); CGRect rect = CGRectMake(0, 0, size.width, size.height); if(self.watermark.isTilePage) { CGContextTranslateCTM(context, w/2,h/2); CGContextConcatCTM(context, t); CGContextTranslateCTM(context, -(w/2), - (h/2)); CGFloat verticalWidth = size.width + self.watermark.tileHorizontalSpace; CGFloat horizontalHeight = size.height + self.watermark.tileVerticalSpace; NSInteger line = (new_h - self.watermark.tileVerticalSpace )/ horizontalHeight + 1 ; //多少行 NSInteger row = (new_w - self.watermark.tileHorizontalSpace) / verticalWidth + 1 ; CGPoint point = CGPointMake(w/2 - size.width/2, h/2 - size.height/2); for (int i = 0; i < line/2+1; i ++) { for (int j = 0; j < row ; j ++) { CGRect area = CGRectMake(point.x + j * verticalWidth, point.y + i*horizontalHeight, size.width,size.height); [self.watermark.image drawInRect:area fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.watermark.opacity]; } } for (int i = 1; i < line/2+1; i ++) { for (int j = 0; j < row ; j ++) { CGRect area = CGRectMake(point.x + j * verticalWidth, point.y - i*horizontalHeight, size.width,size.height); [self.watermark.image drawInRect:area fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.watermark.opacity]; } } for (int i = 0; i < line/2 +1; i ++) { for (int j = 1; j < row ; j ++) { CGRect area = CGRectMake(point.x - j * verticalWidth, point.y + i*horizontalHeight, size.width,size.height); [self.watermark.image drawInRect:area fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.watermark.opacity]; } } for (int i = 1; i < line/2 + 1; i ++) { for (int j = 1; j < row ; j ++) { CGRect area = CGRectMake(point.x - j * verticalWidth, point.y - i*horizontalHeight, size.width, size.height); [self.watermark.image drawInRect:area fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.watermark.opacity]; } } } else { if (self.watermark.verticalMode == 0) { rect.origin.y = pageBounds.size.height-rect.size.height; } else if (self.watermark.verticalMode == 1) { rect.origin.y = (pageBounds.size.height-rect.size.height)/2.0; } else { rect.origin.y = 0; } if (self.watermark.horizontalMode == 0) { rect.origin.x = 0; } else if (self.watermark.horizontalMode == 1) { rect.origin.x = (pageBounds.size.width-rect.size.width)/2.0; } else { rect.origin.x = pageBounds.size.width-rect.size.width; } rect.origin.x += self.watermark.horizontalSpace; rect.origin.y += self.watermark.verticalSpace; CGPoint contextCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextTranslateCTM(context, contextCenter.x, contextCenter.y); CGContextRotateCTM(context, radian); CGContextTranslateCTM(context, -contextCenter.x, -contextCenter.y); [self.watermark.image drawInRect:rect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.watermark.opacity]; } } [NSGraphicsContext restoreGraphicsState]; } - (void)drawBackgroundPage:(CPDFPage *)page toContext:(CGContextRef)context { NSRect pageBounds = [page boundsForBox:CPDFDisplayCropBox]; if (context) { [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]]; } [NSGraphicsContext saveGraphicsState]; [page transformContext:context forBox:CPDFDisplayCropBox]; //self.background.color && self.background.color != [NSColor clearColor] if (self.background.type == KMBackgroundTypeColor) { NSSize size = pageBounds.size; size.width *= self.background.scale; size.height *= self.background.scale; CGFloat radian = self.background.rotation*(M_PI/180); CGAffineTransform t = CGAffineTransformRotate(CGAffineTransformIdentity, radian); CGRect rect = CGRectApplyAffineTransform(CGRectMake(0, 0, size.width, size.height), t); if (self.background.verticalMode == 0) { rect.origin.y = pageBounds.size.height-rect.size.height; } else if (self.background.verticalMode == 1) { rect.origin.y = (pageBounds.size.height-rect.size.height)/2.0; } else { rect.origin.y = 0; } if (self.background.horizontalMode == 0) { rect.origin.x = 0; } else if (self.background.horizontalMode == 1) { rect.origin.x = (pageBounds.size.width-rect.size.width)/2.0; } else { rect.origin.x = pageBounds.size.width-rect.size.width; } rect.origin.y += self.background.verticalSpace; rect.origin.x += self.background.horizontalSpace; CGPoint contextCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextTranslateCTM(context, contextCenter.x, contextCenter.y); CGContextRotateCTM(context, radian); CGContextTranslateCTM(context, -contextCenter.x, -contextCenter.y); // CGContextSetBlendMode(context, kCGBlendModeDestinationIn); CGFloat red,green,blue,alpha; [[self.background.color colorUsingColorSpaceName:NSCalibratedRGBColorSpace] getRed:&red green:&green blue:&blue alpha:&alpha]; CGContextSetRGBFillColor(context, red, green, blue, self.background.opacity); CGContextFillRect(context, CGRectMake(rect.origin.x+(rect.size.width-size.width)/2.0, rect.origin.y+(rect.size.height-size.height)/2.0, size.width, size.height)); } else if (self.background.type == KMBackgroundTypeFile) { /// self.background.image NSImage *image = self.background.image; NSSize size = image.size;; if ([self.background.imagePath.pathExtension.lowercaseString isEqualToString:@"pdf"] && [[NSFileManager defaultManager] fileExistsAtPath:self.background.imagePath]) { } else { size = CGSizeMake(size.width * 2, size.height * 2); } size.width *= self.background.scale; size.height *= self.background.scale; CGFloat radian = self.background.rotation*(M_PI/180); CGAffineTransform t = CGAffineTransformRotate(CGAffineTransformIdentity, radian); CGRect rect = CGRectApplyAffineTransform(CGRectMake(0, 0, size.width, size.height), t); if (self.background.verticalMode == 0) { rect.origin.y = pageBounds.size.height-rect.size.height; } else if (self.background.verticalMode == 1) { rect.origin.y = (pageBounds.size.height-rect.size.height)/2.0; } else { rect.origin.y = 0; } if (self.background.horizontalMode == 0) { rect.origin.x = 0; } else if (self.background.horizontalMode == 1) { rect.origin.x = (pageBounds.size.width-rect.size.width)/2.0; } else { rect.origin.x = pageBounds.size.width-rect.size.width; } rect.origin.y += self.background.verticalSpace; rect.origin.x += self.background.horizontalSpace; CGPoint contextCenter = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect)); CGContextTranslateCTM(context, contextCenter.x, contextCenter.y); CGContextRotateCTM(context, radian); CGContextTranslateCTM(context, -contextCenter.x, -contextCenter.y); [image drawInRect:CGRectMake(rect.origin.x+(rect.size.width-size.width)/2.0, rect.origin.y+(rect.size.height-size.height)/2.0, size.width, size.height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:self.background.opacity]; } [NSGraphicsContext restoreGraphicsState]; } - (void)drawHeaderFooterPage:(CPDFPage *)page toContext:(CGContextRef)context { NSRect pageBounds = [page boundsForBox:CPDFDisplayCropBox]; [NSGraphicsContext saveGraphicsState]; if (context) { [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]]; } [page transformContext:context forBox:CPDFDisplayCropBox]; NSColor *color = self.headerFooter.getTextColor ? : [NSColor blackColor]; NSFont *font = [NSFont boldSystemFontOfSize:self.headerFooter.getTextFontSize]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:NSTextAlignmentCenter]; [style setLineBreakMode:NSLineBreakByCharWrapping]; NSSize size = NSZeroSize; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:style forKey:NSParagraphStyleAttributeName]; [dictionary setObject:color forKey:NSForegroundColorAttributeName]; [dictionary setObject:font forKey:NSFontAttributeName]; NSInteger index = [self.document indexForPage:page]; CGFloat topOffset = 0; CGFloat bottomOffset = 0; if (self.headerFooter.topLeftString && self.headerFooter.topLeftString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.topLeftString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.headerFooter.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(self.headerFooter.leftMargin, posY, size.width, size.height) withAttributes:dictionary]; } if (self.headerFooter.topCenterString && self.headerFooter.topCenterString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.topCenterString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.headerFooter.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(pageBounds.size.width/2 - size.width/2, posY, size.width, size.height) withAttributes:dictionary]; } if (self.headerFooter.topRightString && self.headerFooter.topRightString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.topRightString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.headerFooter.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(pageBounds.size.width -self.headerFooter.rightMargin - size.width, posY, size.width, size.height) withAttributes:dictionary]; } if (self.headerFooter.bottomLeftString && self.headerFooter.bottomLeftString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.bottomLeftString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.headerFooter.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(self.headerFooter.leftMargin, posY, size.width, size.height) withAttributes:dictionary]; } if (self.headerFooter.bottomCenterString && self.headerFooter.bottomCenterString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.bottomCenterString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.headerFooter.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(pageBounds.size.width/2 - size.width/2, posY, size.width, size.height) withAttributes:dictionary]; } if (self.headerFooter.bottomRightString && self.headerFooter.bottomRightString.length > 0) { NSString *tString = [self replacingOldString:self.headerFooter.bottomRightString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.headerFooter.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(pageBounds.size.width - self.headerFooter.rightMargin - size.width, posY, size.width, size.height) withAttributes:dictionary]; } 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); CGContextMoveToPoint(context, 0, self.headerFooter.bottomMargin + bottomOffset); CGContextAddLineToPoint(context,pageBounds.size.width, self.headerFooter.bottomMargin + bottomOffset); CGContextMoveToPoint(context, self.headerFooter.leftMargin, 0); CGContextAddLineToPoint(context, self.headerFooter.leftMargin,pageBounds.size.height); CGContextMoveToPoint(context, pageBounds.size.width - self.headerFooter.rightMargin, 0); CGContextAddLineToPoint(context,pageBounds.size.width - self.headerFooter.rightMargin, pageBounds.size.height); CGContextMoveToPoint(context, 0, pageBounds.size.height - self.headerFooter.topMargin - topOffset); CGContextAddLineToPoint(context,pageBounds.size.width, pageBounds.size.height - self.headerFooter.topMargin - topOffset); CGFloat arr[] = {8,1}; CGContextSetLineDash(context, 0, arr, 1); CGContextDrawPath(context, kCGPathStroke); [NSGraphicsContext restoreGraphicsState]; } - (void)drawBatesPage:(CPDFPage *)page toContext:(CGContextRef)context { NSRect pageBounds = [page boundsForBox:CPDFDisplayCropBox]; [NSGraphicsContext saveGraphicsState]; if (context) { [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]]; } [page transformContext:context forBox:CPDFDisplayCropBox]; NSColor *color = self.bates.getTextColor ? : [NSColor blackColor]; NSFont *font = [NSFont boldSystemFontOfSize:self.bates.getTextFontSize]; NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init]; [style setAlignment:NSTextAlignmentCenter]; [style setLineBreakMode:NSLineBreakByCharWrapping]; NSSize size = NSZeroSize; NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:style forKey:NSParagraphStyleAttributeName]; [dictionary setObject:color forKey:NSForegroundColorAttributeName]; [dictionary setObject:font forKey:NSFontAttributeName]; NSInteger index = [self.document indexForPage:page]; CGFloat topOffset = 0; CGFloat bottomOffset = 0; if (self.bates.topLeftString && self.bates.topLeftString.length > 0) { NSString *tString = [self replacingOldString:self.bates.topLeftString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.bates.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(self.bates.leftMargin, posY, size.width, size.height) withAttributes:dictionary]; } if (self.bates.topCenterString && self.bates.topCenterString.length > 0) { NSString *tString = [self replacingOldString:self.bates.topCenterString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.bates.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(pageBounds.size.width/2 - size.width/2, posY, size.width, size.height) withAttributes:dictionary]; } if (self.bates.topRightString && self.bates.topRightString.length > 0) { NSString *tString = [self replacingOldString:self.bates.topRightString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MIN(pageBounds.size.height - self.bates.topMargin, pageBounds.size.height - size.height); [tString drawInRect:CGRectMake(pageBounds.size.width -self.bates.rightMargin - size.width, posY, size.width, size.height) withAttributes:dictionary]; } if (self.bates.bottomLeftString && self.bates.bottomLeftString.length > 0) { NSString *tString = [self replacingOldString:self.bates.bottomLeftString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.bates.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(self.bates.leftMargin, posY, size.width, size.height) withAttributes:dictionary]; } if (self.bates.bottomCenterString && self.bates.bottomCenterString.length > 0) { NSString *tString = [self replacingOldString:self.bates.bottomCenterString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.bates.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(pageBounds.size.width/2 - size.width/2, posY, size.width, size.height) withAttributes:dictionary]; } if (self.bates.bottomRightString && self.bates.bottomRightString.length > 0) { NSString *tString = [self replacingOldString:self.bates.bottomRightString currentPage:index] ; size = [tString boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:dictionary].size; CGFloat posY = MAX(self.bates.bottomMargin-size.height, 0); [tString drawInRect:CGRectMake(pageBounds.size.width - self.bates.rightMargin - size.width, posY, size.width, size.height) withAttributes:dictionary]; } 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); CGContextMoveToPoint(context, 0, self.bates.bottomMargin + bottomOffset); CGContextAddLineToPoint(context,pageBounds.size.width, self.bates.bottomMargin + bottomOffset); CGContextMoveToPoint(context, self.bates.leftMargin, 0); CGContextAddLineToPoint(context, self.bates.leftMargin,pageBounds.size.height); CGContextMoveToPoint(context, pageBounds.size.width - self.bates.rightMargin, 0); CGContextAddLineToPoint(context,pageBounds.size.width - self.bates.rightMargin, pageBounds.size.height); CGContextMoveToPoint(context, 0, pageBounds.size.height - self.bates.topMargin - topOffset); CGContextAddLineToPoint(context,pageBounds.size.width, pageBounds.size.height - self.bates.topMargin - topOffset); CGFloat arr[] = {8,1}; CGContextSetLineDash(context, 0, arr, 1); CGContextDrawPath(context, kCGPathStroke); [NSGraphicsContext restoreGraphicsState]; } - (NSString *)replacingOldString:(NSString *)oldString currentPage:(NSInteger)page { if (!oldString) { return @""; } NSString * newString = oldString; NSString * startString = @""; if (self.bates) { startString = self.bates.startString; } NSString * newPage = @""; if (self.bates) { NSArray * dates = [self match:@"<<#\\d*?#\\d*#.*?>>|<<#\\d*?#\\d*?>>" stri:oldString]; for (NSTextCheckingResult *object in dates) { NSRange range = object.range; NSString *resultStr = [oldString substringWithRange:range]?:@""; NSString *newResultStr = [resultStr substringWithRange:NSMakeRange(2, resultStr.length - 4)]; NSArray *array = [newResultStr componentsSeparatedByString:@"#"]; if (array.count >1) { NSString *batesDigits = array[1]; if (array.count > 2) { NSString *firstString = array[2]; newPage = [NSString stringWithFormat:@"%0*ld",batesDigits.integerValue,page + firstString.integerValue]; } if(array.count > 3) { newPage = [NSString stringWithFormat:@"%@%@",array[3],newPage]; } if(array.count > 4) { newPage = [newPage stringByAppendingString:array[4]]; } newString = [newString stringByReplacingOccurrencesOfString:resultStr withString:newPage]; } } } else { newPage = [NSString stringWithFormat:@"%ld",page + startString.integerValue]; if (startString) { newString = convertViewPageFormat(newString, newPage, [NSString stringWithFormat:@"%ld",self.document.pageCount + startString.integerValue - 1]); } } newString = convertDateFormat(newString); return newString; } - (NSArray *)match:(NSString *)pattern stri:(NSString *)testString{ // 1.创建正则表达式 NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:pattern options:0 error:nil]; // 2.测试字符串 NSArray * results = [regex matchesInString:testString options:0 range:NSMakeRange(0, testString.length)]; return results; } static NSString * convertDateFormat (NSString *oldString) { NSString *newString = oldString; for (NSString *dateFormat in KMWatermarkAdjectiveTools.getDateFormats) { if ([newString containsString:dateFormat]) { NSString * formatString = [dateFormat stringByReplacingOccurrencesOfString:@"m" withString:@"M"]; NSString *replace = [NSString stringWithFormat:@"<<%@>>",dateFormat]; NSDate *date=[NSDate date]; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; dateFormatter.dateFormat = formatString; NSString * dateString = [dateFormatter stringFromDate:date]; newString= [newString stringByReplacingOccurrencesOfString:replace withString:dateString]; } } return newString; } static NSString * convertViewPageFormat (NSString *oldString,NSString * currentPage,NSString * pageCount) { NSString *newString = oldString; NSArray *PageFromatArray = @[@"1", @"1 of n", @"1/n", @"Page 1", @"Page 1 of n"]; for (NSString *format in PageFromatArray) { NSString *pageFormat = [NSString stringWithFormat:@"<<%@>>",format]; if ([newString containsString:pageFormat]) { NSString *tString = nil; if ([pageFormat isEqual:@"<<1>>"]) { tString = currentPage; } else if ([pageFormat isEqual:@"<<1 of n>>"]) { tString = [NSString stringWithFormat:@"%@ of %@",currentPage,pageCount]; } else if ([pageFormat isEqual:@"<<1/n>>"]){ tString = [NSString stringWithFormat:@"%@/%@",currentPage,pageCount]; } else if ([pageFormat isEqual:@"<>"]){ tString = [NSString stringWithFormat:@"Page %@",currentPage]; } else if ([pageFormat isEqual:@"<>"]){ tString = [NSString stringWithFormat:@"Page %@ of %@",currentPage,pageCount]; } newString= [newString stringByReplacingOccurrencesOfString:pageFormat withString:tString]; } } return newString; } @end