Word文檔Aspose.Words使用教程:構(gòu)建適用于Android的Word轉(zhuǎn)PDF應(yīng)用程序
Word轉(zhuǎn)PDF是一種廣泛使用的文檔轉(zhuǎn)換方式,這也是MS Word提供內(nèi)置功能將Word文檔保存為PDF的原因。由于PDF是共享文檔或在線保存文檔的首選格式,因此在各種情況下都需要Word到PDF的轉(zhuǎn)換。另一方面,基于Android的智能手機(jī)通過應(yīng)用程序在手機(jī)中提供了多種功能,使人們的生活更加輕松。
在本文中,將展示如何在Android應(yīng)用程序中將Word集成為PDF轉(zhuǎn)換功能。為了演示,本文將在幾個步驟中為Android構(gòu)建一個簡單的Word轉(zhuǎn)PDF應(yīng)用程序,該應(yīng)用程序具有以下功能。
- 將Word文檔轉(zhuǎn)換為PDF
- 將PDF保存在手機(jī)的存儲空間中
- 在應(yīng)用程序中查看PDF
>>Aspose.Words for Android via Java 更新至新版本v20.6,點(diǎn)擊下方按鈕下載最新版。
點(diǎn)擊下載最新版Aspose.Words for Android via Java
在Android中創(chuàng)建Word to PDF Converter的步驟
以下是在Java中使用Aspose.Words for Android通過Java創(chuàng)建簡單的Word to PDF Converter應(yīng)用程序的步驟:
-
在Android Studio(或Eclipse)中創(chuàng)建一個新項(xiàng)目,然后選擇“空活動”模板。
-
配置您的項(xiàng)目。
-
打開build.gradle文件。
-
在build.gradle中添加以下存儲庫部分。
repositories { mavenCentral() maven { url "https://repository.aspose.com/repo/" } }
-
在build.gradle的dependencies部分中添加以下條目。
implementation 'com.google.android.material:material:1.1.0' implementation 'com.android.support:multidex:2.0.0' implementation 'com.github.barteksc:android-pdf-viewer:2.8.2' compile (group: 'com.aspose', name: 'aspose-words', version: '20.6', classifier: 'android.via.java')
-
通過在build.gradle的defaultConfig部分下添加以下條目來啟用multidex。
// enable multiDex multiDexEnabled true
-
完整的build.gradle文件將如下所示:
apply plugin: 'com.android.application' android { compileSdkVersion 30 buildToolsVersion "30.0.1" defaultConfig { applicationId "com.example.wordtopdf" minSdkVersion 16 targetSdkVersion 30 versionCode 1 versionName "1.0" // enable multiDex multiDexEnabled true testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } repositories { mavenCentral() maven { url "https://repository.aspose.com/repo/" } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'com.google.android.material:material:1.1.0' implementation 'com.android.support:multidex:2.0.0' implementation 'com.github.barteksc:android-pdf-viewer:2.8.2' compile (group: 'com.aspose', name: 'aspose-words', version: '20.6', classifier: 'android.via.java') testImplementation 'junit:junit:4.12' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
-
打開activity_main.xml文件。
-
將以下腳本粘貼為主要活動的布局。
-
打開MainActivity.java文件。
-
將以下Java代碼粘貼到MainActivity.java中。
package com.example.wordtopdf; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import com.aspose.words.Document; import com.aspose.words.License; import com.github.barteksc.pdfviewer.PDFView; import com.google.android.material.floatingactionbutton.FloatingActionButton; import android.os.Environment; import android.view.View; import android.widget.TextView; import android.widget.Toast; @TargetApi(Build.VERSION_CODES.FROYO) public class MainActivity extends AppCompatActivity { private static final int PICK_PDF_FILE = 2; private final String storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + File.separator; private final String outputPDF = storageDir + "Converted_PDF.pdf"; private TextView textView = null; private Uri document = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // apply the license if you have the Aspose.Words license... applyLicense(); // get treeview and set its text textView = (TextView) findViewById(R.id.textView); textView.setText("Select a Word DOCX file..."); // define click listener of floating button FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.fab); myFab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { // open Word file from file picker and convert to PDF openaAndConvertFile(null); } catch (Exception e) { e.printStackTrace(); } } }); } private void openaAndConvertFile(Uri pickerInitialUri) { // create a new intent to open document Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); // mime types for MS Word documents String[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"}; intent.setType("*/*"); intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); // start activiy startActivityForResult(intent, PICK_PDF_FILE); } @RequiresApi(api = Build.VERSION_CODES.KITKAT) @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if (resultCode == Activity.RESULT_OK) { if (intent != null) { document = intent.getData(); // open the selected document into an Input stream try (InputStream inputStream = getContentResolver().openInputStream(document);) { Document doc = new Document(inputStream); // save DOCX as PDF doc.save(outputPDF); // show PDF file location in toast as well as treeview (optional) Toast.makeText(MainActivity.this, "File saved in: " + outputPDF, Toast.LENGTH_LONG).show(); textView.setText("PDF saved at: " + outputPDF); // view converted PDF viewPDFFile(); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "File not found: " + e.getMessage(), Toast.LENGTH_LONG).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show(); } } } } public void viewPDFFile() { // load PDF into the PDFView PDFView pdfView = (PDFView) findViewById(R.id.pdfView); pdfView.fromFile(new File(outputPDF)).load(); } public void applyLicense() { // set license License lic= new License(); InputStream inputStream = getResources().openRawResource(R.raw.license); try { lic.setLicense(inputStream); } catch (Exception e) { e.printStackTrace(); } } }
-
生成應(yīng)用并在您的Android智能手機(jī)或虛擬設(shè)備中運(yùn)行。
-
轉(zhuǎn)到設(shè)置->應(yīng)用程序->權(quán)限->權(quán)限管理器->存儲,以允許該應(yīng)用訪問存儲。
Word to PDF Converter Android應(yīng)用程序–演示
以下是如何使用剛剛創(chuàng)建的Word to PDF Converter應(yīng)用程序?qū)ord DOCX文檔轉(zhuǎn)換為PDF的演示。(點(diǎn)擊圖片觀看)
還想要更多嗎?您可以點(diǎn)擊閱讀【2020 · Aspose最新資源整合】,查找需要的教程資源。如果您有任何疑問或需求,請隨時加入Aspose技術(shù)交流群(642018183),我們很高興為您提供查詢和咨詢。