I create a UIViewRepresentable to show my PDF inside my SwiftUI app. I want to access the current page number using currentPage?.pageRef?.pageNumber
. from the view in which I display the pdf. How to make PDFView
Created in func makeUIView(context: UIViewRepresentableContext<PDFDisplayRepresentedView>) -> PDFView
Available in the view displaying the pdf so I can access its properties to obtain page number.
import SwiftUI
import PDFKit
struct PDFDisplayView: View {
@Binding var pdfDocument : PDFDocument?
var body: some View {
PDFDisplayRepresentedView(pdfDocument: pdfDocument!)
}
}
struct PDFDisplayRepresentedView: UIViewRepresentable {
let pdfDocument: PDFDocument
init(pdfDocument: PDFDocument) {
self.pdfDocument = pdfDocument
}
func makeUIView(context: UIViewRepresentableContext<PDFDisplayRepresentedView>) -> PDFView {
return createPDFViewUsing(document: pdfDocument)
}
func updateUIView(_ pdfView: PDFView, context: UIViewRepresentableContext<PDFDisplayRepresentedView>) {
pdfView.document = pdfDocument
}
private func createPDFViewUsing(document : PDFDocument) -> PDFView {
let pdfView = PDFView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
pdfView.document = document
... set up pdfView attributes
return pdfView
}
}
view showing pdf where I want to know the page number
pseudo code
…
@State private var pdfDisplayView : PDFDisplayView?
….
var body: some View {
load the pdf into this view.
need to access the page number
pdfDisplayView
}