实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.example.jammy.pdf_demo;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.artifex.mupdf.MuPDFCore;
import com.artifex.mupdf.MuPDFPageAdapter;
import com.artifex.mupdf.ReaderView;
import com.artifex.mupdf.SavePdf;
import java.io.File;
import butterknife.Bind;
import butterknife.ButterKnife;
/**
* Created by Jammy on 2016/6/23.
*/
public class PDFActivity extends Activity {
@Bind(R.id.readerView)
ReaderView readerView;
@Bind(R.id.rl_back)
RelativeLayout rlBack;
@Bind(R.id.rl_sign)
RelativeLayout rlSign;
@Bind(R.id.rl_update)
RelativeLayout rlUpdate;
@Bind(R.id.rl_clear)
RelativeLayout rlClear;
@Bind(R.id.rl_save)
RelativeLayout rlSave;
boolean isUpdate = false;
String in_path;
String out_path;
String update_path;
PopupWindow popupWindow;
SignatureView signatureView;
boolean iBack = false;
float density; int first = 1;
int back_first = 0;
String file_id;
ProgressDialog progressDialog;
MuPDFCore muPDFCore;
Save_Pdf save_pdf;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf);
ButterKnife.bind(this);
View screenView = this.getWindow().getDecorView();
screenView.setDrawingCacheEnabled(true);
screenView.buildDrawingCache();
DisplayMetrics metric = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metric);
density = metric.density;
in_path = Environment.getExternalStorageDirectory().getPath() "/123.pdf";
out_path = in_path.substring(0, in_path.length() - 4) "1.pdf";
try {
muPDFCore = new MuPDFCore(in_path); readerView.setAdapter(new MuPDFPageAdapter(this, muPDFCore));
View view = LayoutInflater.from(this).inflate(R.layout.signature_layout, null);
signatureView = (SignatureView) view.findViewById(R.id.qianming);
readerView.setDisplayedViewIndex(0);
popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false);
rlSign.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
if (rlSave.getVisibility() == View.GONE) {
popupWindow.showAsDropDown(rlSign, 0, 0);
rlSave.setVisibility(View.VISIBLE);
rlClear.setVisibility(View.VISIBLE);
rlUpdate.setVisibility(View.VISIBLE);
} else {
popupWindow.dismiss();
signatureView.clear();
rlSave.setVisibility(View.GONE);
rlClear.setVisibility(View.GONE);
rlUpdate.setVisibility(View.GONE);
}
}
});
rlSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
float scale = readerView.getmScale();
SavePdf savePdf = new SavePdf(in_path, out_path);
savePdf.setScale(scale);
savePdf.setPageNum(readerView.getDisplayedViewIndex() 1);
savePdf.setWidthScale(1.0f * readerView.scrollX / readerView.getDisplayedView().getWidth()); savePdf.setHeightScale(1.0f * readerView.scrollY / readerView.getDisplayedView().getHeight());
savePdf.setDensity(density);
Bitmap bitmap = Bitmap.createBitmap(signatureView.getWidth(), signatureView.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
signatureView.draw(canvas);
savePdf.setBitmap(bitmap);
save_pdf = new Save_Pdf(savePdf);
save_pdf.execute();
popupWindow.dismiss();
iBack = true;
rlSave.setVisibility(View.GONE);
rlClear.setVisibility(View.GONE);
rlUpdate.setVisibility(View.GONE);
rlBack.setVisibility(View.VISIBLE);
progressDialog = ProgressDialog.show(PDFActivity.this, null, ".");
signatureView.clear();
}
});
rlBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
if (iBack) {
if (back_first == 1) {
muPDFCore = new MuPDFCore(getIntent().getExtras().getString("inPath"));
first = 1;
in_path = getIntent().getExtras().getString("inPath");
out_path = in_path.substring(0, in_path.length() - 4) "1.pdf";
} else {
muPDFCore = new MuPDFCore(out_path);
String temp = in_path;
in_path = out_path;
out_path = temp;
}
readerView.setmScale(1.0f);
readerView.setDisplayedViewIndex(readerView.getDisplayedViewIndex());
iBack = false;
if (rlSave.getVisibility() == View.VISIBLE) {
rlSave.setVisibility(View.GONE);
rlClear.setVisibility(View.GONE);
rlUpdate.setVisibility(View.GONE);
signatureView.clear();
}
rlBack.setVisibility(View.GONE);
back_first--;
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(PDFActivity.this);
builder.setTitle("鏃犳硶杩斿洖");
builder.setPositiveButton(" ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
rlClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signatureView.clear();
}
});
rlUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PDFActivity.this," ",Toast.LENGTH_SHORT).show();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
class Save_Pdf extends AsyncTask {
SavePdf savePdf;
public Save_Pdf(SavePdf savePdf) {
this.savePdf = savePdf;
}
@Override
protected Object doInBackground(Object[] params) {
savePdf.addText();
if (first == 1) {
update_path = in_path.substring(0, in_path.length() - 4) ".pdf";
in_path = in_path.substring(0, in_path.length() - 4) "2.pdf";
first ;
}
return null;
}
@Override
protected void onPostExecute(Object o) {
Toast.makeText(PDFActivity.this, "", Toast.LENGTH_SHORT).show();
try {
muPDFCore = new MuPDFCore(out_path);
readerView.setAdapter(new MuPDFPageAdapter(PDFActivity.this, muPDFCore));
String temp = in_path;
in_path = out_path;
out_path = temp;
readerView.setmScale(1.0f);
readerView.setDisplayedViewIndex(readerView.getDisplayedViewIndex());
progressDialog.dismiss();
back_first ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(" ");
builder.setPositiveButton( “", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
PDFActivity.this.finish();
}
}).setNegativeButton("", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).show();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (first != 1) {
File file = new File(in_path);
File file1 = new File(out_path);
File file2 = new File(update_path);
if (file.exists()) file.delete();
if (file1.exists()) file1.delete();
if (file2.exists() && isUpdate) file2.delete();
}
save_pdf.cancel(true);
}
}
标签: pdf签名
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论