PDF to JPG

Source: Internet
Author: User

/users/mac/library/developer/coresimulator/devices/38bb3233-7551-4dbe-a3d9-58934568818d/data/containers/data/ application/b4866c5a-b68e-437f-a787-8c83fc7d13d2/documents/

Add PDF files to the above emulator documents file Test.pdf

Call:

[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);

}

PDF to JPG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.