Simple iOS PDF Reader

PDF is very frequently used document type. Especially when you want the read-only document. Many time we need to display PDF in our application. Fortunately, iOS has very Good built-in support for creating and display PDF. If we talk about display only we have many alternatives for that:

  • Web view (WKWebView/ UIWebView)
  • UIDocumentInteractionController
  • Third Party PDF reader

Update: iOS 11 gave us a new way to work with PDF – PDFKit. It was on MacOS and now it is on iOS also. If your min app target is iOS 11, I suggest to go for PDFKit instead of this. I will make an post on PDFKit very shortly.

Inbuilt options are a good choice when you just need to display PDF. But if you need more control over PDF you can go with third party reader which provide many features. But sometimes you need in between something or something which is not already provided like different animation, Different page size or something specific to your application. In that kind of situation, you can make your own PDF reader and it is not as hard as it sounds. (Actually it is pretty easy).

So today we will create something like this:

It is basic PDF reader which load local PDF or download from the internet and show it as page curl animation. You can download this from here: PDF-Reader

Let’s see what we need to build this demo:

PDFPageView:

This class is the heart of the application. PDFPageView renders a single page of PDF. It needs an object of CGPDFDocument and page number. We have used in built functions provided by Core Graphics to render PDF. Let’s see code:

class PDFPageView: UIView
{
    var PDFDocument:CGPDFDocument? = nil
    var pageNumber:Int? = nil;

    override func draw(_ rect: CGRect)
    {
        if PDFDocument != nil
        {
            let ctx = UIGraphicsGetCurrentContext()

            UIColor.white.set()

            ctx?.fill(rect)

            _ = ctx?.ctm
            _ = ctx?.scaleBy(x: 1, y: -1)
            _ = ctx?.translateBy(x: 0, y: -rect.size.height)

            let page = PDFDocument?.page(at: pageNumber!)

            let pageRect = page?.getBoxRect(CGPDFBox.cropBox)

            let ratioW = rect.size.width / (pageRect?.size.width)!
            let ratioH = rect.size.height / (pageRect?.size.height)!

            let ratio = min(ratioW, ratioH)

            let newWidth = (pageRect?.size.width)! * ratio;
            let newHeight = (pageRect?.size.height)! * ratio;

            let offsetX = (rect.size.width - newWidth);
            let offsetY = (rect.size.height - newHeight);

            ctx?.scaleBy(x: newWidth / (pageRect?.size.width)!, y: newHeight / (pageRect?.size.height)!)
            ctx?.translateBy(x: -(pageRect?.origin.x)! + offsetX, y: -(pageRect?.origin.y)! + offsetY)

            ctx?.drawPDFPage(page!)
        }
    }
}

Here the most line of code is to set proper drawing position. If we forget size and position for sometimes we need only the following code to render PDF.

let page = PDFDocument?.page(at: pageNumber!)
ctx?.drawPDFPage(page!)

Of corse that will not look good, that’s why we have done some code to set proper position of page. But now you know that “Drawing PDF is an easy task”.

PDFPageViewController:

In this demo PDFPageViewController is responsible to create an object of PDFPageView. Because we need zoom we will use UIScrollView. So PDFPageViewController has ScrollView and inside that scroll view we have PDFPageView. PDFPageViewController also pass object of CGPDFDocument and page number

PDFViewController:

This view controller does two things:

  1. Load PDF: If PDF is local, load from app bundle. If PDF is at the remote location, download PDF from that location. and Create object of CGPDFDocument.
  2. Prepare object of UIPageViewController to display PDFPageViewController as a page curl animation. We use UIPageViewController as a child view controller. This view controller decides which page to display inside PDFPageViewController.

PDFListViewController:

It just display list of available PDF in two section. Local PDF which are already in App Bundle and Remote PDF which are somewhere of internet. When user select any of this PDF app will redirect to PDFViewController with URL of PDF.

This is all you need to display PDF. Actually you just need PDFPageView. Like you can create small PDFPageView and display Thumb of PDF.

I hope this will help to create your own PDF reader to use in your next application or at least you get some PDF file for testing.

Reference:
  1. A UIPageViewController Tutorial for Swift
  2. Rendering PDF is easier than you thought | Cocoanetics
PDF Used In this Demo:
  1. Internet Security Threat Report
  2. Meet Our Solar System
  3. Our Solar System
  4. The Natural Choice
  5. Help Planet Earth
  6. McAfee Labs Threats Report
  7. European Software Development Salary Survey
  • Joshua Harris

    this is want I am looking for…. Thanks

  • Mihrimah

    Thank you so much for this amazing work!