Skip to content

Instantly share code, notes, and snippets.

@nicwise
Created April 23, 2013 12:32
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save nicwise/5443188 to your computer and use it in GitHub Desktop.
Save nicwise/5443188 to your computer and use it in GitHub Desktop.
PDF download and viewing - Xamarin.iOS
using System;
using System.IO;
using System.Threading.Tasks;
using MonoTouch.UIKit;
using MonoTouch.QuickLook;
using System.Net;
using MonoTouch.Foundation;
using System.Security.Cryptography;
using System.Text;
namespace BigTed.Utils
{
public class PDFItem : QLPreviewItem
{
string title;
string uri;
public PDFItem(string title, string uri)
{
this.title = title;
this.uri = uri;
}
public override string ItemTitle {
get { return title; }
}
public override NSUrl ItemUrl {
get { return NSUrl.FromFilename(uri); }
}
}
public class PDFPreviewControllerDataSource : QLPreviewControllerDataSource
{
string url = "";
string filename = "";
public PDFPreviewControllerDataSource (string url, string filename)
{
this.url = url;
this.filename = filename;
}
public override QLPreviewItem GetPreviewItem (QLPreviewController controller, int index)
{
return new PDFItem (filename, url);
}
public override int PreviewItemCount (QLPreviewController controller)
{
return 1;
}
}
public class BTPdfUtils
{
public static string UrlHash(string source)
{
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
// Convert the input string to a byte array and compute the hash.
byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(source));
// Create a new Stringbuilder to collect the bytes
// and create a string.
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for (int i = 0; i < data.Length; i++)
{
sBuilder.Append(data[i].ToString("x2"));
}
// Return the hexadecimal string.
return sBuilder.ToString();
}
public static void DownloadAndDisplayPdf (string url, string filename, UINavigationController parentController)
{
string path = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "attachments");
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
string hash = UrlHash (url);
path = Path.Combine (path, hash);
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
string newFilename = Path.Combine (path, filename);
if (!newFilename.StartsWith (path))
{
newFilename = Path.Combine (path, hash + ".pdf");
}
Task.Factory.StartNew (() => {
try
{
if (File.Exists (newFilename))
{
var fi = new FileInfo(newFilename);
if (fi.Length > 1024 * 2)
{
return true;
}
File.Delete (newFilename);
}
if (Reachability.InternetConnectionStatus() == NetworkStatus.NotReachable)
{
return false;
}
var wc = new WebClient();
wc.DownloadFile(new Uri(url), newFilename);
return true;
} catch (Exception ex){
return false;
}
}).ContinueWith (tr => {
BTUtils.HideStatus();
if (tr.Result)
{
QLPreviewController previewController = new QLPreviewController ();
previewController.DataSource = new PDFPreviewControllerDataSource (newFilename, filename);
parentController.PushViewController(previewController, true);
} else {
//show a fail to the user
}
}, new UIKitScheduler ());
}
}
}
@kerfallak
Copy link

is QuickLook available in visual studio for windows? I have been trying to find it on nuget but not successful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment