Ios--pdf Display (CGPDFDOCUMENTREF)

Source: Internet
Author: User

Summary: Learn to display PDFs using custom view loading;

Objective

Preview pdf file in iOS, display PDF file generally use two ways, one is UIWebView, how to say the advantage is simple or simple, directly display the PDF file, the other is a custom UIView, With Cgpdfdocumentref read the contents of the PDF file, in the custom DrawRect method to depict the obtained PDF content; It's apple. After IOS4.0, Apple launches a new file preview control: Qlpreveiewcontroller, which supports PDF file reading. Today I mainly write a custom view+cgpdfdocumentref display PDF file;

(a) The use of Cgpdfdocumentref to obtain the specified PDF content; The following is the method I wrote to get the content;

1 //for local PDF files2-(Cgpdfdocumentref) Pdfrefbyfilepath: (NSString *) Afilepath {3 cfstringref Path;4 cfurlref URL;5 Cgpdfdocumentref document;6     7Path =cfstringcreatewithcstring (NULL, [Afilepath utf8string], kCFStringEncodingUTF8);8URL =Cfurlcreatewithfilesystempath (NULL, Path, Kcfurlposixpathstyle, NO);9Document =cgpdfdocumentcreatewithurl (URL);Ten      One cfrelease (path); A cfrelease (URL); -      -     returndocument; the } -  --(NSString *) Getpdfpathbyfile: (NSString *) FileName { -     return[[NSBundle Mainbundle] pathforresource:filename ofType:@". pdf"]; + } -  + //for Web PDF files A-(Cgpdfdocumentref) Pdfrefbydatabyurl: (NSString *) Afileurl { atNSData *pdfdata =[NSData Datawithcontentsoffile:afileurl]; -Cfdataref Dataref =(__bridge_retained cfdataref) (pdfdata); -      -Cgdataproviderref Proref =Cgdataprovidercreatewithcfdata (dataref); -Cgpdfdocumentref Pdfref =Cgpdfdocumentcreatewithprovider (proref); -      in cgdataproviderrelease (proref); - cfrelease (dataref); to      +     returnPdfref; -}

Parses a PDF file through a file path, returning the contents of the file.

(b) The second step is to draw the PDF content in a custom view;

- (void) DrawRect: (cgrect) Rect {cgcontextref context=Uigraphicsgetcurrentcontext (); Cgpdfpageref PageRef= Cgpdfdocumentgetpage ([self createpdffromexistfile], _pagenumber);//gets the contents of the specified page, such as _pagenumber=1, for the first page of the PDFCGRect mediarect = Cgpdfpagegetboxrect (PageRef, Kcgpdfcropbox);//rect for PDF contentCgcontextretain (context);        Cgcontextsavegstate (context); [[Uicolor Whitecolor]Set]; Cgcontextfillrect (context, rect);//Fill background color, otherwise full black;CGCONTEXTTRANSLATECTM (Context,0, rect.size.height);//set Displacement, x, y;cgfloat under_bar_height=64.0f; CGCONTEXTSCALECTM (context, Rect.size.width/MediaRect.size.width,-(rect.size.height + under_bar_height)/mediaRect.size.height);    Cgcontextsetinterpolationquality (context, Kcginterpolationhigh);    Cgcontextsetrenderingintent (context, kcgrenderingintentdefault); Cgcontextdrawpdfpage (context, PAGEREF);//Draw a PDFcgcontextrestoregstate (context); Cgcontextrelease (context);}

The above is the code to draw the contents of the PDF, each line of code I wrote a note, in fact, are very well understood.

Of course, the above only shows the logic of the specified page of the PDF, in order to display the entire contents of the PDF file, need to cooperate with Uiscrollview or uipageviewcontroller to achieve, the principle is very simple, combined with the above logic as long as the input of a pageindex value, Refresh the content of the page can be achieved. Here is a case I wrote, Pageviewcontroller realization;

- (void) viewdidload {[Super viewdidload]; Self.pdfarr=[Nsmutablearray array]; Nsdictionary*options =[nsdictionary dictionarywithobject:[nsnumber numberwithinteger:uipageviewcontrollerspinelocationmin] ForKey:    Uipageviewcontrolleroptionspinelocationkey]; SELF.PAGEVC=[[Uipageviewcontroller Alloc]initwithtransitionstyle:uipageviewcontrollertransitionstylepagecurl    Navigationorientation:uipageviewcontrollernavigationorientationhorizontal Options:options]; _pagevc.view.frame=Self.view.bounds; _pagevc.Delegate=Self ; _pagevc.datasource=Self ;        [Self ADDCHILDVIEWCONTROLLER:_PAGEVC]; Pdfview*testpdf = [[Pdfview alloc]initwithframe:self.view.frame Atpage:1Andfilename:@"Reader"]; Cgpdfdocumentref Pdfref=[Testpdf Createpdffromexistfile]; size_t Count= Cgpdfdocumentgetnumberofpages (PDFREF);//This position is mainly to get the number of PDF pages;         for(inti =0; I < count; i++) {Uiviewcontroller*PDFVC =[[Uiviewcontroller alloc] init]; Pdfview*pdfview = [[Pdfview alloc] initWithFrame:self.view.frame atpage: (i+1) Andfilename:@"Reader"];                [Pdfvc.view Addsubview:pdfview];    [_pdfarr ADDOBJECT:PDFVC]; } [_PAGEVC Setviewcontrollers:[nsarray arraywithobject:_pdfarr[0] ] Direction:uipageviewcontrollernavigationdirectionforward animated:yes Completion:nil];    [Self.view Addsubview:_pagevc.view]; }//delegation method;-(Uiviewcontroller *) Viewcontrolleratindex: (Nsinteger) index{//Create A new view controller and pass suitable data.        if([_pdfarr count] = =0)|| (Index >[_pdfarr Count])) {        returnNil; } NSLog (@"index =%ld",(Long) (index); return(Uiviewcontroller *) _pdfarr[index];}-(Nsuinteger) Indexofviewcontroller: (Uiviewcontroller *) viewcontroller{return[Self.pdfarr Indexofobject:viewcontroller];}-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerafterviewcontroller: (Uiviewcontroller *) viewcontroller{Nsuinteger index= [Self Indexofviewcontroller: (Uiviewcontroller *) Viewcontroller]; if(Index = =nsnotfound) {        returnNil; } Index++; if(Index = =[_pdfarr Count]) {        returnNil; }        return[self viewcontrolleratindex:index];}-(Uiviewcontroller *) Pageviewcontroller: (Uipageviewcontroller *) Pageviewcontroller Viewcontrollerbeforeviewcontroller: (Uiviewcontroller *) viewcontroller{Nsuinteger index= [Self Indexofviewcontroller: (Uiviewcontroller *) Viewcontroller]; if(Index = =0) || (Index = =nsnotfound)) {        returnNil; } Index--; return[self viewcontrolleratindex:index];}

The above is a simple implementation;

(c) For the third method, using Qlpreveiewcontroller to preview the PDF, I have no specific code to achieve a preview of the PDF, the network should have resources to check;

The use of Qlpreveiewcontroller, it is easy to say how to preview the PDF content, it is very simple, if you want to customize the display, you need to take the time to study.

Summarize

Whichever way you choose, you can load the PDF file; WebView In addition to simple rough but only as a simple browsing, cgpdfdocumentref need developers to implement the display logic, but this controllability will be better, the display of the effect can be customized, Qlpreveiewcontroller words, I do not have to realize, so we can go to try.

Ios--pdf Display (CGPDFDOCUMENTREF)

Related Article

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.