Navigate E-books with ComPDFKit in Objective-C

Tutorials | E-books · Navigation · How To Thu. 01 Sep. 2022

When we read books on paper, we look over the catalog for the page numbers. Then, turn to the corresponding page manually. For e-books, the navigation function helps us to do that. Just by clicking, the content you want to read will display in front of your eyes.

 

The function of navigation can be achieved by links and bookmarks. ComPDFKit PDF SDK provides the functionality of navigation. Let’s see how to apply the navigation to the e-book industry in code methods.

 

 

The Needs of E-book Users

 

Even now, we may find some kinds of e-books without navigation. After you know the page number from the catalog, you can’t jump to specific pages immediately. That’s maddening. To figure these problems out, navigation is a pretty convenient and important function and is widely used in e-books.

 

Some writers may want to recommend another book to their readers. They can just add a link to the content. There are more situations for you to apply the navigation function such as catalog in front of the book contents, outline beside the book contents, links and bookmarks in the books, etc. 

 

 

Navigate by Links

 

The links are used in the books for internal and external linking. For internal links, we can add a link to a page, a point, or a specified rectangle on a page. This is usually used in reference, study guide e-books for connecting to related information. Here we will present how to add, delete, and exchange the links by code.

 

Navigate by Links with ComPDFKit

 

The picture above is a part of an e-book. I want to add a link to the words “ComPDFKit PDF SDK”. So that the page will jump to page 4 after clicking the words. The code methods are shown as follows.

 

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

CPDFSelection *selection = pdfView.currentSelection;
CPDFDestination *destination = [[CPDFDestination alloc] initWithDocument:document pageIndex:3];
CPDFLinkAnnotation *link = [[CPDFLinkAnnotation alloc] initWithDocument:document];
link.bounds = selection.bounds;
link.destination = destination;
[selection.page addAnnotation:link];

 

Now, the code below is for exchanging the links to let the page jump to page 10.

CPDFDestination *destination = [[CPDFDestination alloc] initWithDocument:document pageIndex:9];
link.destination = destination;



Navigate by Bookmarks

 

Bookmarks are a tool for readers to mark special places like the interesting parts of stories or the current reading page. When you want to see the section you marked, just click the bookmark. 

 

Take the following picture as an example. We also show you how to add, delete, or exchange a bookmark in the section of “Requirements”.

 

Navigate by Bookmarks with ComPDFKit

 

Add:

NSURL *url = [NSURL fileURLWithPath:@""];
CPDFDocument *document = [[CPDFDocument alloc] initWithURL:url];

CPDFSelection *selection = pdfView.currentSelection;
NSInteger pageIndex = [document indexForPage:selection.page];
[document addBookmark:@"Requirements" forPageIndex:pageIndex];

 

Delete:

[document removeBookmarkForPageIndex:pageIndex];

 

 

The Differences Between Links and Bookmarks

 

From now on, do you find the difference between links and bookmarks? As we mentioned above. They all can connect to another specific place. And the ability to turn to another page or content is achieved by links. The only difference is links are in the content of PDF files. But the bookmarks are detached from the text. 

 

That’s to say. Wherever you read, you can jump to another part by bookmarks. To make a bookmark detached from the text, CPDFBookmark will be needed. After knowing that, do you know what the outline is?

 

 

Final Words!

 

As we can see, bookmarks and links in ebooks make reading easier and more convenient. Here are more functions of reading PDFs. Contact us for a free trial.

Ready to Get Started?

Download our all-in-one ComPDFKit for free and run it to your project within minutes!