Tutorials

Read PDF with ComPDFKit for iOS

By ComPDFKit | Thu. 14 Nov. 2024
iOSObjective-C

There is no doubt that reading is the most basic function for opening PDF documents. For some products, it doesn't deserve spending time to focus on reading. However, for ComPDFKit PDF SDK, we endeavor to optimize our viewer to provide a better reading experience.

 

In this post, we will cover different features of our viewer and show you how to implement the viewer in your apps by interacting with our library via Objective-C code.

 

 

Main Features

 

The viewer provides a battle-hardened and high-quality rendering engine with a wide range of advanced functions as follows: 

 

- Standard page display modes, including: Scrolling, Double page, Crop mode, and Cover mode.

- Navigation with thumbnails, outlines, and bookmarks.

- Text search & selection.

- Zoom in and out & Fit-page.

- Switch between different themes, including: Dark mode, Sepia mode, Reseda mode, and Custom color mode.

- Text reflow.

 

 

Building Your Own PDF Viewer Solution

 

There is currently no reference code available, which is the primary challenge of building your own solution, meaning you’ll need to start from scratch when building.

 

Furthermore, as the PDF specification has more than 1,000 pages, we can see that PDF is an extremely complex file format. So that you need to get a general understanding of PDF before building to guarantee the process involving deployment, test, and refinement can be smoothly completed.

 

In addition, you have to struggle to maintain your solution after building. With each new release, you need to divert from working on current tasks to spending time fixing bugs.

 

With our SDK, you can get advanced features and professional technical support without consuming time.

 

 

Integrating Our Viewer in Objective-C

 

ComPDFKit PDF SDK offers developers a way to quickly embed a highly configurable PDF viewer in any iOS application.

 

Display Modes

 

- Scroll direction

 

Display Modes: Scroll direction 1

 

Display Modes: Scroll direction 2

 

- Two-up mode

 

Display Modes: Two-up mode

 

- Cover mode

 

Display Modes: Cover mode

 

- Crop mode

 

Display Modes: Crop mode

 

PDF Navigation

 

- Page navigation

After loading a PDF document, you can programmatically interact with it, which allows you to scroll to different pages or destinations. All of the interaction APIs are available on CPDFView.

Scrolls to the specified page, use function CPDFView::goToPageIndex:animated

Goes to the specified destination, destinations include a page and a point on the page specified in page space, use function CPDFView::goToDestination:animated

Goes to the specified rectangle on the specified page, use function CPDFView::goToRect:onPage:animated

This allows you to scroll the CPDFViewobject to a specific CPDFAnnotation or CPDFSelection object, because both of these objects have bounds methods that return an annotation or selection position in page space.

Note: This method’s rect is specified in page-space coordinates. Page space is a coordinate system with the origin at the lower-left corner of the current page.

 

 

- Outline

Outline allows users to quickly locate and link their point of interest within a PDF document. Each outline contains a destination or actions to describe where it links to. It is a tree-structured hierarchy, so function CPDFDocument::outlineRoot must be called first to get the root of the whole outline tree before accessing the outline tree. Here, “root outline” is an abstract object which can only have some child outline without next sibling outline and any data (includes outline data, destination data and action data). It cannot be shown on the application UI since it has no data. You can also use function CPDFDocument::setNewOutlineRoot to create a new root outline.

After the root outline is retrieved, the following functions can be called to access other outline:

To access the parent outline, use function CPDFOutline::parent

To access the child outline.

 

- (NSArray *)childOutline:(CPDFOutline *)outline {
    NSUInteger numberOfChildren = [outline numberOfChildren];
   NSMutableArray *child = [NSMutableArray array];
   for (int i=0; i<numberOfChildren; i++) {
[child addObject:[outline childAtIndex:i]];
}
    return child;
}

 

To insert a new outline, use function CPDFOutline::insertChildAtIndex:

To remove an outline, use function CPDFOutline::removeFromParent:

To move an outline, use function CPDFOutline::insetChild:atIndex:

When moving items around within an outline hierarchy, you should retain the item and call CPDFOutline::removeFromParent first.

 

- Bookmarks

Since each bookmark is associated with a specific page, it provides the ability to link to a different page in a document allowing the user to navigate interactively from one part of the document to another.

To access bookmarks, use function CPDFDocument::bookmarks.

To access a bookmark for page, use function CPDFDocument::bookmarkForPageIndex:.

To add a new bookmark, use function CPDFDocument::addBookmark:forPageIndex:.     

To remove a bookmark, use function CPDFDocument::removeBookmarkForPageIndex:.

 

Text Search & Selection

 

-Text search

ComPDFKit PDF SDK offers developers an API for programmatic full-text search, as well as UI for searching and highlighting relevant matches.

Asynchronously finds all instances of the specified string in the document, use function CPDFDocument::beginFindString:withOptions:, which returns immediately. It causes delegate methods to be called when searching begins and ends, on each search hit, and when the search proceeds to a new page.

 

/**
 * Called when the beginFindString:withOptions: or findString:withOptions:
method begins finding.
 */
- (void)documentDidBeginDocumentFind:(CPDFDocument *)document;
/**
 * Called when the beginFindString:withOptions: or findString:withOptions: method returns.
 */
- (void)documentDidEndDocumentFind:(CPDFDocument *)document;
/**
 * Called when a find operation begins working on a new page of a document.
 */
- (void)documentDidBeginPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
/**
 * Called when a find operation finishes working on a page in a document.
 */
- (void)documentDidEndPageFind:(CPDFDocument *)document pageAtIndex:(NSUInteger)index;
/**
 * Called when a string match is found in a document.
 *
 * @discussion To determine the string selection found, use the selection.
 */
- (void)documentDidFindMatch:(CPDFSelection *)selection;

 

Synchronously finds all instances of the specified string in the document, use function CPDFDocument::findString:withOptions: Each hit gets added to an NSArray object as a CPDFSelection object. If there are no hits, this method returns an empty array. Use this method when the complete search process will be brief and when you don’t need the flexibility or control offered by CPDFDocument::beginFindString:withOptions:

 

Cancels a search initiated with CPDFDocument::beginFindString:withOptions: , use function CPDFDocument::cancelFindString:.

Highlighting Search Results, CPDFView offers a way to both add and clear search results, use function CPDFView::setHighlightedSelection:animated:

 

- Text selection

PDF text contents are stored in CPDFPage objects which are related to a specific page. CPDFPage class can be used to retrieve information about text in a PDF page, such as single character, single word, text content within specified character range or bounds and more.

How to get the text bounds on a page by selection:

 

- (CPDFSelection *)selectionForPage:(CPDFPage *)page fromPoint:(CGPoint)fPoint toPoint:(CGPoint)tPoint {
NSInteger fCharacterIndex = [page characterIndexAtPoint:fPoint];
NSInteger tCharacterIndex = [page characterIndexAtPoint:tPoint];
   NSRange range = NSMakeRange(fCharacterIndex, tCharacterIndex - fCharacterIndex + 1);
   CPDFSelection *selection = [page selectionForRange:range];
   return selection;
}

 

Themes

 

- Dark mode

 

Themes: Dark mode

 

- Sepia mode

 

Themes: Sepia mode

 

- Reseda mode

 

Themes: Reseda mode

 

If you want to read more about our viewer and what you can do with it, please check out our guides.

 

 

Conclusion

 

Hope this blog can give you a clear understanding of our viewer. If you still have confusion or any suggestions, please contact support and we will reply within a business day.