2017年2月28日 星期二

在 Xamarin.Android 當中讀取 PDF 資料…I

 

document-428331_1920

 

平常我們在文件上很常使用 PDF 作為文件的儲存格式,但遇到 Mobile App 要讀取 PDF 的時候,就會常常不知該如何完成,那就讓本篇文章來指引如何在 Xamarin.Android 中完成 PDF 文件的讀取吧!

 

本篇將介紹如何透過手機的 Default App 讀取 PDF,開始囉~~~

 

首先,新建立一個 Xamarin.Android 的專案。

1

 

再來準備一下 PDF 文件,放在此 XamarinDroidForReadPdfFile 專案的 Asserts 中。如下圖:

2

 

繼續準備此測試程式的 Layout 外觀,在所建立的 XamarinDroidForReadPdfFile 專案中,找到 Resources –> layout 當中打開 Main.axml,將設計畫面處理成如下圖所示:

3

(實線框當中的元件為Button,由上到下的 Id 依序為 CreatePDFFileFromLocalButton、 DeletePDFFileButton、ReadPDFUsingDefaultAppButton。)

 

開啟專案底下的 MainActivity.cs 來撰寫對應的程式碼。

4

 

設定畫面按鈕與按鈕點選的事件註冊,如下圖程式碼:

5

 

在此測試的 App 其中的第一個按鈕功能是想要做到從建立PDF檔案到Android 的實體檔案路徑上。下圖展示額外寫一個方法來完成從將檔案複製到Android 實體路徑的功能:

6

 

接著回到 CreatePDFFileFromLocalButton 按鈕 Click 事件的程式碼,呼叫此方法。

7

 

繼續完成第二個 DeletePDFFileButton 按鈕的 Click 功能,將已經存在Android 手機實體路徑的 PDF 檔案刪除,如下圖程式碼:

8

 

最後第三個按鈕 ReadPDFUsingDefaultAppButton 的 Click 功能,則是切換到另外一個叫作 ReadPdfUsingDefaultAppActivity 的 Activity,如下圖:

9

 

而在 ReadPdfUsingDefaultAppActivity 這個 Activity 的 OnCreate 方法當中設計了呼叫 ReadPdfByDefaultApp 的方法,如下圖:

10

其中虛線框的 OpenFile() 方法則是參考
https://forums.xamarin.com/discussion/39912/open-pdf-in-android-default-pdf-reader
當中 AlessandroCaliaro 所回應的程式碼完成的。

 

最後記得到此專案的 "屬性" 中,找到 "Android 資訊清單",設定所需要的兩個使用權限 "Read_External_Storage"、"Write_External_Storage"。

 

測試結果

開始測試之前,請先從 Visual Studio 的 "工具" 選單當中,找到 "Android -> Android 裝置監視器"。

11

在 "Android 裝置監視器" 當中,看到 "File Explorer" 分頁。

12

 

接著回到在 Visual Studio 當中執行測試該專案後,回到 "Android 裝置監視器" 的 "File Explorer" 當中的 "data –> data" 底下,找到該測試專案 XamarinDroidForReadPdfFile.XamarinDroidForReadPdfFile 使用檔案的情形。

13

 

在執行起來的 XamarinDroidForReadPdfFile 當中,點選第一顆按鈕:

14

 

就會在 "File Explorer" 當中看到出現了 "MyPDFDemoFile.pdf "。

15

 

接著再按下第三顆按鈕準備透過手機預設的 PDF App 讀取 PDF。

16

 

若手機上沒有安裝讀取 PDF 的 App 則會顯示如下畫面:

18

 

當然,若是有安裝好讀取 PDF 的 App 的話,就可以正確開啟所指定的 PDF 檔案。

19

 

以上就是在 Xamarin.Android 專案上透過預設 PDF App 讀取 PDF 檔案,有興趣的人趕快試用看看吧~~


 


本篇文章同步發表於 昕力大學 ,網址是:

https://tpu.thinkpower.com.tw/tpu/File/html/201702/20170228035753_f.html

歡迎前往 昕力大學 觀看更多有關 Xamarin 的文章。

 

參考資料:

Open PDF in Android default PDF reader:
https://forums.xamarin.com/discussion/39912/open-pdf-in-android-default-pdf-reader

3 則留言:

  1. 你好,有個 Xamarin 開發 App 的需求,是否方便與你聯繫? Paza / hipaza@gmail.com

    回覆刪除
  2. Hi Author just now i found your blog its really awesome. Keep this work. It will more helpful for web and mobile app developers. Hire affordable Xamarin Developer

    回覆刪除
  3. Do you understand there's a 12 word phrase you can tell your partner... that will induce intense feelings of love and instinctual appeal for you deep inside his chest?

    Because deep inside these 12 words is a "secret signal" that triggers a man's instinct to love, idolize and protect you with all his heart...

    12 Words Who Fuel A Man's Love Instinct

    This instinct is so hardwired into a man's genetics that it will drive him to work better than before to do his best at looking after your relationship.

    Matter-of-fact, fueling this influential instinct is so mandatory to having the best possible relationship with your man that once you send your man one of the "Secret Signals"...

    ...You will soon find him expose his heart and soul to you in such a way he's never expressed before and he will identify you as the only woman in the world who has ever truly fascinated him.

    回覆刪除