Adding a custom view to a PDF page

In this document
chevron_rightIn Xamarin.Android
chevron_rightIn Xamarin.iOS

In Xamarin.Android

Adding custom view to a PDF page in Xamarin.Android is identical to adding custom views in Android. See tutorial here.

For example, the equivalent of the example provided in the Android guide will be:

<pdftron.PDF.PDFViewCtrl
    android:id="@+id/pdfviewctrl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical|horizontal" >
  <pdftron.PDF.Tools.CustomRelativeLayout
    android:layout_width="50dp"
    android:layout_height="50dp"
    app:posX="50"
    app:posY="150"
    app:pageNum="3"
    app:zoomWithParent="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Custom Layout Text View"
        android:textSize="24dp"
        android:elevation="2dp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/light_gray" />
  </pdftron.PDF.Tools.CustomRelativeLayout>
</pdftron.PDF.PDFViewCtrl>

In Xamarin.iOS

Adding custom view to a PDF page in Xamarin.iOS is identical to adding custom views in iOS. See tutorial here.

For example, the equivalent of the example provided in the ios guide will be:

First, create a FloatingView class:

public class FloatingView : UIView
{
    public FloatingView() : base()
    {

    }

    public int PageNumber { get; set; }

    public pdftron.PDF.Rect PdfPageRect { get; set; }
}

Then, add it to the PDFViewCtrl:

var mCustomView = new FloatingView();
mCustomView.BackgroundColor = UIColor.Red;
mCustomView.PageNumber = 1;
mCustomView.PdfPageRect = new Rect(10, 10, 100, 100);
mPdfViewCtrl.OverlayView.AddSubview(mCustomView);

positionFloatingViews();

Where positionFloatingViews is:

void positionFloatingViews()
{
    foreach (UIView view in mPdfViewCtrl.OverlayView.Subviews)
    {
        if (view is FloatingView)
        {
            var overlayView = view as FloatingView;
            bool pageHidden = false;
            if (!mPdfViewCtrl.PagePresentationModeIsContinuous)
            {
                pageHidden = !mPdfViewCtrl.PageIsOnScreen(overlayView.PageNumber);
            }
            overlayView.Hidden = pageHidden;
            CGRect screenRect = mPdfViewCtrl.PDFRectPage2CGRectScreen(TypeConvertHelper.ConvRectToNative(overlayView.PdfPageRect), overlayView.PageNumber);
            screenRect.X += (nfloat)mPdfViewCtrl.GetHScrollPos();
            screenRect.Y += (nfloat)mPdfViewCtrl.GetVScrollPos();
            overlayView.Frame = screenRect;
        }
    }
}

Lastly, adjust position in callbacks:

mPdfViewCtrl.PdfViewCtrlOnLayoutChanged += (sender, e) =>
{
    positionFloatingViews();
};
mPdfViewCtrl.PdfScrollViewDidEndZooming += (sender, e) =>
{
    positionFloatingViews();
};