| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- //
- // RAPDFPage.m
- // pdftest
- //
- // Created by Ray on 10/11/16.
- // Copyright © 2016 United Software Applications, Inc. All rights reserved.
- //
- #import "RAPDFPage.h"
- #import "GroupDrawable.h"
- #import "ImageDrawable.h"
- #import "LineDrawable.h"
- #import "TextDrawable.h"
- #import "TableDrawable.h"
- #import "GridDrawable.h"
- @implementation RAPDFPage
- -(instancetype) init:(NSMutableDictionary *)page size:(CGRect)rect
- {
- if (self = [super init]) {
- self.rect = rect;
- [self setPageTemplate:page];
- self.pagecount=1;
- }
- return self;
-
-
-
- }
- -(void) setPageTemplate:(NSMutableDictionary *)pageTemplate
- {
- _pageTemplate = pageTemplate;
- self.margin_left=[self.pageTemplate[@"margin_left"] doubleValue];
- self.margin_right=[self.pageTemplate[@"margin_right"] doubleValue];
- self.margin_top=[self.pageTemplate[@"margin_top"] doubleValue];
- self.margin_bottom=[self.pageTemplate[@"margin_bottom"] doubleValue];
- self.header=self.pageTemplate[@"header"];
- self.footer=self.pageTemplate[@"footer"];
-
-
- self.drawable_rect = CGRectMake(self.rect.origin.x+self.margin_left, self.rect.origin.y+self.margin_top, self.rect.size.width-(self.margin_right+self.margin_left), self.rect.size.height-(self.margin_bottom+self.margin_top));
-
- self.pos_x = _margin_left;
- self.pos_y = _margin_top;
-
- // self.rect = CGRectMake(0, 0, [self.pageTemplate[@"width"] doubleValue], [self.pageTemplate[@"height"] doubleValue]);
- }
- -(int) QueryPageCount:(CGContextRef) context dataSource:(NSMutableDictionary*)data
- {
-
-
-
-
- self.data=data;
- GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
-
-
- self.header_height=pageHeader.rect.size.height;
- pageHeader.delegate = self;
- GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
-
- pageFooter.delegate = self;
-
-
-
-
- // while (true)
- {
- // page 去除页眉,页脚后的区域
- NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.origin.y+self.drawable_rect.size.height-pageFooter.rect.size.height);
-
- // CGRect rect = self.rect;
-
- // CGContextBeginPage(context, &rect);
- // [pageHeader Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
-
-
- for (int i=0;i<[self.pageTemplate[@"contents"][@"count"] intValue];i++)
- {
- NSMutableDictionary * item =self.pageTemplate[@"contents"][ [NSString stringWithFormat:@"item_%d",i ] ];
- PDFDrawable* drawable_obj=nil;
- if([item[@"type"] isEqualToString:@"img"])
- {
- drawable_obj= [[ImageDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"line"])
- {
- drawable_obj= [[LineDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"text"])
- {
- drawable_obj= [[TextDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"group"])
- {
- drawable_obj= [[GroupDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"table"])
- {
- drawable_obj= [[TableDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"grid"])
- {
- drawable_obj= [[GridDrawable alloc] init:item];
-
- }
- drawable_obj.delegate = self;
- [drawable_obj Query_Rect:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
- }
-
- // [pageFooter Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
- // CGContextEndPage(context);
-
- }
-
- return self.pagecount;
- }
- -(void) DirectDraw:(CGContextRef) context page:(CGPDFPageRef)page
- {
-
-
-
- // CGAffineTransform m;
- // m = CGPDFPageGetDrawingTransform (page, kCGPDFCropBox, self.rect, -180, true);
- CGContextSaveGState (context);
-
- // CGContextTranslateCTM(myContext, 80, self.frame.size.height);
- // CGContextScaleCTM(myContext, 1, -1);
-
- CGContextDrawPDFPage (context, page);
- CGContextRestoreGState (context);
-
-
- }
- -(void) DirectDraw:(CGContextRef) context drawable:(PDFDrawable*)drawable_obj
- {
-
-
- if(drawable_obj==nil)
- return;
-
- NSLog(@"pdfpage directdraw begin");
-
- NSRange range = NSMakeRange(0,self.rect.size.height);
-
- // CGRect rect = self.rect;
-
- drawable_obj.delegate = self;
- [drawable_obj Draw:context dataSource:nil ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
-
- NSLog(@"pdfpage directdraw end");
-
- }
- -(void) Draw:(CGContextRef) context dataSource:(NSMutableDictionary*)data
- {
-
-
-
-
- self.data=data;
-
-
-
- GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
- self.header_height=pageHeader.rect.size.height;
- pageHeader.delegate = self;
- GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
-
- pageFooter.delegate = self;
-
-
-
-
- // while (true)
- {
- // page 去除页眉,页脚后的区域
- NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, /*self.drawable_rect.origin.y+*/self.drawable_rect.size.height-pageFooter.rect.size.height-pageHeader.rect.size.height);
- // NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.size.height-pageFooter.rect.size.height+self.drawable_rect.size.height);
- CGRect rect = self.rect;
-
- CGContextBeginPage(context, &rect);
- [pageHeader Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
-
- [pageFooter Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
-
- for (int i=0;i<[self.pageTemplate[@"contents"][@"count"] intValue];i++)
- {
- NSMutableDictionary * item =self.pageTemplate[@"contents"][ [NSString stringWithFormat:@"item_%d",i ] ];
- PDFDrawable* drawable_obj=nil;
- if([item[@"type"] isEqualToString:@"img"])
- {
- drawable_obj= [[ImageDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"line"])
- {
- drawable_obj= [[LineDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"text"])
- {
- drawable_obj= [[TextDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"group"])
- {
- drawable_obj= [[GroupDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"table"])
- {
- drawable_obj= [[TableDrawable alloc] init:item];
-
- }
- else if([item[@"type"] isEqualToString:@"grid"])
- {
- drawable_obj= [[GridDrawable alloc] init:item];
-
- }
- drawable_obj.delegate = self;
- [drawable_obj Draw:context dataSource:data ParentRect:(CGRect)self.drawable_rect startX:0 startY:self.header_height flipHeight:self.rect.size.height range:range];
- }
-
- CGContextEndPage(context);
-
- }
- }
- -(void) nextPage:(CGContextRef) context
- {
- CGContextEndPage(context);
- CGRect rect = self.rect;
-
- CGContextBeginPage(context, &rect);
-
-
-
- GroupDrawable* pageHeader = [[GroupDrawable alloc]init:self.header];
- pageHeader.delegate = self;
- GroupDrawable* pageFooter = [[GroupDrawable alloc]init:self.footer];
- pageFooter.delegate = self;
-
- self.data[@"CURRENT_PAGE"]=[NSNumber numberWithInt:[self.data[@"CURRENT_PAGE"] intValue]+1];
-
- // page 去除页眉,页脚后的区域
- NSRange range = NSMakeRange(self.drawable_rect.origin.y+pageHeader.rect.size.height, self.drawable_rect.origin.y+self.drawable_rect.size.height-pageFooter.rect.size.height);
-
-
- [pageHeader Draw:context dataSource:self.data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
-
-
-
-
-
- [pageFooter Draw:context dataSource:self.data ParentRect:(CGRect)self.drawable_rect startX:0 startY:0 flipHeight:self.rect.size.height range:range];
-
-
- }
- #pragma mark - commoneditor delegate
- -(void) outofPage:(CGContextRef) context
- {
- [self nextPage:context];
- }
- -(void) addPageCount:(CGContextRef)context
- {
- self.pagecount++;
- }
- @end
|