旋转PDF文档,一段时间后缩小

时间:2011-06-30 08:16:01

标签: iphone pdf pdf-generation

我在旋转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;
}

2 个答案:

答案 0 :(得分:2)

经过大量的狩猎后,我发现Sorin(iPDFdev)rotation code是最好的。它负责所有四个方向。

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

rotate的结果为CGPDFPageGetRotationAnglecropBox来自CGPDFPageGetBoxRect。您可以从博客文章中获取更多信息。

答案 1 :(得分:0)

这是快速语言中旋转 Pdf 文档的最佳解决方案

 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
        
        
    }