-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainActivity.cs
89 lines (74 loc) · 2.88 KB
/
MainActivity.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.IO;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Views;
namespace AKPdfViewer
{
[Activity(Label = "AKPdfViewer", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
AKPdfViewer _pdfViewer;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
ViewGroup mainView = (ViewGroup)FindViewById(Android.Resource.Id.Content);
// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Click += delegate {
var filePath = CopyPdfDocument();
_pdfViewer = new AKPdfViewer(Application.Context, filePath);
_pdfViewer.MarginTop = _pdfViewer.MarginBottom = 0;
_pdfViewer.MarginLeft = _pdfViewer.MarginRight = 0;
_pdfViewer.SetLayout();
_pdfViewer.SetModalMode(true, "Close");
mainView.AddView(_pdfViewer);
_pdfViewer.OnCustomerPdfViewerError += PopulateErrorToWebView;
_pdfViewer.DidDismiss += DidDismiss;
};
void DidDismiss(object sender, EventArgs e)
{
if (_pdfViewer != null)
{
if (_pdfViewer.IsModal)
{
_pdfViewer.DidDismiss -= DidDismiss;
}
_pdfViewer.OnCustomerPdfViewerError -= PopulateErrorToWebView;
mainView.RemoveView(_pdfViewer);
_pdfViewer = null;
}
}
void PopulateErrorToWebView(object sender, string errorMsg)
{
Console.WriteLine(errorMsg);
}
string CopyPdfDocument()
{
string pdfName = "test.pdf";
string pdfPath = Path.Combine("data/data/com.companyname.akpdfviewer", pdfName);
// Check if your DB has already been extracted.
if (!File.Exists(pdfPath))
{
using (BinaryReader br = new BinaryReader(Android.App.Application.Context.Assets.Open(pdfName)))
{
using (BinaryWriter bw = new BinaryWriter(new FileStream(pdfPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int len = 0;
while ((len = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, len);
}
}
}
}
return pdfPath;
}
}
}
}