zoukankan      html  css  js  c++  java
  • PDF转jpg

     /Users/mac/Library/Developer/CoreSimulator/Devices/38BB3233-7551-4DBE-A3D9-58934568818D/data/Containers/Data/Application/B4866C5A-B68E-437F-A787-8C83FC7D13D2/Documents/

    上面模拟器Documents文件里添加pdf文件test.pdf

    调用:

    [self createJPGsFromPDF:test.pdf];

    -(void) createJPGsFromPDF:(NSString *)fromPDFName

    {

        

        if (fromPDFName == nil || [fromPDFName isEqualToString:@""]) {

            return;

        }

        

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *documentsDir = [paths objectAtIndex:0];

        

        NSString *docPath = [documentsDir stringByAppendingPathComponent:fromPDFName];

        

        

        NSURL *fromPDFURL = [NSURL fileURLWithPath:docPath];

        CGPDFDocumentRef fromPDFDoc = CGPDFDocumentCreateWithURL((CFURLRef) fromPDFURL);

       

        // Get Total Pages

        float pages = CGPDFDocumentGetNumberOfPages(fromPDFDoc);

        

        // Create Folder for store under "Documents/"

        NSError *error = nil;

        NSFileManager *fileManager = [[NSFileManager alloc] init];

        NSString *folderPath = [documentsDir stringByAppendingPathComponent:[fromPDFName stringByDeletingPathExtension]];

        [fileManager createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];

        

        int i = 1;

        for (i = 1; i <= pages; i++) {

            CGPDFPageRef pageRef = CGPDFDocumentGetPage(fromPDFDoc, i);

            CGPDFPageRetain(pageRef);

            

            // determine the size of the PDF page

            CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox);

            

            // renders its content.

            UIGraphicsBeginImageContext(pageRect.size);

            

            CGContextRef imgContext = UIGraphicsGetCurrentContext();

            CGContextSaveGState(imgContext);

            CGContextTranslateCTM(imgContext, 0.0, pageRect.size.height);

            CGContextScaleCTM(imgContext, 1.0, -1.0);

            CGContextSetInterpolationQuality(imgContext, kCGInterpolationDefault);

            CGContextSetRenderingIntent(imgContext, kCGRenderingIntentDefault);

            CGContextDrawPDFPage(imgContext, pageRef);

            CGContextRestoreGState(imgContext);

            

            //PDF Page to image

            UIImage *tempImage = UIGraphicsGetImageFromCurrentImageContext();

            

            UIGraphicsEndImageContext();

            //Release current source page

            CGPDFPageRelease(pageRef);

            

            // Store IMG

            NSString *imgname = [NSString stringWithFormat:@"fromPDFName_%d.jpg", i];

            NSString *imgPath = [folderPath stringByAppendingPathComponent:imgname];

            [UIImageJPEGRepresentation(tempImage, 1.0) writeToFile:imgPath atomically:YES];

            

        }

        

        CGPDFDocumentRelease(fromPDFDoc);

        

    }

  • 相关阅读:
    [Error]错误 C2660: Gdiplus::GdiplusBase::operator new: 函数不带三个参数
    opengl多线程的问题
    去掉CFormView的滚动条
    DevIL库使用时图片翻转的问题
    让notepad++正确显示actionscript文件语法高亮
    [Error]world geometry is not supported by the generic scenemanager
    3d Max 9的"正在验证许可证"问题的解决
    CSizingControlBar Error C2440: “static_cast”: 无法从“UINT (__thiscall CSizingControlBarG::* )(CPoint)”转换为>>>
    MFC下的OpenGL
    酷!不用外挂,Win7资源监视器查看QQ好友IP
  • 原文地址:https://www.cnblogs.com/niexiaobo/p/4724530.html
Copyright © 2011-2022 走看看