Adding a view to a page

You can add a custom View or UIView object to a page.

Add a custom view to a page in Xamarin.iOS

This tutorial only applies to Xamarin.iOS. See Xamarin.Android equivalent here .

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:

1public class FloatingView : UIView
2{
3 public FloatingView() : base()
4 {
5
6 }
7
8 public int PageNumber { get; set; }
9
10 public pdftron.PDF.Rect PdfPageRect { get; set; }
11}

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

1var mCustomView = new FloatingView();
2mCustomView.BackgroundColor = UIColor.Red;
3mCustomView.PageNumber = 1;
4mCustomView.PdfPageRect = new Rect(10, 10, 100, 100);
5mPdfViewCtrl.OverlayView.AddSubview(mCustomView);
6
7// position the view
8positionFloatingViews();

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:

1void positionFloatingViews()
2{
3 foreach (UIView view in mPdfViewCtrl.OverlayView.Subviews)
4 {
5 if (view is FloatingView)
6 {
7 var overlayView = view as FloatingView;
8 bool pageHidden = false;
9 if (!mPdfViewCtrl.PagePresentationModeIsContinuous)
10 {
11 pageHidden = !mPdfViewCtrl.PageIsOnScreen(overlayView.PageNumber);
12 }
13 overlayView.Hidden = pageHidden;
14 CGRect screenRect = mPdfViewCtrl.PDFRectPage2CGRectScreen(TypeConvertHelper.ConvRectToNative(overlayView.PdfPageRect), overlayView.PageNumber);
15 screenRect.X += (nfloat)mPdfViewCtrl.GetHScrollPos();
16 screenRect.Y += (nfloat)mPdfViewCtrl.GetVScrollPos();
17 overlayView.Frame = screenRect;
18 }
19 }
20}
Apryse Docs Image

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:

1mPdfViewCtrl.PdfViewCtrlOnLayoutChanged += (sender, e) =>
2{
3 positionFloatingViews();
4};
5mPdfViewCtrl.PdfScrollViewDidEndZooming += (sender, e) =>
6{
7 positionFloatingViews();
8};

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales