PDFView in swift

Save

In the article, I will discuss about the PDFKit framework and how we use PDFView in our swift project.

The PDFKit is a framework available in AppKit since iOS 11.0 and macOS 10.4. Please click here to read more about PDFKit.

PDFView is mainly used to show the pdf file in our application and also it allows users to set zoom level, select content, navigate through a document, and copy the textual content to the Pasteboard, and also keeps track of page history.

Declaration

iOS, Mac Catalyst

macOS

Getting Started

Swift version: 5
iOS version: 13
Xcode: 11.3

First Create a project from Xcode.

File ▸ New ▸ Project…. Choose the iOS ▸ Application ▸ Single View App template and create a new project.

Then, we will create an object of PDFView and we will add this view inside the super view.

Now we load a pdf file from the app bundle.

We can Fit content in the pdfview  set autoScales true

Now run code and it will display Demo.pdf file in our app.

PDFView

Now we will add a watermark in the PDFView.

First we have to initializes a PDFDocument instance and sets its delegate to self.

Now, the delegate, of type PDFDocumentDelegate, implements a classForPage() method.

Now, implement WatermarkPage class and draw the watermark.

I hope this article helps you to display the pdf file in the project.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


3 comments

  • David
    How do you go about adding a button or some sort of way to exit this PDF subview?
    • Debabrata Roy (Moderator)
      Use a custom class and drawPagePost method.
      Exemple:
      class customPDF : PDFView {
           public override func drawPagePost(_ page: PDFPage, to context: CGContext) {
          let subview = UIView()
          self.documentView?.addSubview(subview)
          }
      }
  • Tobias
    How would you inject e.g. a userName from an external struct or class into the WatermarkPage class as the string to be used, when func classForPage() only returns the type WatermarkPage?

    Many thanks for your help & BR
    Tobias

  • css.php

    We've bought a Shopware Extension which is a hybrid App, which we can now offer in the Google Play Store and the IOS App Store. I'am still impressed how fast and how sharp the support team leads us through every process of the App-Store clarification. Very fast and useful response.

    Markus Walter

    Markus Walter

    Founder, Das Apartment Living

    Talk to Sales

    Global

    Live Chat
    Start a Project


      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home