开发者

Rotating PDF document, shrinks after a while

开发者 https://www.devze.com 2023-03-16 21:27 出处:网络
I have a problem in rotating the PDF document. I am able to rotate the document, butafter some rotations the document is diminishing its dimensions. I can\'t find what\'s wrong. Here\'s my code:

I have a problem in rotating the PDF document. I am able to rotate the document, but after some rotations the document is diminishing its dimensions. I can't find what's wrong. Here's my code:

//////// 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;
}


After a lot of hunting I've found Sorin's (iPDFdev) rotation code to be the best. It takes care of all four orientations.

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;
    }

Where rotate is the result of CGPDFPageGetRotationAngle and cropBox is from CGPDFPageGetBoxRect. You can get more info from the blog post.


This is best solution in swift language to Rotate Pdf Document

 func rotate(pdfsUrl:URL,rotate: Rotate_PDf)  -> Data? {
        
        let data = NSMutableData()
        
        do{
            let pdfData = try Data(contentsOf: pdfsUrl)
            if let pdf = CGPDFDocument(CGDataProvider(data: pdfData as CFData)!) {
                
                autoreleasepool {
                    let pageCount = pdf.numberOfPages
                    UIGraphicsBeginPDFContextToData(data, .zero, nil)
                    
                    for index in 0...pageCount {
                        
                        let page = pdf.page(at: index)
                        
                        if let pageRect = page?.getBoxRect(CGPDFBox.mediaBox){
                            
                            var newRect = pageRect
                            switch (rotate) {
                            case .Degree90,.Degree270:
                                newRect.size = CGSize(width: newRect.size.height, height: newRect.size.width)
                                break
                            default:
                                break
                            }
                            UIGraphicsBeginPDFPageWithInfo(newRect, nil)

                            if let ctx = UIGraphicsGetCurrentContext(){
                                
                                ctx.interpolationQuality = .high
                                ctx.saveGState()
                                
                                switch (rotate) {
                                case .DegreeDefault:
                                    
                                    ctx.translateBy(x: 0, y: (pageRect.size.height))
                                    ctx.scaleBy(x: 1, y: -1)

                                    break;
                                case .Degree90:

                                    ctx.translateBy(x: (pageRect.size.height), y: (pageRect.size.width))
                                    ctx.rotate(by: CGFloat(Double.pi/2))
                                    ctx.scaleBy(x: -1, y: 1)
                                    
                                    break;
                                case .Degree180:
                                
                                    ctx.scaleBy(x: 1, y: -1)
                                    ctx.translateBy(x: pageRect.size.width, y: 0)
                                    ctx.rotate(by: CGFloat(Double.pi))

                                    break;
                                case .Degree270:
                                
                                    ctx.translateBy(x: 0, y: 0)
                                    ctx.rotate(by: CGFloat(Double.pi/2))
                                    ctx.scaleBy(x: 1, y: -1)

                                    break;
                                }
                                
                                ctx.drawPDFPage(page!)
                                ctx.restoreGState()
                            }
                        }
                        
                    }
                    
                    UIGraphicsEndPDFContext()
                }
            }
        }catch{
            
        }
        
        
        return data as Data
        
        
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号