Word文档Aspose.Words使用教程:构建适用于Android的Word转PDF应用程序
Word转PDF是一种广泛使用的文档转换方式,这也是MS Word提供内置功能将Word文档保存为PDF的原因。由于PDF是共享文档或在线保存文档的首选格式,因此在各种情况下都需要Word到PDF的转换。另一方面,基于Android的智能手机通过应用程序在手机中提供了多种功能,使人们的生活更加轻松。
在本文中,将展示如何在Android应用程序中将Word集成为PDF转换功能。为了演示,本文将在几个步骤中为Android构建一个简单的Word转PDF应用程序,该应用程序具有以下功能。
- 将Word文档转换为PDF
- 将PDF保存在手机的存储空间中
- 在应用程序中查看PDF
点击下载最新版Aspose.Words for Android via Java(技术交流Q群761297826)https://www.evget.com/product/3250/download
在Android中创建Word to PDF Converter的步骤
以下是在Java中使用Aspose.Words for Android通过Java创建简单的Word to PDF Converter应用程序的步骤:
- 在Android Studio(或Eclipse)中创建一个新项目,然后选择“空活动”模板。
- 配置您的项目。
- 打开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 30buildToolsVersion "30.0.1"defaultConfig {applicationId "com.example.wordtopdf"minSdkVersion 16targetSdkVersion 30versionCode 1versionName "1.0"// enable multiDexmultiDexEnabled truetestInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles 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;@Overrideprotected 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 texttextView = (TextView) findViewById(R.id.textView);textView.setText("Select a Word DOCX file...");// define click listener of floating buttonFloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.fab);myFab.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {try {// open Word file from file picker and convert to PDFopenaAndConvertFile(null);} catch (Exception e) {e.printStackTrace();}}});}private void openaAndConvertFile(Uri pickerInitialUri) {// create a new intent to open documentIntent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);intent.addCategory(Intent.CATEGORY_OPENABLE);// mime types for MS Word documentsString[] mimetypes = {"application/vnd.openxmlformats-officedocument.wordprocessingml.document", "application/msword"};intent.setType("*/*");intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);// start activiystartActivityForResult(intent, PICK_PDF_FILE);}@RequiresApi(api = Build.VERSION_CODES.KITKAT)@Overridepublic 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 streamtry (InputStream inputStream =getContentResolver().openInputStream(document);) {Document doc = new Document(inputStream);// save DOCX as PDFdoc.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 PDFviewPDFFile();} 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 PDFViewPDFView pdfView = (PDFView) findViewById(R.id.pdfView);pdfView.fromFile(new File(outputPDF)).load();}public void applyLicense(){// set licenseLicense lic= new License();InputStream inputStream = getResources().openRawResource(R.raw.license);try {lic.setLicense(inputStream);} catch (Exception e) {e.printStackTrace();}} }
- 生成应用并在您的Android智能手机或虚拟设备中运行。
- 转到设置->应用程序->权限->权限管理器->存储,以允许该应用访问存储。
Word to PDF Converter Android应用程序–演示
以下是如何使用刚刚创建的Word to PDF Converter应用程序将Word DOCX文档转换为PDF的演示。(点击图片观看)
Word文档Aspose.Words使用教程:构建适用于Android的Word转PDF应用程序相关推荐
- 计算机word文档无法工作,win10所有word文档都打不开如何解决_win10电脑所有word文档无法打开解决教程...
word是我们经常会用到的文档编辑工具,用户们都会在电脑中安装使用,不过使用的时候难免遇到一些问题,比如有win10 ghost 纯净版系统用户要打开word文档的时候发现电脑中所有word文档都打不 ...
- python获取word页数_使用Python的word文档的页数(Number of pages of a word document with Python)...
使用Python的word文档的页数(Number of pages of a word document with Python) 有没有办法用Python有效地获得word文档(.doc,.doc ...
- word文档服务器多人打开后就损坏,打开word文档损坏
我正在尝试将Word文档(在ASP .NET服务器和Microsoft.Office.Interop.Word中生成)发送到客户端,但每次我尝试在客户端使用IE9打开时,它都说它可以'打开它,因为它已 ...
- (五)、JAVA基于OPENXML的word文档插入、合并、替换操作系列之word文件合并[支持多文件]
(五).JAVA基于OPENXML的word文档插入.合并.替换操作系列之word文件合并[支持多文件] 二.word合并的多种方案简单比较 三.基于Open Xml WordprocessingML ...
- word文档分节符如何删除_如何在Word文档中查找分节符
word文档分节符如何删除 Section breaks in Word allow you to break up your document into sections and format ea ...
- 复制一个Word文档的部分或全部内容到另一个Word文档
我最近喜欢折腾Office软件相关的东西,想把很多Office软件提供的功能用.NET来实现,如果后期能把它用来开发一点我自己的小应用程序那就更好了. 扯远了,回到正题.复制文档内容这个功能太常见啦, ...
- java根据模板生成word文档_Python办公自动化:使用python来自动生成word文档
让python做办公自动化,让你闲下来 让python做自动化,让你闲下来 上节对python的excel Python办公自动化系列:自动操作Excel自动化做了介绍.这次介绍如何用python对w ...
- word文档中表格计算机功能在哪,电脑在Word文档中插入Excel图表不显示如何解决...
电脑中都会有安装一个Word文档,我们可以编辑和整理文件,但是一位用户说做Word文档里,想插入EXCEL图表,但是显示出来的是一段代码,尝试很多次还是一样,怎么办呢?如果你还在为此问题困扰,那么可以 ...
- python分解word文档为多个_用python批量处理word文档
应我家领导要求,开发一个word文档批处理脚本,涉及word文档.excel表格.文件存取.排序与索引.简单GUI等内容,前期针对各分项功能实现写了几篇小文章,现在将总体思路记录一下,作为这个系列的完 ...
最新文章
- js 判断数据类型的几种方法
- ICCV2013-Hybrid Deep Learning for Face Verification
- 定义分销渠道(distribution channel)
- react 动态路 嵌套动子路由_2020年,我是如何从一名Vueer转岗到React阵营
- 【UVA - 10038】Jolly Jumpers (模拟,水题,标记)
- thinkphp5连接数据库mysql_ThinkPHP学习(三)配置PHP5支持MySQL,连接MySQL数据库
- 蓝桥杯 ADV-223 算法提高 8-1因式分解
- NLP最新趋势,7个主流业务场景!
- day08—css布局解决方案之多列布局
- Java Matcher源码学习记录
- [转]Linux统计代码行数
- linux 安装tomcat
- 【AlphaGo论文学习】Mastering the game of Go without human knowledge翻译及心得
- SpringCloudConfig分布式配置中心介绍与搭建使用以及ConfigClient实现动态刷新配置
- 正式版上线、登录币安NFT市场,PlatoFarm近况
- atof(),atoi(),itoa(),sprintf()等用法总结
- 攻防世界Web题 - unseping 总结
- 画环形或者蚊香线圈的软件介绍以及使用笔记
- swagger 2.9.2
- 0基础软件测试小白,如何找到一份高薪的工作?