1

I am trying to open a pdf file on a Xamarin project using webview and i was able to do it using the following :

protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); RequestWindowFeature(WindowFeatures.NoTitle); SetContentView(Resource.Layout.ViewReport); var mywebview = FindViewById<WebView>(Resource.Id.webView1); var myurl= "http://docs.google.com/gview?embedded=true&url=http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf"; mywebview.Settings.AllowFileAccess = true; mywebview.Settings.JavaScriptEnabled = true; mywebview.Settings.BuiltInZoomControls = true; mywebview.LoadUrl(myurl); mywebview.SetWebViewClient(new PodWebViewClient()); } 

My problem is that the PDF file that i am trying open is created via Itextsharp from my MVC project:

public ActionResult Print() { var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 25, 25); var output = new MemoryStream(); var writer = PdfWriter.GetInstance(document, output); document.Open(); string contents = System.IO.File.ReadAllText(Server.MapPath("~/Content/HTMTemplate/pdf_template.htm"), Encoding.Unicode); //==================== // do something here... //==================== FontFactory.Register(Server.MapPath("~/fonts/ARIALUNI.TTF")); StyleSheet ST = new StyleSheet(); ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.FACE, "Arial Unicode MS"); ST.LoadTagStyle(HtmlTags.BODY, HtmlTags.ENCODING, BaseFont.IDENTITY_H); var parsedHtmlElements = HTMLWorker.ParseToList(new StringReader(contents), ST); foreach (var htmlElement in parsedHtmlElements) document.Add(htmlElement as IElement); document.Close(); var cd = new System.Net.Mime.ContentDisposition { FileName = "samplepdf.pdf", Inline = true }; Response.AppendHeader("Content-Disposition", cd.ToString());//open in browser return File(output.ToArray(), "application/pdf"); } 

but when i did the following:

 var myurl= "http://docs.google.com/gview?embedded=true&url=http://www.sample.com/doc/print"; 

i was shown a text file with html tags and not my pdf file.

How should i fix this? I think my problem lies in my pdf file but I don't know which is it as I was able to open the pdf in my browser.. Hope you can help me. Thanks.

1 Answer 1

3

Use pdf.js and call it like this

Control.LoadUrl(string.Format("file:///android_asset/pdfjs/web/viewer.html?file={0}", WebUtility.UrlEncode(pdfWebView.Uri))); 

Here you find much more details about this approach (used for Xamarin.Forms).

Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately, pdfjs has big problems. The biggest problem for me was that pdfjs does not support touch gestures !!!!! i.e. you cannot navigate and zoom the document using the touch gestures ! For me , it is not usable and not acceptable by any user.
I found the solution for the not-responding-to-gestures. stackoverflow.com/a/56943170/592651

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.