2011-06-30 8 views
2

PDF文書の回転に問題があります。私は文書を回転させることができますが、回転の後に文書の寸法が小さくなります。何が間違っているのか分かりません。ここに私のコードは次のとおりです。PDF文書を回転すると、しばらくして縮小します

//////// Creating document and pdfContext related code ////// 

-(IBAction)rotate 
{ 

     BOOL ok = [self CopyFileIfNotExists:@"Untitled.pdf"]; 

    fileLoc = [self PathForFile:@"Untitled.pdf"]; 
    angle = angle + 90; 
    pdfDoc = [self MyGetPDFDocumentRef:[fileLoc UTF8String]]; 
    CGPDFPageRef page = CGPDFDocumentGetPage (pdfDoc, 1); 
    //CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
    CGRect pageRect = CGRectMake(0, 0, 612, 792); 

    [self MyCreatePDFFile:pageRect :[fileLoc UTF8String]]; 
} 

-(void) MyCreatePDFFile :(CGRect)pageRect :(const char *)filename 
{ 

    CFStringRef path; 
    CFURLRef url; 
    CFMutableDictionaryRef myDictionary = NULL; 

    path = CFStringCreateWithCString (NULL, filename,       kCFStringEncodingUTF8); 
    url = CFURLCreateWithFileSystemPath (NULL, path, 
             kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    myDictionary = CFDictionaryCreateMutable(NULL, 0, 
              &kCFTypeDictionaryKeyCallBacks, 
              &kCFTypeDictionaryValueCallBacks); 
    CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); 
    CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); 
    pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); 
    CFRelease(myDictionary); 
    CFRelease(url); 
    CGContextBeginPage (pdfContext, &pageRect); 
    [self myDrawContent:pdfContext]; 
    CGContextEndPage (pdfContext); 
    CGContextRelease (pdfContext); 
} 

-(void)myDrawContent:(CGContextRef)context 
{ 


    int noOfPages = CGPDFDocumentGetNumberOfPages(pdfDoc); 

    CGRect pageRect = CGRectMake(0, 0, 612, 792); 
    for(int i = 1 ; i <= noOfPages ; i++) 
    { 
     CGPDFPageRef page = CGPDFDocumentGetPage (pdfDoc, i); 
     //pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
     //[self MyDisplayPDFPage:pdfContext :i :[fileLoc UTF8String]]; 
     [self MyDrawPDFPageInRect:pdfContext :page :kCGPDFMediaBox :pageRect :angle :true]; 
    } 

} 

-(void) MyDrawPDFPageInRect:(CGContextRef)context :(CGPDFPageRef)page :(CGPDFBox)box :(CGRect)rect :(int)rotation :(bool)preserveAspectRatio 
{ 



//////// this is rotating code of PDF /// 


     CGAffineTransform m;  
    m = CGPDFPageGetDrawingTransform (page, box, rect, rotation, preserveAspectRatio); 
    CGContextSaveGState (context); 
    CGContextConcatCTM (context, m); 
    CGRect pageframe = CGPDFPageGetBoxRect (page, box); 
    CGContextClipToRect (context,pageframe); 
    CGContextDrawPDFPage (context, page); 
    CGContextRestoreGState (context); 
} 


-(CGPDFDocumentRef) MyGetPDFDocumentRef: (const char *)filename 
{ 

    CFStringRef path; 
    CFURLRef url; 
    CGPDFDocumentRef document; 
    path = CFStringCreateWithCString (NULL, filename,kCFStringEncodingUTF8); 
    url = CFURLCreateWithFileSystemPath (NULL, path,         kCFURLPOSIXPathStyle, 0); 
    CFRelease (path); 
    document = CGPDFDocumentCreateWithURL (url); 
    CFRelease(url); 
    int count = CGPDFDocumentGetNumberOfPages (document); 
    if (count == 0) { 
     printf("`%s' needs at least one page!", filename); 
     return NULL; 
    } 
    return document; 
} 

答えて

3

狩猟の多くはソリンさん(iPDFdev)rotation codeがベストであることを、私を見つけた後。それは4つの方向すべてを処理します。

switch (rotate) { 
     case 0: 
      CGContextTranslateCTM(context, 0, cropBox.size.height); 
      CGContextScaleCTM(context, 1, -1); 
      break; 
     case 90: 
      CGContextScaleCTM(context, 1, -1); 
      CGContextRotateCTM(context, -M_PI/2); 
      break; 
     case 180: 
     case -180: 
      CGContextScaleCTM(context, 1, -1); 
      CGContextTranslateCTM(context, cropBox.size.width, 0); 
      CGContextRotateCTM(context, M_PI); 
      break; 
     case 270: 
     case -90: 
      CGContextScaleCTM(context, 1, -1); 
      CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width); 
      CGContextRotateCTM(context, M_PI/2); 
      break; 
    } 
rotateCGPDFPageGetRotationAngleの結果で

cropBoxCGPDFPageGetBoxRectからです。あなたはブログの投稿からより多くの情報を得ることができます。

+0

ご返信ありがとうございます。 –

+0

完全に働いた。正確に私が必要としたもの。 – vagrant

関連する問題