ios PDFKit displaymode = singlepage only shows the first page of the pdf
Asked Answered
I

3

7

I'm trying to display a pdf on ios via apples PDFKit library, and rather than use PDFDisplayMode.singlePageContinuous mode, I want to stop at page breaks so I'm trying to use PDFDisplayMode.singlePage. https://developer.apple.com/documentation/pdfkit/pdfdisplaymode

However, this mode seems to only display one page of the pdf which is quite useless. I've tried adding swipe handlers to the page but they aren't working either.

I've found sample applications and altered their code to test the pdfdisplaymode but get the same problem e.g. https://github.com/vipulshah2010/PDFKitDemo

How can I implement a one page at a time pdfviewer with pdfkit, that allows swiping between pages?!

Incomplete answered 12/3, 2019 at 4:16 Comment(0)
J
18

A another simple way to do this is setting

pdfView.usePageViewController(true) 

This adds the swiping between pages for you and no need to set up your own gestures. See example below:

override func viewDidLoad() {
    super.viewDidLoad()

    // Add PDFView to view controller.
    let pdfView = PDFView(frame: self.view.bounds)
    self.view.addSubview(pdfView)

    // Configure PDFView to be one page at a time swiping horizontally
    pdfView.autoScales = true
    pdfView.displayMode = .singlePage
    pdfView.displayDirection = .horizontal
    pdfView.usePageViewController(true)

    // load PDF
    let webUrl: URL! = URL(string: url)
    pdfView.document = PDFDocument(url: webUrl!)
}
Jubilate answered 29/4, 2019 at 10:59 Comment(1)
Hi, this does not worked with twoup displayMode.Can you please help in this.Genitive
C
5

Use the swipe gesture recognizer (UISwipeGestureRecognizer) to let the user swipe the PDF view screen (PDFView) to the left and right.

import UIKit
import PDFKit

class ViewController: UIViewController, PDFViewDelegate {
    // MARK: - Variables

    // MARK: - IBOutlet
    @IBOutlet weak var pdfView: PDFView!

    // MARK: - Life cycle
    override func viewDidLoad() {
        super.viewDidLoad()

        let filePath = "/Users/george/Library/Developer/CoreSimulator/Devices/B5C5791C-3916-4BCB-8EB6-5D3D61C08DC0/data/Containers/Data/Application/4B644584-0025-45A7-9D71-C8F8478E4620/Documents/my PDF.pdf"
        pdfView.document = getDocument(path: filePath)
        pdfView.backgroundColor = .lightGray
        pdfView.autoScales = true
        pdfView.displayMode = .singlePageContinuous
        pdfView.usePageViewController(true, withViewOptions: nil)
        createMenu()
        thumbnail()

        /* swipe gesture */
        let leftSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(respondLeftSwipeGesture(_:)))
        leftSwipeGesture.direction = [UISwipeGestureRecognizer.Direction.left]
        self.view.addGestureRecognizer(leftSwipeGesture)
        let rightSwipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(respondRightSwipeGesture(_:)))
        rightSwipeGesture.direction = [UISwipeGestureRecognizer.Direction.right]
        pdfView.addGestureRecognizer(rightSwipeGesture)
    }

    @objc func respondLeftSwipeGesture(_ sender: UISwipeGestureRecognizer) {
        if pdfView.document == nil { return }
        pdfView.goToPreviousPage(self)
    }

    @objc func respondRightSwipeGesture(_ sender: UISwipeGestureRecognizer) {
        if pdfView.document == nil { return }
        pdfView.goToNextPage(self)
    }

    func getDocument(path: String) -> PDFDocument? {
        let pdfURL = URL(fileURLWithPath: path)
        let document = PDFDocument(url: pdfURL)
        return document
    }
}
Contexture answered 12/3, 2019 at 5:8 Comment(2)
Thanks that worked with the sample app. The display mode in your answer ie singlePageContinuous is the mode I specifically said I don't want to use, however the solution works with singlePage mode too. Actually I discovered the reason my swipe handler wasn't working in single page mode is possibly due to a pdf that PDFKit can't handle for some bizarre reason. yoshinoya.com/wp-content/themes/original/pdf/leaf.pdfIncomplete
No need to implement gestures.Th ey will work automatically if you write : pdfView.usePageViewController(true, withViewOptions: nil)Altostratus
V
0

You might simply set the displayMode to continuous and it might work:

pdfView.displayMode = .singlePageContinuous
Vitality answered 13/5, 2019 at 23:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.