Some test text!

menu
search
Custom viewkeyboard_arrow_down

Add a UIView to a PDF page

This guide demonstrates how to make a UIView appear as if it is stuck to a page.

It is convenient for the user to make a UIView that is "stuck" to a page record its desired page location. To do this, you could add an extension/category to a UIView, or use a derived class, as shown below:

class FloatingView: UIView {
    // the page on which the UIView appears
    
    var pageNumber: Int32 = 0
    // the location, expressed in PDF coordinates, at which the view appears
    var pdfPageRect: PTPDFRect?
}

In this example, we will add a red square to a location near the bottom left hand corner of the first page:

// prepare the new view
let redRect = FloatingView()
redRect.backgroundColor = UIColor.red
redRect.pageNumber = 1
redRect.pdfPageRect = PTPDFRect(x1: 10, y1: 10, x2: 100, y2: 100)

// add the view to PTPDFViewCtrl's overlayView
self.pdfViewCtrl.overlayView.addSubview(redRect)

// position the view
positionFloatingViews()

Without the last line in the code snippet above, the new view would not appear in the correct location. The following method will position it, and any others that have been added:

func positionFloatingViews() {
    for case let overlayView as FloatingView in self.pdfViewCtrl.overlayView.subviews {
        // make sure this is a floating view
        if type(of: overlayView) === FloatingView.self {
            var pageHidden = false
            if !pdfViewCtrl.pagePresentationModeIsContinuous() {
                pageHidden = !pdfViewCtrl.pageIs(onScreen: overlayView.pageNumber)
            }
            overlayView.isHidden = pageHidden
            var screenRect: CGRect = pdfViewCtrl.pdfRectPage2CGRectScreen(overlayView.pdfPageRect, pageNumber: overlayView.pageNumber)
            screenRect.origin.x += CGFloat(pdfViewCtrl.getHScrollPos())
            screenRect.origin.y += CGFloat(pdfViewCtrl.getVScrollPos())
            overlayView.frame = screenRect
        }
    }
}
Floating UIView
A red `UIView` positioned near the bottom left hand corner of the first page.

Whenever the PDF page shifts within the PTPDFViewCtrl, which may happen when changing the page presentation mode, zooming, and such, the views need to be re-positioned (positionFloatingViews needs to run again). To do this, implement the PTPDFViewCtrl delegate methods pdfScrollViewDidEndZooming: and onLayoutChanged, and in them, call positionFloatingViews:

func pdfViewCtrl(onLayoutChanged pdfViewCtrl: PTPDFViewCtrl!) {
    positionFloatingViews()
}

func pdfViewCtrl(_ pdfViewCtrl: PTPDFViewCtrl!, pdfScrollViewDidEndZooming scrollView: UIScrollView!, with view: UIView!, atScale scale: Float) {
    positionFloatingViews()
}

Get the answers you need: Support

close

Free Trial

Get unlimited trial usage of PDFTron SDK to bring accurate, reliable, and fast document processing capabilities to any application or workflow.

Select a platform to get started with your free trial.

Unlimited usage. No email address required.

PDFTron Receives USD$71 Million Growth Investment Led By Silversmith Capital Partners

Learn more
close