//
//  PDFPageView.m
//  AntsContract
//
//  Created by Ray on 12/16/16.
//  Copyright © 2016 United Software Applications, Inc. All rights reserved.
//

#import "PDFPageView.h"

@implementation PDFPageView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    
            //CGPDFDocumentRef document = [self OpenPDF:@"GEIC - Home Improvement Contract 2016.pdf"];
    
    
    


    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
    [self MyDisplayPDFPage:context index:self.pageIndex ];//file:@"GEIC - Home Improvement Contract 2016.pdf"];
}



-(void) MyDisplayPDFPage:(CGContextRef) myContext index:(size_t)pageNumber //file:(NSString*) filename//(,  , const char *filename)
{
    
    
    {
    CGPDFPageRef page = self.pageRef;

    CGContextSaveGState (myContext);
    
                CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
        
//                CGContextTranslateCTM(myContext, 0, mediabox.size.height);
//                CGContextScaleCTM(myContext, 1, -1);

        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, self.frame, 0, true);
        CGContextConcatCTM(myContext, pdfTransform);
        
        CGContextScaleCTM(myContext, 1, -1);
        CGContextTranslateCTM(myContext, 0, -self.frame.size.height/pdfTransform.a);
        

    CGContextDrawPDFPage (myContext, page);
    CGContextRestoreGState (myContext);
    }
    
    
    
    
    
//    CGPDFPageRef page = self.pageRef;
//
//    CGContextSaveGState (myContext);
//    
//    CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
//    
//    CGContextTranslateCTM(myContext, 0, mediabox.size.height);
//    // Reverse the Y axis to grow from bottom to top.
//    CGContextScaleCTM(myContext, 1, -1);
//    
//    CGContextDrawPDFPage (myContext, page);
//    CGContextRestoreGState (myContext);
//    return ;
    
//    {
//        
//        // CGPDFDocumentRef document;
//        CGPDFPageRef page = self.pageRef;
//
//        CGContextSaveGState (myContext);
//        
//        
//        CGRect mediabox= CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
//        
//        CGContextTranslateCTM(myContext, 0, mediabox.size.height);
//        CGContextScaleCTM(myContext, 1, -1);
//        
//        CGContextDrawPDFPage (myContext, page);
//        CGContextRestoreGState (myContext);
//    }
    
    
    
    
    
 /*
   // CGPDFDocumentRef document;
    CGPDFPageRef page = self.pageRef;
 //   document = [self OpenPDF:filename];                                   // 1 创建PDFDocument对象
  //  page = CGPDFDocumentGetPage (document, pageNumber);           // 2 获取指定页的PDF文档
    
    
//    CGAffineTransform m;
//    m = CGPDFPageGetDrawingTransform (page, kCGPDFMediaBox, self.bounds, -180, true);
    CGContextSaveGState (myContext);
    
    CGContextTranslateCTM(myContext, 80, self.frame.size.height);
    CGContextScaleCTM(myContext, 1, -1);
    
 //   CGContextConcatCTM (myContext, m);
  //  CGContextClipToRect (myContext,CGPDFPageGetBoxRect (page, kCGPDFCropBox));
    CGContextDrawPDFPage (myContext, page);
    CGContextRestoreGState (myContext);
    */
    
  //  CGContextDrawPDFPage (myContext, page);                                       // 3 将PDF绘制到图形上下文中
//    CGPDFDocumentRelease (document);
}
@end
