Load PDF in iPhone app

Source: Internet
Author: User

Original article: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/


This section describes how to load PDF files from the server to iOS programs.

In quicklook frameworkQlpreviewcontrollerClass. To use this class, you can view the following documents:

A) iwork document
B) Microsoft Office documents
C) RTF document
D) PDF document
E) Image
F) Text File
G) CSV (separated by commas)


In this tutorial, I will display a PDF file, which may be placed anywhere: Server or application bundle. But for better demonstration, I will get this file from the server.

Step 1: Open xcode to create the following interface:

Step 2: add the quick look framework to the project and import the corresponding header file in the. h file of View Controller: # import <quicklook/quicklook. h>. Create a qlpreviewcontroller instance in the didselectrowatindex method of table view, and specify its datasource and delegate.

-(Void) tableview :( uitableview *) tableviewdidselectrowatindexpath :( nsindexpath *) indexpath {


// Use different URL strings to initialize fileurl objects

Switch (indexpath. Row ){

Case 0:

Fileurl = [nsurl urlwithstring: kstringurlviewcontrollerpdf];



Case 1:

Fileurl = [nsurl urlwithstring: kstringurlqlpreviewcontrollerpdf];


Case 2:

Fileurl = [nsurl urlwithstring: kstringurluidocumentinteractioncontrollerpdf];



// Create a qlpreviewcontroller instance

Qlpreviewcontroller * previewcontroller = [[qlpreviewcontrolleralloc] init];


// Set the datasource attribute to self

Previewcontroller. datasource = self;


// Press qlpreviewcontroller into the navigationcontroller stack.

[[Self navigationcontroller] pushviewcontroller: previewcontroller animated: Yes];



Code Description:

The main code has been annotated. We initialized the filurl object, which is an nsurl and will be used as the datasource of qlpreviewcontroller to load the PDF file.


Step 3: implement the qlpreviewcontroller datasource method. In this method, the PDF is displayed.

# Pragma markqlpreviewcontrollerdatasource


// Returns the number of preview controllers to be previewed.

-(Nsinteger) numberofpreviewitemsinpreviewcontroller :( qlpreviewcontroller *) previewcontroller





// Return the content to be previewed by the preview controller.

Returns the item thatthe preview controller shoshould Preview

-(ID) previewcontroller :( qlpreviewcontroller *) previewcontrollerpreviewitematindex :( nsinteger) idx


Return fileurl;


Code Description:

Numberofpreviewitemsinpreviewcontroller: This method notifies quick look preview controller of the number of items in the preview navigation window (required ).

Previewitematindex: The method returns the specified entry based on the index (required ).

Step 4: add the view to the navigation controller, and then add the navigation controller to the window. You can also choose not to use the navigation controller.

Step 5: run the program and view the PDF's

You can scale and print PDF files.

You can also use uidocumentinteractioncontroller, but it is a bit complicated and I think quick look is simpler.

Source code: Here.

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.