Search code examples
iospdfxamarin.formsmobile-applicationpdf.js

Display pdf with a capability to place a picture on top of a page


So I am trying to create a feature for a mobile app. It needs to be able to display a pdf file in a view and allow a user to place a sizeable picture of his signature on desirable location.

I am working with xamarin.forms and to display a pdf file using this: https://github.com/xamarin/recipes/tree/master/Recipes/xamarin-forms/Controls/display-pdf

Now I need to know what is the best approach for the second part. A user places an image and all I need is to get coordinates of the location and its width.

So far I see two possible ways:

  1. Create two views on top of each other, the first for pdf and second for the signature. Allow manipulation of the image in the second and get data from there. Not sure how to do it or if it is a good idea.
  2. Make IOS use pdfjs in the same way as Android (having troubles with that) and on pdfjs side implement fabric.js and then get data from there.

Any suggestions or ideas for a better way to do it?

Thanks


Solution

  • You can add image and signature into webView.ScrollView , and make some adjustments on its ContentInset.

    1. Change webView.ScrollView.ContentInset first.

    2. Add additional view .

    3. Make the webView scroll to the correct location(original anchor).

    Complete code :

    class MyDelegate: UIWebViewDelegate
    {
        public override void LoadingFinished(UIWebView webView)
        {
            webView.ScrollView.ContentInset = new UIEdgeInsets(200, 0, 0, 0);
    
    
            UIView view  = new UIView(frame: new CoreGraphics.CGRect(0, -200, webView.Frame.Width, 200));
            UIImageView image = new UIImageView(frame: new CoreGraphics.CGRect(0, 0, webView.Frame.Width, 100));
            UILabel label = new UILabel(frame: new CoreGraphics.CGRect(0, 100, webView.Frame.Width, 100));
            view.Add(image);
            view.Add(label);
            webView.ScrollView.AddSubview(view);
    
            webView.ScrollView.ContentOffset = new CoreGraphics.CGPoint(0, -200);
        }
    }
    
    public class CustomWebViewRenderer : ViewRenderer<CustomWebView, UIWebView>
    {
        protected override void OnElementChanged (ElementChangedEventArgs<CustomWebView> e)
        {
            base.OnElementChanged (e);
    
            if (Control == null) {
                UIWebView web = new UIWebView();
                web.Delegate = new MyDelegate();
                SetNativeControl(web);
            }
        }
        //xxxxx
    }