|
@@ -1,52 +1,187 @@
|
|
|
package com.compdfkit.pdfviewer;
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
-import androidx.core.content.ContextCompat;
|
|
|
-import androidx.core.graphics.drawable.DrawableCompat;
|
|
|
+import androidx.appcompat.widget.Toolbar;
|
|
|
+import androidx.core.view.MenuProvider;
|
|
|
|
|
|
import android.content.Intent;
|
|
|
import android.graphics.Bitmap;
|
|
|
import android.graphics.BitmapFactory;
|
|
|
-import android.graphics.Canvas;
|
|
|
-import android.graphics.drawable.BitmapDrawable;
|
|
|
-import android.graphics.drawable.Drawable;
|
|
|
+import android.graphics.RectF;
|
|
|
import android.net.Uri;
|
|
|
import android.os.Build;
|
|
|
import android.os.Bundle;
|
|
|
import android.os.Environment;
|
|
|
import android.provider.Settings;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.DragEvent;
|
|
|
+import android.view.Menu;
|
|
|
+import android.view.MenuInflater;
|
|
|
+import android.view.MenuItem;
|
|
|
+import android.view.View;
|
|
|
+import android.widget.ImageView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
import com.compdfkit.core.document.CPDFDocument;
|
|
|
+import com.compdfkit.core.page.CPDFPage;
|
|
|
import com.compdfkit.core.utils.ColorUtils;
|
|
|
-import com.compdfkit.core.watermark.CPDFImageWatermark;
|
|
|
import com.compdfkit.core.watermark.CPDFTextWatermark;
|
|
|
import com.compdfkit.core.watermark.CPDFWatermark;
|
|
|
+import com.compdfkit.pdfviewer.customview.WatermarkView;
|
|
|
+import com.compdfkit.pdfviewer.fragments.WatermarkConsoleFragment;
|
|
|
import com.compdfkit.ui.reader.CPDFReaderView;
|
|
|
+import com.google.android.material.tabs.TabLayout;
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
|
-public class MainActivity extends AppCompatActivity {
|
|
|
+public class MainActivity extends AppCompatActivity implements MenuProvider {
|
|
|
+ private ImageView pageImage;
|
|
|
+
|
|
|
+ private CPDFDocument document = new CPDFDocument(this);
|
|
|
+
|
|
|
+ private WatermarkView watermark;
|
|
|
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_main);
|
|
|
|
|
|
-// if (!(Build.VERSION.SDK_INT < Build.VERSION_CODES.R || Environment.isExternalStorageManager())) {
|
|
|
-// Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
-// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
-// startActivity(intent);
|
|
|
-// }
|
|
|
+ Toolbar toolbar = findViewById(R.id.toolbar);
|
|
|
+ setSupportActionBar(toolbar);
|
|
|
+
|
|
|
+ watermark = findViewById(R.id.watermark_text);
|
|
|
+ WatermarkConsoleFragment consoleFragment = (WatermarkConsoleFragment) getSupportFragmentManager().findFragmentByTag("console_fragment");
|
|
|
+
|
|
|
+ assert consoleFragment != null;
|
|
|
+ consoleFragment.setAssociatedWatermark(watermark);
|
|
|
+
|
|
|
+ pageImage = findViewById(R.id.page_image);
|
|
|
+
|
|
|
+ TabLayout tabLayout = findViewById(R.id.tab_layout);
|
|
|
+ tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
|
|
|
+ @Override
|
|
|
+
|
|
|
+ public void onTabSelected(TabLayout.Tab tab) {
|
|
|
+ changeWatermark(tab.getPosition());
|
|
|
+
|
|
|
+ consoleFragment.changePicker(tab.getPosition());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTabUnselected(TabLayout.Tab tab) {}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onTabReselected(TabLayout.Tab tab) {}
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!(Build.VERSION.SDK_INT < Build.VERSION_CODES.R || Environment.isExternalStorageManager())) {
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
|
|
|
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
|
|
|
File PDFFile = new File("/storage/emulated/0/TestPDFs/01.pdf");
|
|
|
Uri PDFUri = Uri.fromFile(PDFFile);
|
|
|
|
|
|
- CPDFDocument document = new CPDFDocument(this);
|
|
|
CPDFDocument.PDFDocumentError error = document.open(PDFFile.getAbsolutePath());
|
|
|
|
|
|
- CPDFReaderView readerView;
|
|
|
- readerView = (CPDFReaderView) findViewById(R.id.pdf_view);
|
|
|
+ CPDFPage currentPage = document.pageAtIndex(0);
|
|
|
+
|
|
|
+ RectF pageSize = document.getPageSize(0);
|
|
|
+ float pageW = pageSize.right - pageSize.left;
|
|
|
+ float pageH = pageSize.bottom - pageSize.top;
|
|
|
+
|
|
|
+ pageImage.post(() -> {
|
|
|
+ int imageWidth = (int) (pageImage.getHeight() / pageH * pageW);
|
|
|
+ Bitmap pageBitmap = Bitmap.createBitmap(imageWidth, pageImage.getHeight(), Bitmap.Config.RGB_565);
|
|
|
+
|
|
|
+ currentPage.renderPage(pageBitmap, 0, 0, imageWidth, pageImage.getHeight(), 0xFFFFFFFF, 100, 0, true, true);
|
|
|
+ pageImage.setImageBitmap(pageBitmap);
|
|
|
+
|
|
|
+ pageImage.setOnDragListener(new View.OnDragListener() {
|
|
|
+ float currentSlope;
|
|
|
+
|
|
|
+ final float convertRate = (float) (180 / 3.141592653);
|
|
|
+
|
|
|
+ double currentRotation = 0;
|
|
|
+ double resultRotation = 0;
|
|
|
+
|
|
|
+ int[] location = new int[2];
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onDrag(View view, DragEvent dragEvent) {
|
|
|
+
|
|
|
+ if (watermark.getCurrentEvent() == 0) {
|
|
|
+ float xOffset = 1.0f * watermark.getWidth() / 2;
|
|
|
+ float yOffset = 1.0f * watermark.getHeight() / 2;
|
|
|
+
|
|
|
+ int xOutline = pageImage.getLeft();
|
|
|
+ int yOutline = pageImage.getTop();
|
|
|
+ watermark.getLocationOnScreen(location);
|
|
|
+
|
|
|
+ //TODO:需要优化控件越界行为
|
|
|
+ switch (dragEvent.getAction()) {
|
|
|
+ case DragEvent.ACTION_DRAG_STARTED:
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_ENTERED:
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_LOCATION:
|
|
|
+ watermark.setX(dragEvent.getX() + xOutline - xOffset); // dragEvent.getX()
|
|
|
+ watermark.setY(dragEvent.getY() + yOutline - yOffset); // dragEvent.getY()
|
|
|
+
|
|
|
+ Log.d("DragTest", "Drag Location: (" + dragEvent.getX() + "," + dragEvent.getY() + ")");
|
|
|
+ Log.d("DragTest", "Watermark Location: (" + location[0] + "," + location[1] + ")");
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_ENDED:
|
|
|
+ watermark.setVisibility(View.VISIBLE);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ float xOffset = 1.0f * watermark.getWidth() / 2;/* 1.0f * watermark.getWatermarkLayout().getWidth() / 2; /* 控件一半宽度 */
|
|
|
+ float yOffset = 1.0f * watermark.getHeight() / 2;;/*1.0f * watermark.getWatermarkLayout().getHeight() / 2; /* 控件一半高度 */
|
|
|
+
|
|
|
+ float baseSlope = yOffset / xOffset * -1;
|
|
|
+
|
|
|
+ watermark.getLocationOnScreen(location);
|
|
|
+
|
|
|
+ float centerPointX = watermark.getX() + 1.0f * watermark.getWidth() / 2; /* 控件中心点x = 当前控件位置x + 控件一半宽度 */
|
|
|
+ float centerPointY = watermark.getY() + 1.0f * watermark.getHeight() / 2; /* 控件中心点y = 当前控件位置y + 控件一半高度 */
|
|
|
+
|
|
|
+ switch (dragEvent.getAction()) {
|
|
|
+ case DragEvent.ACTION_DRAG_STARTED:
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_ENTERED:
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_LOCATION:
|
|
|
+
|
|
|
+ //TODO:该部分的取坐标存在冗余代码
|
|
|
+ currentSlope = (dragEvent.getY() + pageImage.getTop() - centerPointY) / (dragEvent.getX() + pageImage.getLeft() - centerPointX) * -1;
|
|
|
+ currentRotation = Math.atan(currentSlope) * convertRate;
|
|
|
+
|
|
|
+ if (dragEvent.getY() + pageImage.getTop() < centerPointY && dragEvent.getX() + pageImage.getLeft() < centerPointX)
|
|
|
+ currentRotation += 180;
|
|
|
+
|
|
|
+ if (dragEvent.getY() + pageImage.getTop() > centerPointY && dragEvent.getX() + pageImage.getLeft() < centerPointX)
|
|
|
+ currentRotation += 180;
|
|
|
+
|
|
|
+ resultRotation = currentRotation - Math.atan(baseSlope) * convertRate;
|
|
|
+
|
|
|
+ watermark.setRotation((float) resultRotation * -1);
|
|
|
+ watermark.setRotationRadian((float) Math.atan(currentSlope));
|
|
|
+ Log.d("RollTest", "Current slope: " + currentSlope + ", Result rotation: " + resultRotation + ", Current rotation: " + currentRotation);
|
|
|
+ return true;
|
|
|
+ case DragEvent.ACTION_DRAG_ENDED:
|
|
|
+ watermark.setVisibility(View.VISIBLE);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
|
|
|
if (error == CPDFDocument.PDFDocumentError.PDFDocumentErrorPassword) {
|
|
|
error = document.open(PDFUri, "password");
|
|
@@ -56,78 +191,74 @@ public class MainActivity extends AppCompatActivity {
|
|
|
Toast.makeText(this, "File failed.", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
|
|
|
- readerView.setPDFDocument(document);
|
|
|
- readerView.setVerticalMode(true);
|
|
|
- readerView.setDoublePageMode(false);
|
|
|
-
|
|
|
int watermarkIndex = 0;
|
|
|
- CPDFWatermark watermark = null;
|
|
|
+ CPDFWatermark watermarkz = null;
|
|
|
|
|
|
try {
|
|
|
- watermark = document.getWatermark(watermarkIndex);
|
|
|
- if (watermark != null)
|
|
|
- watermark.clear();
|
|
|
+ watermarkz = document.getWatermark(watermarkIndex);
|
|
|
+ if (watermarkz != null)
|
|
|
+ watermarkz.clear();
|
|
|
} finally {
|
|
|
- if (watermark != null && watermark.isValid()) {
|
|
|
- watermark.release();
|
|
|
+ if (watermarkz != null && watermarkz.isValid()) {
|
|
|
+ watermarkz.release();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- Drawable drawable = ContextCompat.getDrawable(this, R.drawable.pdf);
|
|
|
-
|
|
|
- CPDFImageWatermark imgWatermark = (CPDFImageWatermark)
|
|
|
- document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_IMG);
|
|
|
-
|
|
|
- assert drawable != null;
|
|
|
- Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
|
|
- Canvas canvas = new Canvas(bitmap);
|
|
|
- drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
|
- drawable.draw(canvas);
|
|
|
-
|
|
|
- try {
|
|
|
- if (imgWatermark != null && imgWatermark.isValid()) {
|
|
|
- imgWatermark.setText("RA"); //设置文本
|
|
|
- imgWatermark.setFontName("Helvetica"); //设置字体
|
|
|
- imgWatermark.setImage(bitmap, 100, 100);
|
|
|
- imgWatermark.setTextRGBColor(ColorUtils.parseColor(new int[] { 102, 204, 255 }));
|
|
|
- imgWatermark.setOpacity(0.5f);
|
|
|
- imgWatermark.setRotation(0.785f);
|
|
|
- imgWatermark.setFontSize(100);
|
|
|
- imgWatermark.setFront(false);
|
|
|
- imgWatermark.setVertalign(CPDFWatermark.Vertalign.WATERMARK_VERTALIGN_CENTER);
|
|
|
- imgWatermark.setHorizalign(CPDFWatermark.Horizalign.WATERMARK_HORIZALIGN_CENTER);
|
|
|
- imgWatermark.setVertOffset(0);
|
|
|
- imgWatermark.setHorizOffset(0);
|
|
|
- imgWatermark.setScale(0.2f);
|
|
|
- imgWatermark.setPages("1,3-5");
|
|
|
- imgWatermark.setFullScreen(true);
|
|
|
- imgWatermark.setHorizontalSpacing(100);
|
|
|
- imgWatermark.setVerticalSpacing(100);
|
|
|
- imgWatermark.update();
|
|
|
- }
|
|
|
- } finally {
|
|
|
- if (imgWatermark != null && imgWatermark.isValid()) {
|
|
|
- imgWatermark.release();
|
|
|
- }
|
|
|
- }
|
|
|
+// Drawable drawable = ContextCompat.getDrawable(this, R.drawable.pdf);
|
|
|
+//
|
|
|
+// CPDFImageWatermark imgWatermark = (CPDFImageWatermark)
|
|
|
+// document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_IMG);
|
|
|
+//
|
|
|
+// assert drawable != null;
|
|
|
+// Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
|
|
+// Canvas canvas = new Canvas(bitmap);
|
|
|
+// drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
|
|
+// drawable.draw(canvas);
|
|
|
+//
|
|
|
+// try {
|
|
|
+// if (imgWatermark != null && imgWatermark.isValid()) {
|
|
|
+// imgWatermark.setText("RA"); //设置文本
|
|
|
+// imgWatermark.setFontName("Helvetica"); //设置字体
|
|
|
+// imgWatermark.setImage(bitmap, 100, 100);
|
|
|
+// imgWatermark.setTextRGBColor(ColorUtils.parseColor(new int[] { 102, 204, 255 }));
|
|
|
+// imgWatermark.setOpacity(0.5f);
|
|
|
+// imgWatermark.setRotation(0.785f);
|
|
|
+// imgWatermark.setFontSize(100);
|
|
|
+// imgWatermark.setFront(false);
|
|
|
+// imgWatermark.setVertalign(CPDFWatermark.Vertalign.WATERMARK_VERTALIGN_CENTER);
|
|
|
+// imgWatermark.setHorizalign(CPDFWatermark.Horizalign.WATERMARK_HORIZALIGN_CENTER);
|
|
|
+// imgWatermark.setVertOffset(0);
|
|
|
+// imgWatermark.setHorizOffset(0);
|
|
|
+// imgWatermark.setScale(0.2f);
|
|
|
+// imgWatermark.setPages("1,3-5");
|
|
|
+// imgWatermark.setFullScreen(true);
|
|
|
+// imgWatermark.setHorizontalSpacing(100);
|
|
|
+// imgWatermark.setVerticalSpacing(100);
|
|
|
+// imgWatermark.update();
|
|
|
+// }
|
|
|
+// } finally {
|
|
|
+// if (imgWatermark != null && imgWatermark.isValid()) {
|
|
|
+// imgWatermark.release();
|
|
|
+// }
|
|
|
+// }
|
|
|
|
|
|
// CPDFTextWatermark textWatermark = (CPDFTextWatermark)
|
|
|
// document.createWatermark(CPDFWatermark.Type.WATERMARK_TYPE_TEXT);
|
|
|
// try {
|
|
|
// if (textWatermark != null && textWatermark.isValid()) {
|
|
|
-// textWatermark.setText("RA"); //设置文本
|
|
|
+// textWatermark.setText("Watermark"); //设置文本
|
|
|
// textWatermark.setFontName("Helvetica"); //设置字体
|
|
|
-// textWatermark.setTextRGBColor(ColorUtils.parseColor(new int[] { 102, 204, 255 }));
|
|
|
+// textWatermark.setTextRGBColor(ColorUtils.parseColor(new int[] { 255, 0, 0 }));
|
|
|
// textWatermark.setOpacity(1f);
|
|
|
-// textWatermark.setFontSize(100);
|
|
|
-// textWatermark.setFront(false);
|
|
|
+// textWatermark.setFontSize(62);
|
|
|
+// textWatermark.setFront(true);
|
|
|
// textWatermark.setVertalign(CPDFWatermark.Vertalign.WATERMARK_VERTALIGN_CENTER);
|
|
|
// textWatermark.setHorizalign(CPDFWatermark.Horizalign.WATERMARK_HORIZALIGN_CENTER);
|
|
|
// textWatermark.setVertOffset(0);
|
|
|
// textWatermark.setHorizOffset(0);
|
|
|
// textWatermark.setScale(1.0f);
|
|
|
-// textWatermark.setPages("1,3-5");
|
|
|
-// textWatermark.setFullScreen(true);
|
|
|
+// textWatermark.setPages("0-5");
|
|
|
+// textWatermark.setFullScreen(false);
|
|
|
// textWatermark.setHorizontalSpacing(10);
|
|
|
// textWatermark.setVerticalSpacing(10);
|
|
|
// textWatermark.update();
|
|
@@ -138,4 +269,28 @@ public class MainActivity extends AppCompatActivity {
|
|
|
// }
|
|
|
// }
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onCreateMenu(@NonNull Menu menu, @NonNull MenuInflater menuInflater) {
|
|
|
+ menuInflater.inflate(R.menu.menu_done, menu);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean onMenuItemSelected(@NonNull MenuItem menuItem) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void changeWatermark(int type) {
|
|
|
+ watermark.setVisibility(View.INVISIBLE);
|
|
|
+ if (type == 1) {
|
|
|
+ watermark = findViewById(R.id.watermark_image);
|
|
|
+ } else {
|
|
|
+ watermark = findViewById(R.id.watermark_text);
|
|
|
+ }
|
|
|
+ watermark.setVisibility(View.VISIBLE);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refreshPageImage(Bitmap updatePageBitmap) {
|
|
|
+ pageImage.setImageBitmap(updatePageBitmap);
|
|
|
+ }
|
|
|
}
|