Commit c30c7593 authored by wanglei's avatar wanglei

...

parent ec932d27
...@@ -15,6 +15,16 @@ android { ...@@ -15,6 +15,16 @@ android {
versionName = "1.0" versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
// ndk {
// abiFilters.add("arm64-v8a")
// // 取消注释以下行以添加更多 ABI 支持
// abiFilters.add("armeabi-v7a")
// }
// sourceSets {
// getByName("main") {
// jniLibs.srcDirs("src/main/jniLibs")
// }
// }
} }
buildTypes { buildTypes {
...@@ -34,13 +44,8 @@ android { ...@@ -34,13 +44,8 @@ android {
viewBinding = true viewBinding = true
buildConfig = true buildConfig = true
} }
sourceSets {
getByName("main") {
assets {
srcDirs("src\\main\\assets", "src\\main\\assets")
}
}
}
} }
dependencies { dependencies {
......
...@@ -2,21 +2,21 @@ package com.base.pdfviewerscannerwhite.ui.document.pdf ...@@ -2,21 +2,21 @@ package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.content.Intent import android.content.Intent
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.artifex.mupdfdemo.MuPDFCore
import com.base.pdfviewerscannerwhite.databinding.ActivityPdfLoadingBinding import com.base.pdfviewerscannerwhite.databinding.ActivityPdfLoadingBinding
import com.base.pdfviewerscannerwhite.helper.BaseActivity import com.base.pdfviewerscannerwhite.helper.BaseActivity
import com.base.pdfviewerscannerwhite.utils.LogEx
import com.base.pdfviewerscannerwhite.utils.ToastUtils.toast
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.File import kotlin.random.Random
class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() { class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() {
private val TAG = "PdfLoadingActivity" private val TAG = "PdfLoadingActivity"
private var doWhat = ""
private var srcPath: String = "" private var srcPath: String = ""
private var newName: String = "" private var newPath: String = ""
private var splitIndex: List<Int> = listOf() private var splitIndex: List<Int> = listOf()
private lateinit var pdfPresenter: PdfPresenter private lateinit var pdfPresenter: PdfPresenter
...@@ -24,16 +24,36 @@ class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() { ...@@ -24,16 +24,36 @@ class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() {
ActivityPdfLoadingBinding.inflate(layoutInflater) ActivityPdfLoadingBinding.inflate(layoutInflater)
} }
var isFinishBoolean: Boolean = false private var isFinishBoolean: Boolean = false
var newPdfFile: File? = null private var progressFinishAction: (() -> Unit)? = null
override fun initView() { override fun initView() {
pdfPresenter = PdfPresenter(this) pdfPresenter = PdfPresenter(this)
initSpPa() initSpPa()
pdfPresenter.splitPdf(File(srcPath), newName, splitIndex, finishAction = { when (doWhat) {
newPdfFile = it DO_SPLIT_PDF -> {
isFinishBoolean = true progressFinishAction = {
}) finish()
}
pdfPresenter.splitPdf(srcPath, newPath, splitIndex, finishAction = {
isFinishBoolean = true
})
}
DO_SAVE_PDF -> {
muPDFCore?.save()
muPDFCore = null
progressFinishAction = {
finish()
}
lifecycleScope.launch {
delay(Random.nextLong(1500, 2500))
isFinishBoolean = true
}
}
}
startProgress() startProgress()
} }
...@@ -49,18 +69,22 @@ class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() { ...@@ -49,18 +69,22 @@ class PdfLoadingActivity : BaseActivity<ActivityPdfLoadingBinding>() {
} }
delay(500) delay(500)
} }
if (newPdfFile == null) { progressFinishAction?.invoke()
toast("split pdf failed!")
finish()
return@launch
}
} }
private fun initSpPa() { private fun initSpPa() {
doWhat = intent?.extras?.getString("doWhat", "") ?: ""
srcPath = intent?.extras?.getString("srcPath", "") ?: "" srcPath = intent?.extras?.getString("srcPath", "") ?: ""
newName = intent?.extras?.getString("newName", "") ?: "" newPath = intent?.extras?.getString("newPath", "") ?: ""
splitIndex = intent.extras?.getString("splitIndex")?.split(",")?.map { it.toInt() } ?: listOf() splitIndex = intent.extras?.getString("splitIndex")?.split(",")?.map { it.toInt() } ?: listOf()
} }
companion object {
var muPDFCore: MuPDFCore? = null
const val DO_SPLIT_PDF = "do_split_pdf"
const val DO_SAVE_PDF = "do_save_pdf"
}
} }
\ No newline at end of file
...@@ -88,7 +88,6 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p ...@@ -88,7 +88,6 @@ class PdfPagerAdapter(val pdfPath: String, val itemLayout: Int = R.layout.item_p
runCatching { runCatching {
val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex, scale) val drawable = PdfBoxUtils.getPdfDrawablePage(context, pdfPath, item.pageIndex, scale)
item.pageDrawable = drawable item.pageDrawable = drawable
// 任务代码
itemView.post { itemView.post {
item.pageDrawable?.let { item.pageDrawable?.let {
iv.setImageDrawable(it) iv.setImageDrawable(it)
......
package com.base.pdfviewerscannerwhite.ui.document.pdf package com.base.pdfviewerscannerwhite.ui.document.pdf
import android.content.Context import android.content.Context
import android.os.Handler
import android.util.Log
import com.artifex.mupdfdemo.MuPDFCore import com.artifex.mupdfdemo.MuPDFCore
import com.artifex.mupdfdemo.OutlineActivityData import com.artifex.mupdfdemo.OutlineActivityData
import com.base.pdfviewerscannerwhite.bean.PdfPageBean import com.base.pdfviewerscannerwhite.bean.PdfPageBean
...@@ -15,14 +13,12 @@ class PdfPresenter( ...@@ -15,14 +13,12 @@ class PdfPresenter(
val context: Context, val context: Context,
val pdfView: PdfView? = null val pdfView: PdfView? = null
) { ) {
private val TAG = "PdfPresenter"
var handler: Handler? = null
fun openFile(path: String): MuPDFCore? { fun openFile(path: String): MuPDFCore? {
val muPDFCore: MuPDFCore? = null var muPDFCore: MuPDFCore? = null
try { try {
MuPDFCore(context, path) muPDFCore = MuPDFCore(context, path)
// 新建:删除旧的目录数据 // 新建:删除旧的目录数据
OutlineActivityData.set(null) OutlineActivityData.set(null)
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
...@@ -44,13 +40,13 @@ class PdfPresenter( ...@@ -44,13 +40,13 @@ class PdfPresenter(
} }
fun splitPdf( fun splitPdf(
file: File, newName: String, splitIndex: List<Int>, srcPath: String, newPath: String, splitIndex: List<Int>,
finishAction: (newFile: File?) -> Unit finishAction: (newFile: File?) -> Unit
) = Thread { ) = Thread {
val newFile = File(file.parentFile, newName)
try { try {
// 加载现有 PDF 文档 // 加载现有 PDF 文档
val sourceDocument = PDDocument.load(file) val sourceDocument = PDDocument.load(File(srcPath))
// 创建新的 PDF 文档 // 创建新的 PDF 文档
val newDocument = PDDocument() val newDocument = PDDocument()
...@@ -61,7 +57,7 @@ class PdfPresenter( ...@@ -61,7 +57,7 @@ class PdfPresenter(
newDocument.addPage(page) newDocument.addPage(page)
} }
// 保存新的 PDF 文档 // 保存新的 PDF 文档
newDocument.save(newFile) newDocument.save(newPath)
newDocument.close() newDocument.close()
sourceDocument.close() sourceDocument.close()
} catch (e: Exception) { } catch (e: Exception) {
...@@ -69,6 +65,48 @@ class PdfPresenter( ...@@ -69,6 +65,48 @@ class PdfPresenter(
finishAction.invoke(null) finishAction.invoke(null)
return@Thread return@Thread
} }
finishAction.invoke(newFile) finishAction.invoke(File(newPath))
}.start() }.start()
fun createNewPath(path: String): String {
val file = File(path)
var newName = file.name.split(".")[0]
//aaaa_1.pdf
if (isUnderscoreNumberSuffix(newName)) {
val nameSplit = newName.split("_")
val number = nameSplit[1].toInt() + 1
newName = "${nameSplit[0]}_$number"
} else {
newName = "${newName}_1"
}
val newFile = File(file.parentFile, "$newName.pdf")
if (!newFile.exists()) {
newFile.createNewFile()
}
return newFile.absolutePath
}
private fun isUnderscoreNumberSuffix(input: String): Boolean {
val pattern = "_\\d+$".toRegex()
return pattern.matches(input)
}
fun savePdf(context: Context, muPDFCore: MuPDFCore?, srcPath: String, newPath: String) {
//保存的逻辑,
// 1.原本的文件拷贝一份
// 2。然后保存修改到原本的文件,重名命名该文件为新文件
// 3.拷贝的文件在恢复原位置
val tempFile = File(context.filesDir, "temp.pdf")
if (!tempFile.exists()) {
tempFile.createNewFile()
}
File(srcPath).compareTo(tempFile)
muPDFCore?.save()
val newFile = File(newPath)
}
} }
\ No newline at end of file
...@@ -7,10 +7,11 @@ ...@@ -7,10 +7,11 @@
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.document.pdf.PdfActivity"> tools:context=".ui.document.pdf.PdfActivity">
<!-- <com.artifex.mupdfdemo.MuPDFReaderView--> <com.artifex.mupdfdemo.MuPDFReaderView
<!-- android:id="@+id/mupdf_reader_view"--> android:id="@+id/mupdf_reader_view"
<!-- android:layout_width="match_parent"--> android:layout_width="match_parent"
<!-- android:layout_height="match_parent" />--> android:layout_height="match_parent" />
<ViewAnimator <ViewAnimator
android:id="@+id/v_animator_top" android:id="@+id/v_animator_top"
...@@ -134,7 +135,7 @@ ...@@ -134,7 +135,7 @@
android:textSize="16sp" android:textSize="16sp"
android:visibility="gone" android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@+id/iv_xuanzhuan" app:layout_constraintEnd_toEndOf="@+id/iv_more"
app:layout_constraintTop_toTopOf="parent" app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
...@@ -142,10 +143,27 @@ ...@@ -142,10 +143,27 @@
</ViewAnimator> </ViewAnimator>
<TextView
android:id="@+id/tv_pageCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="28dp"
android:background="@drawable/bg_54585b_5"
android:includeFontPadding="false"
android:paddingHorizontal="2dp"
android:paddingVertical="2dp"
android:textColor="@color/white"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/v_animator_top"
tools:text="1/3" />
<ViewAnimator <ViewAnimator
android:id="@+id/v_animator_bottom" android:id="@+id/v_animator_bottom"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/white" android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"> app:layout_constraintBottom_toBottomOf="parent">
...@@ -160,14 +178,17 @@ ...@@ -160,14 +178,17 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/white" android:background="@color/white"
android:paddingVertical="8dp"
android:visibility="gone" android:visibility="gone"
app:layout_constraintBottom_toTopOf="@id/v_animator_bottom"> app:layout_constraintBottom_toTopOf="@id/v_animator_bottom">
<LinearLayout <LinearLayout
android:id="@+id/ll_highlight" android:id="@+id/ll_highlight"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@id/ll_glide_line" app:layout_constraintEnd_toStartOf="@id/ll_glide_line"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
...@@ -194,8 +215,10 @@ ...@@ -194,8 +215,10 @@
<LinearLayout <LinearLayout
android:id="@+id/ll_glide_line" android:id="@+id/ll_glide_line"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@id/ll_strikethrough" app:layout_constraintEnd_toStartOf="@id/ll_strikethrough"
app:layout_constraintStart_toEndOf="@id/ll_highlight" app:layout_constraintStart_toEndOf="@id/ll_highlight"
...@@ -222,8 +245,10 @@ ...@@ -222,8 +245,10 @@
<LinearLayout <LinearLayout
android:id="@+id/ll_strikethrough" android:id="@+id/ll_strikethrough"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintEnd_toStartOf="@id/ll_painting_brush" app:layout_constraintEnd_toStartOf="@id/ll_painting_brush"
app:layout_constraintStart_toEndOf="@id/ll_glide_line" app:layout_constraintStart_toEndOf="@id/ll_glide_line"
...@@ -252,6 +277,7 @@ ...@@ -252,6 +277,7 @@
android:id="@+id/ll_painting_brush" android:id="@+id/ll_painting_brush"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/ll_strikethrough" app:layout_constraintStart_toEndOf="@id/ll_strikethrough"
...@@ -294,4 +320,16 @@ ...@@ -294,4 +320,16 @@
</ViewAnimator> </ViewAnimator>
<ImageView
android:id="@+id/iv_bianji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="15dp"
android:layout_marginBottom="108dp"
android:src="@mipmap/bianji"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="ContentDescription" />
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
...@@ -40,7 +40,6 @@ ...@@ -40,7 +40,6 @@
android:layout_marginHorizontal="56dp" android:layout_marginHorizontal="56dp"
android:layout_marginTop="19dp" android:layout_marginTop="19dp"
android:max="100" android:max="100"
android:progress="50"
android:progressDrawable="@drawable/progress_bg" android:progressDrawable="@drawable/progress_bg"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
......
...@@ -4,11 +4,23 @@ plugins { ...@@ -4,11 +4,23 @@ plugins {
id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.android'
} }
android { android {
compileSdkVersion 28
namespace "com.lonelypluto.pdfviewerlibrary" namespace "com.lonelypluto.pdfviewerlibrary"
compileSdk 34
defaultConfig { defaultConfig {
minSdkVersion 17 minSdk 24
targetSdk 34
ndk {
abiFilters "arm64-v8a"
// "armeabi-v7a"
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
} }
buildTypes { buildTypes {
...@@ -25,6 +37,5 @@ android { ...@@ -25,6 +37,5 @@ android {
dependencies { dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar']) implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation 'com.android.support:appcompat-v7:28.0.0'
implementation(libs.androidx.appcompat) implementation(libs.androidx.appcompat)
} }
\ No newline at end of file
...@@ -4,7 +4,9 @@ import android.content.Context; ...@@ -4,7 +4,9 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.PointF; import android.graphics.PointF;
import android.graphics.RectF; import android.graphics.RectF;
import java.util.ArrayList; import java.util.ArrayList;
import com.lonelypluto.pdfviewerlibrary.R; import com.lonelypluto.pdfviewerlibrary.R;
public class MuPDFCore { public class MuPDFCore {
...@@ -12,17 +14,23 @@ public class MuPDFCore { ...@@ -12,17 +14,23 @@ public class MuPDFCore {
private static boolean gs_so_available = false; private static boolean gs_so_available = false;
static { static {
System.out.println("Loading dll"); try {
System.loadLibrary("mupdf_java"); System.out.println("Loading dll");
System.out.println("Loaded dll"); System.loadLibrary("mupdf_java");
if (gprfSupportedInternal()) { System.out.println("Loaded dll");
try { Boolean flag = gprfSupportedInternal();
System.loadLibrary("gs"); if (flag) {
gs_so_available = true; try {
} catch (UnsatisfiedLinkError e) { System.loadLibrary("gs");
gs_so_available = false; gs_so_available = true;
} catch (UnsatisfiedLinkError e) {
gs_so_available = false;
}
} }
} catch (Exception e) {
e.printStackTrace();
} }
} }
/* Readable members */ /* Readable members */
......
...@@ -14,9 +14,7 @@ import android.view.View; ...@@ -14,9 +14,7 @@ import android.view.View;
import android.widget.Adapter; import android.widget.Adapter;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.Scroller; import android.widget.Scroller;
import com.lonelypluto.pdfviewerlibrary.R; import com.lonelypluto.pdfviewerlibrary.R;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
...@@ -108,12 +106,14 @@ public class ReaderView ...@@ -108,12 +106,14 @@ public class ReaderView
} }
public void setDisplayedViewIndex(int i) { public void setDisplayedViewIndex(int i) {
if (0 <= i && i < mAdapter.getCount()) { if (mAdapter != null) {
onMoveOffChild(mCurrent); if (0 <= i && i < mAdapter.getCount()) {
mCurrent = i; onMoveOffChild(mCurrent);
onMoveToChild(i); mCurrent = i;
mResetLayout = true; onMoveToChild(i);
requestLayout(); mResetLayout = true;
requestLayout();
}
} }
} }
...@@ -590,7 +590,9 @@ public class ReaderView ...@@ -590,7 +590,9 @@ public class ReaderView
super.onLayout(changed, left, top, right, bottom); super.onLayout(changed, left, top, right, bottom);
try { try {
onLayout2(changed, left, top, right, bottom); if (mAdapter != null) {
onLayout2(changed, left, top, right, bottom);
}
} catch (OutOfMemoryError e) { } catch (OutOfMemoryError e) {
System.out.println("Out of memory during layout"); System.out.println("Out of memory during layout");
...@@ -625,6 +627,7 @@ public class ReaderView ...@@ -625,6 +627,7 @@ public class ReaderView
return; return;
View cv = mChildViews.get(mCurrent); View cv = mChildViews.get(mCurrent);
Point cvOffset; Point cvOffset;
if (!mResetLayout) { if (!mResetLayout) {
...@@ -828,10 +831,12 @@ public class ReaderView ...@@ -828,10 +831,12 @@ public class ReaderView
private View getOrCreateChild(int i) { private View getOrCreateChild(int i) {
View v = mChildViews.get(i); View v = mChildViews.get(i);
if (v == null) { if (v == null) {
v = mAdapter.getView(i, getCached(), this); if (mAdapter != null) {
addAndMeasureChild(i, v); v = mAdapter.getView(i, getCached(), this);
onChildSetup(i, v); addAndMeasureChild(i, v);
onScaleChild(v, mScale); onChildSetup(i, v);
onScaleChild(v, mScale);
}
} }
return v; return v;
...@@ -922,8 +927,14 @@ public class ReaderView ...@@ -922,8 +927,14 @@ public class ReaderView
} }
private Point subScreenSizeOffset(View v) { private Point subScreenSizeOffset(View v) {
return new Point(Math.max((getWidth() - v.getMeasuredWidth()) / 2, 0), int mw = 0;
Math.max((getHeight() - v.getMeasuredHeight()) / 2, 0)); int mh = 0;
if (v != null) {
mw = v.getMeasuredWidth();
mh = v.getMeasuredHeight();
}
return new Point(Math.max((getWidth() - mw) / 2, 0),
Math.max((getHeight() - mh) / 2, 0));
} }
private static int directionOfTravel(float vx, float vy) { private static int directionOfTravel(float vx, float vy) {
......
...@@ -23,6 +23,7 @@ import java.net.MalformedURLException; ...@@ -23,6 +23,7 @@ import java.net.MalformedURLException;
public class SavePdf { public class SavePdf {
private float defaultScale = 0.90756303f; private float defaultScale = 0.90756303f;
public void setWidthScale(float widthScale) { public void setWidthScale(float widthScale) {
this.widthScale = widthScale; this.widthScale = widthScale;
} }
...@@ -40,11 +41,12 @@ public class SavePdf { ...@@ -40,11 +41,12 @@ public class SavePdf {
private float scale; private float scale;
private float density; ///手机屏幕的分辨率密度 private float density; ///手机屏幕的分辨率密度
private float width; private float width;
private float height; private float height;
/** /**
* 设置放大比例 * 设置放大比例
*
* @param scale * @param scale
*/ */
public void setScale(float scale) { public void setScale(float scale) {
...@@ -53,6 +55,7 @@ public class SavePdf { ...@@ -53,6 +55,7 @@ public class SavePdf {
/** /**
* 设置宽高 * 设置宽高
*
* @param * @param
*/ */
public void setWH(float width, float height) { public void setWH(float width, float height) {
...@@ -98,43 +101,24 @@ public class SavePdf { ...@@ -98,43 +101,24 @@ public class SavePdf {
public void addText() { public void addText() {
try { try {
PdfReader reader = new PdfReader(inPath, "PDF".getBytes());///打开要写入的PDF PdfReader reader = new PdfReader(inPath, "PDF".getBytes());///打开要写入的PDF
Log.e("addText", "1");
FileOutputStream outputStream = new FileOutputStream(outPath);//设置涂鸦后的PDF FileOutputStream outputStream = new FileOutputStream(outPath);//设置涂鸦后的PDF
Log.e("addText", "2");
PdfStamper stamp; PdfStamper stamp;
stamp = new PdfStamper(reader, outputStream); stamp = new PdfStamper(reader, outputStream);
Log.e("addText", "3 " + pageNum);
PdfContentByte over = stamp.getOverContent(pageNum);//////用于设置在第几页打印签名 PdfContentByte over = stamp.getOverContent(pageNum);//////用于设置在第几页打印签名
byte[] bytes = Bitmap2Bytes(bitmap); Log.e("addText", "4");
Image img = Image.getInstance(bytes);//将要放到PDF的图片传过来,要设置为byte[]类型 if (bitmap != null) {
com.lowagie.text.Rectangle rectangle = reader.getPageSize(pageNum); byte[] bytes = Bitmap2Bytes(bitmap);
img.setAlignment(Image.MIDDLE);// 图像在文档中的对齐方式 Image img = Image.getInstance(bytes);//将要放到PDF的图片传过来,要设置为byte[]类型
com.lowagie.text.Rectangle rectangle = reader.getPageSize(pageNum);
//这里是重点!!!!!设置Image图片大小,需要根据屏幕的分辨率,签名时PDF的放大比例来计算;还有就是当PDF开始显示的时候,他已经做了一次缩放,可以用 rectangle.getWidth() / (bitmap.getWidth() / 2)求得那个放大比 img.setAlignment(Image.MIDDLE);// 图像在文档中的对齐方式
// img.scaleAbsolute(363 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2), 557 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2)); img.scalePercent(rectangle.getWidth() * widthScale * 100);
// img.scaleAbsolute(594 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2), 557 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2)); img.setAbsolutePosition(width * (rectangle.getWidth() * widthScale) * (scale), rectangle.getHeight() - ((height) * (rectangle.getWidth() * widthScale) * (scale / defaultScale)) + img.getHeight() / 2 * widthScale * 100);
// img.scaleAbsolute(602 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2), 870 * 1.0f * density / 2 / scale * rectangle.getWidth() / (bitmap.getWidth() / 2)); over.addImage(img);
//这里设置image相对PDF左下角的偏移量,我的做法是得到放大后位置相对于整个PDF的百分比再乘PDF的大小得到他的相对偏移位置 }
// img.setAbsolutePosition(rectangle.getWidth() * widthScale, rectangle.getHeight() * heightScale);// 偏右上 Log.e("addText", "5");
Log.e("zyw", "position = " + rectangle.getWidth() * widthScale + " " + rectangle.getHeight() * heightScale);
Log.e("zyw", "density = " + density);
Log.e("zyw", "img.getWidth() = " + img.getWidth() + " img.getHeight() = " + img.getHeight());
Log.e("zyw", "scale = " + scale);
Log.e("zyw", "widthScale = " + widthScale + " heightScale = " + heightScale);
Log.e("zyw", "bitmap.w = " + bitmap.getWidth() + " bitmap.h = " + bitmap.getHeight());
Log.e("zyw", "rectangle.getLeft = " + rectangle.getLeft() + " rectangle.getBottom() = " + rectangle.getBottom());
Log.e("zyw", "rectangle.getWidth = " + rectangle.getWidth() + " rectangle.getHeight = " + rectangle.getHeight());
Log.e("zyw", "比例1 = " + ((float)rectangle.getWidth()/img.getWidth())*100);
Log.e("zyw", "比例2 = " + rectangle.getWidth() * widthScale*100);
Log.e("zyw", "坐标AbsolutePosition = " + width * (rectangle.getWidth() * widthScale) + " " + (1964-height-img.getHeight()) * (rectangle.getWidth() * widthScale));
Log.e("zyw", "差值 = " + rectangle.getHeight() * (heightScale-widthScale));
// img.scalePercent(((float)594/1080)*100);
Log.e("zyw", "缩放比例 = " + scale / defaultScale);
img.scalePercent(rectangle.getWidth() * widthScale*100);
// img.setAbsolutePosition(rectangle.getLeft(), rectangle.getBottom() - 120);
img.setAbsolutePosition(width * (rectangle.getWidth() * widthScale) * (scale ), rectangle.getHeight() - ((height) * (rectangle.getWidth() * widthScale) * (scale / defaultScale)) + img.getHeight()/2*widthScale*100);
// img.setAbsolutePosition(rectangle.getLeft() + percent/2, rectangle.getBottom() - percent/2);// 偏左下
over.addImage(img);
stamp.close(); stamp.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
e.printStackTrace(); e.printStackTrace();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment