PDFKit .go(to: ) Method in SwiftUI

834 views Asked by At

hello i want to display a PDF file at page 7 in my SwiftUI app.

I would also like to use the functions .go(to :) and .currentPage() outside the UIViewRepresentable.

import SwiftUI
import PDFKit

struct PDFKitView: View {
var pdf: PDF
var body: some View {
    PDFKitRepresentedView(pdf)
}
}

struct PDFKitRepresentedView: UIViewRepresentable {
    let pdf: PDF
    init(_ pdf: PDF) {
        self.pdf = pdf
    }

func makeUIView(context: UIViewRepresentableContext<PDFKitRepresentedView>) -> PDFKitRepresentedView.UIViewType {
  
    let pdfView = PDFView()
    
    let document = PDFDocument(data: pdf.content!)
    
    pdfView.document = document
    pdfView.autoScales = true
    
    print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x600002245420> page index 0)
    print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
    
    if let myPage = document?.page(at: 7) {
        
        pdfView.go(to: myPage)
    }
    
    print("pdfView.currentPage: \(String(describing: pdfView.currentPage))") // pdfView.currentPage: Optional(<PDFPage: 0x6000022455c0> page index 7)
    print("pdfView: \(pdfView)") // pdfView: <PDFView: 0x7fb206526c20; frame = (0 0; 0 0); gestureRecognizers = <NSArray: 0x600002ef4150>; layer = <CALayer: 0x60000202ddc0>>
    
    return pdfView
}

func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PDFKitRepresentedView>) {
    // Update the view.
}
}

The PDF file is shown but at page 1 and not 7 :/

I'm still relatively new to it Swift, can anyone give me a hint?

1

There are 1 answers

1
Carl On BEST ANSWER

this is how it works now

the problem was

PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))

and not

PDFView()

here the complete code

import SwiftUI
import PDFKit

struct PDFKitView: View {
@State var pdf: PDF
@Binding var pageIndex: Int

var body: some View {
    VStack{
        PDFPreviewController(pdfX: $pdf , pageIndex: $pageIndex )
    }
}
}

class PDFPreviewViewConroller: UIViewController {

public var pdfView: PDFView!

override func loadView() {
    
    pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
    
    self.view = pdfView
}

override func viewDidLoad() {
    super.viewDidLoad()
}
}

struct PDFPreviewController: UIViewControllerRepresentable {

@Binding var pdf: PDF
@Binding var pageIndex: Int

init(pdfX: Binding<PDF>, pageIndex: Binding<Int>) {
    _pdf = pdfX
    _pageIndex = pageIndex
}

func makeUIViewController(context: UIViewControllerRepresentableContext<PDFPreviewController>) -> PDFPreviewViewConroller {
    
    return PDFPreviewViewConroller()
}

func updateUIViewController(_ uiViewController: PDFPreviewViewConroller, context: UIViewControllerRepresentableContext<PDFPreviewController>) {
    
    uiViewController.pdfView.document = PDFDocument(data: pdf.content!)
    
    if let myPage = uiViewController.pdfView.document?.page(at: (pageIndex)) {
        uiViewController.pdfView.go(to: myPage)
    }  
}

func makeCoordinator() -> Coordinator {
    Coordinator(pdf: $pdf, pageIndex: $pageIndex)
}

class Coordinator: NSObject {
    
    @Binding var pageIndex: Int
    @Binding var pdf: PDF
    
    init(pdf: Binding<PDF>, pageIndex: Binding<Int>) {
        _pageIndex = pageIndex
        _pdf = pdf
    }
}
}