在Android項目中引入MuPdf

來源:互聯網
上載者:User

標籤:複製   好的   idt   tac   admin   源碼編譯   tar   工具   地址   


由於公司手機App要加入一個附件查看功能,需要查看PDF檔案,在網上找了許多第三方工具,最後選擇了MuPDF。


更多第三方工具可以查看大神總結的:http://www.cnblogs.com/pokeGame/archive/2011/06/02/2068575.html

MuPDF介紹:

Android 裝置上輕量級、高品質的 PDF/XPS/CBZ 查看器。

MuPDF 上的轉譯器專為高品質的抗失真映像量身打造,它以像素級的精度高品質呈現文字和文字間的間距,從而獲得最進階別的顯示逼真度,在裝置螢幕上再現印刷紙張的顯示效果。

MuPDF 小巧、快速,但功能完備。它可以支援 PDF 檔案的透明化、加密、檔案中的超連結、批註、搜尋等眾多功能;使用者還可以利用它查XPS/OPENXPS 檔案。MuPDF 採用模組化編程,如果使用者非常渴望某些功能,軟體整合人員可以將這些功能納入。

準備工作:

在Androi項目中引入MuPDF,首先要有編譯好的包,可在以下地址下載源碼編譯:git://git.ghostscript.com/mupdf.git,編譯過程很複雜,我失敗了很多次,目前還不知道到底是哪裡有問題。
所以我使用的是別人編譯好的:http://pan.baidu.com/s/1CHp3M

有了編譯好的包,一切都簡單了,現在我們開始引入MuPDF。

引入步驟:

  1. 將jni,libs,obj 檔案夾複製到app檔案夾下,將res 檔案夾複製到app\src\main檔案夾下,將src\com檔案夾複製到app\src\main\java\ 檔案夾下,覆蓋原有檔案
  2. 在app中的build.gradle中添加
    //將libs中的.so檔案加入APK中    sourceSets {        main {            jniLibs.srcDirs = [‘libs‘]        }    }

    所有代碼如下:

    apply plugin: ‘com.android.application‘

    android {
    compileSdkVersion 24
    buildToolsVersion "24.0.2"
    defaultConfig {
    applicationId "com.example.admin.pdfread_test2"
    minSdkVersion 17
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
    release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
    }
    }

    //將libs中的.so檔案加入APK中
    sourceSets {
    main {
    jniLibs.srcDirs = [‘libs‘]
    }
    }
    }

    dependencies {
    compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
    androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
    exclude group: ‘com.android.support‘, module: ‘support-annotations‘
    })
    compile ‘com.android.support:appcompat-v7:24.2.1‘
    testCompile ‘junit:junit:4.12‘
    }


  3. AndroidManifest.xml配置MuPDFActivity和SD卡存取權限
    <activity android:name="com.artifex.mupdf.MuPDFActivity"><intent-filter>    <action android:name="android.intent.action.MAIN" />    <category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><!--jniLibs.srcDirs = [‘libs‘]--><!-- SD卡存取權限 --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><!-- 讀 -->
  4. 調用MuPDF顯示PDF檔案
          String Workstation_DataFileUrl = getApplicationContext().getFilesDir().getAbsolutePath() + "/sample.pdf";        Uri uri = Uri.parse(Workstation_DataFileUrl);        Log.d("MyDebug", Workstation_DataFileUrl);        Intent intent = new Intent(MainActivity.this, MuPDFActivity.class);        intent.setAction(Intent.ACTION_VIEW);        intent.setData(uri);        startActivity(intent);
  5. 配置你的樣式,在values.xml檔案中有這樣一段配置:
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">        <!-- Customize your theme here. -->        <item name="colorPrimary">@color/colorPrimary</item>        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>        <item name="colorAccent">@color/colorAccent</item>    </style>

    用來配置你的樣式,在colors.xml檔案中配置對應的顏色

  6. 編譯執行,編譯時間會報錯【錯誤: 程式包R不存在】, 引入包即可

 

在Android項目中引入MuPdf

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.