然后大体思路就是使用framelayout在上面用画笔画上水印,背景置为null,所以不会遮挡下面PDF的展示,
下面使用PDFview来加载PDF,具体使用方法贴到下面
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
本文是在android4.4上运行的demo,没有加上动态申请权限,但是现在android6.0以上的设备占了大多数,所以各位客官用到的话需要在加载PDF的时候判断下权限哦
import android.content.Context;
import android.graphics.Canvas;
import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.View;
/**
* Created by Duqianlong on 2019/2/28.
*/
public class WaterMarkView extends View {
private String name;
private String waterMark;
public WaterMarkView(Context context) {
super(context);
initWaterMark();
}
public WaterMarkView(Context context, AttributeSet attrs) {
super(context, attrs);
initWaterMark();
}
public WaterMarkView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initWaterMark();
}
//生成水印文本
private void initWaterMark() {
String waterMark = "机密文件,拷贝必究" + "\r\n" + name;
}
public void setUserName(String userName,String time,String phone) {
this.name = userName+ " " + phone+ "\r\n" + time;
}
//开始绘制水印
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//获得页面尺寸
int width = getWidth();
int height = getHeight();
//TextPaint是paint的子类,该类可以很方便的进行文字的绘制
TextPaint textPaint = new TextPaint();
textPaint.setARGB(120, 223, 22, 28);//设置水印颜色
textPaint.setTextSize(30.0F);//设置水印字体大小
textPaint.setAntiAlias(true); // 抗锯齿
//参数意义分别为:文字内容、TextPaint对象、文本宽度、对齐方式、行距倍数、行距加数和是否包含内边距。
//这里比较重要的地方是设置文本宽度,当文本宽度比这个值大的时候就会自动换行。
// StaticLayout layout = new StaticLayout(waterMark, textPaint,width,
StaticLayout layout = new StaticLayout(name, textPaint, width,
Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
// float addWidth=36.0f,addHeight=24.0f;
float addWidth = width / 10, addHeight = height / 10;
//水印的位置
// float[] x = new float[]{width / 4-addWidth , width * 3 / 4-addWidth, width / 4-addWidth, width* 3 / 4-addWidth};
// float[] y = new float[]{height / 4-addHeight, height / 4-addHeight, height*3 / 4-addHeight, height * 3 / 4-addHeight}; //水印的位置
float[] x = new float[]{0, addWidth,addWidth*5, 0,addWidth*4,addWidth*8,addWidth,addWidth*5,addWidth*9,addWidth*4,addWidth*8};
float[] y = new float[]{addHeight, addHeight * 3, addHeight*3/2, addHeight*6,addHeight*9/2,addHeight*3,addHeight*17/2,addHeight*7,addHeight*11/2,addHeight*10,addHeight*17/2};
//页面上绘制水印
for (int i = 0; i < 11; i++) {
canvas.save();
canvas.translate(x[i], y[i]);
canvas.rotate(-30);
layout.draw(canvas);
canvas.restore();
}
}
}
import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
PDFView vPdfView;
Handler handler;
InputStream stream;
TextView textView;
@SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化水印
initWaterMarkView();
//加载PDF流
initPDFview();
//流下载完毕后,主线程渲染PDF
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 100) {
vPdfView.fromStream(stream)
.enableSwipe(true)
.swipeHorizontal(false)
.enableDoubletap(true)
.defaultPage(0)
.enableAnnotationRendering(false)
.password(null)
.scrollHandle(null)
.onPageChange(new OnPageChangeListener() { //获取当前页数以及总页数,不需要则不用调用
@Override
public void onPageChanged(int page, int pageCount) { //page : 当前展示页数, pagecount:总页数
textView.setText(page+"/"+pageCount);
}
})
.load();
}
}
};
}
private void initPDFview() {
//子线程下载流
textView=findViewById(R.id.my_Page);
vPdfView = findViewById(R.id.myPdfview);
new Thread(new Runnable() {
@Override
public void run() {
try {
// URL url = new URL("http://172.25.35.179:8080/mobile/publicMethod/getPdf");
URL url = new URL("https://ssl.r93535.com/JTOAInterface/mobile/publicMethod/getPdf");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
stream = urlConnection.getInputStream();
if (stream != null) {
Message message = handler.obtainMessage();
message.what = 100;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void initWaterMarkView() {
WaterMarkView markView = findViewById(R.id.water_mark_view);
markView.setUserName("张大壮","2019.3.5","15313599999");
markView = new WaterMarkView(MainActivity.this);
}
}
这算是一个伪水印吧,没有把水印加到PDF文件里面,但是为了文件安全,尽量不把文件存到本地再去加水印,以上这样或许更安全一点点