Kaynağa Gözat

Feature: add TextBox/Stamp/Shape to PDF file

cooperku_kdanmobile 5 yıl önce
ebeveyn
işleme
4e3f6392e2

+ 3 - 3
reader/src/main/java/com/kdanmobile/reader/ReaderActivity.kt

@@ -373,17 +373,17 @@ abstract class ReaderActivity : AppCompatActivity() {
             onViewerEditTabAddButtonClickListener = object : ViewerEditView.OnViewerEditTabAddButtonClickListener {
                 override fun onTabTextBoxAddButtonClick(textBoxTabView: TextBoxTabView) {
                     hideAllToolbars()
-                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+                    viewModel.setTextBoxAttribute(textBoxTabView.getTextBoxAttribute())
                 }
 
                 override fun onTabStampAddButtonClick(stampTabView: StampTabView) {
                     hideAllToolbars()
-                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+                    viewModel.setStampAttribute(stampTabView.getStampAttribute())
                 }
 
                 override fun onTabShapeAddButtonClick(shapeTabView: ShapeTabView) {
                     hideAllToolbars()
-                    TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
+                    viewModel.setShapeAttribute(shapeTabView.getShapeAttribute())
                 }
             }
         }

+ 33 - 0
reader/src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -8,10 +8,14 @@ import android.graphics.Bitmap
 import android.graphics.RectF
 import android.net.Uri
 import com.kdanmobile.kmpdfkit.annotation.bean.*
+import com.kdanmobile.kmpdfkit.globaldata.AnnotConfig
 import com.kdanmobile.kmpdfkit.globaldata.Config
 import com.kdanmobile.kmpdfkit.manager.KMPDFFactory
 import com.kdanmobile.kmpdfkit.manager.controller.KMPDFDocumentController
 import com.kdanmobile.kmpdfkit.pdfcommon.*
+import com.kdanmobile.reader.screen.data.ShapeAttribute
+import com.kdanmobile.reader.screen.data.StampAttribute
+import com.kdanmobile.reader.screen.data.TextBoxAttribute
 import com.kdanmobile.reader.screen.handler.*
 
 class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMsg: String) : ViewModel() {
@@ -206,6 +210,35 @@ class ReaderViewModel(private val pdfSdkLicense: String, private val pdfSdkRsaMs
         }
     }
 
+    fun setTextBoxAttribute(attr: TextBoxAttribute) {
+
+        val kmpdfFreetextAnnotationBean = KMPDFFreetextAnnotationBean("", attr.textColor, attr.textSize, 255, "Courier", attr.isBold, attr.isItalic)
+        kmpdfFactory?.setAnnotationAttribute(kmpdfFreetextAnnotationBean)
+        kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.FREETEXT)
+    }
+
+    fun setStampAttribute(attr: StampAttribute) {
+
+        val kmpdfStampAnnotationBean = when (attr.isStandardStamp()) {
+            true -> KMPDFStampAnnotationBean("", KMPDFStampAnnotationBean.StampType.STANDARD, KMPDFStampAnnotationBean.StandardStamp(attr.resId))
+            false -> KMPDFStampAnnotationBean("", KMPDFStampAnnotationBean.StampType.TEXT, KMPDFStampAnnotationBean.TextStamp(attr.rect, attr.config))
+        }
+        kmpdfFactory?.setAnnotationAttribute(kmpdfStampAnnotationBean)
+        kmpdfFactory?.setAnnotationEditMode(KMPDFAnnotationBean.AnnotationType.STAMP)
+    }
+
+    fun setShapeAttribute(attr: ShapeAttribute) {
+
+        val kmpdfShapeAnnotationBean = when (attr.shapeType) {
+            AnnotConfig.ShapeAnnotationType.LINE -> KMPDFLineAnnotationBean("", attr.lineColor, attr.lineWidth, attr.lineAlpha)
+            AnnotConfig.ShapeAnnotationType.ARROW -> KMPDFArrowAnnotationBean("", attr.lineColor, attr.lineWidth, attr.lineAlpha)
+            AnnotConfig.ShapeAnnotationType.CIRCLE -> KMPDFCircleAnnotationBean("", attr.lineColor, attr.lineWidth, attr.lineAlpha, attr.fillColor, attr.fillAlpha)
+            AnnotConfig.ShapeAnnotationType.SQUARE -> KMPDFSquareAnnotationBean("", attr.lineColor, attr.lineWidth, attr.lineAlpha, attr.fillColor, attr.fillAlpha)
+        }
+        kmpdfFactory?.setAnnotationAttribute(kmpdfShapeAnnotationBean)
+        kmpdfFactory?.setAnnotationEditMode(kmpdfShapeAnnotationBean.type)
+    }
+
     val pdfInfoHandler = object : PdfInfoHandler {
         override fun getOpenPdfFilename(): String {
             return kmpdfFactory?.fileName ?: ""

+ 5 - 0
reader/src/main/java/com/kdanmobile/reader/screen/data/ShapeAttribute.kt

@@ -0,0 +1,5 @@
+package com.kdanmobile.reader.screen.data
+
+import com.kdanmobile.kmpdfkit.globaldata.AnnotConfig
+
+data class ShapeAttribute(val shapeType: AnnotConfig.ShapeAnnotationType, val lineColor: Int, val lineWidth: Float, val lineAlpha: Int, val fillColor: Int, val fillAlpha: Int)

+ 11 - 0
reader/src/main/java/com/kdanmobile/reader/screen/data/StampAttribute.kt

@@ -0,0 +1,11 @@
+package com.kdanmobile.reader.screen.data
+
+import android.graphics.Rect
+import com.kdanmobile.kmpdfkit.annotation.stamp.StampConfig
+import com.kdanmobile.kmpdfkit.annotation.stamp.TextStampConfig
+
+data class StampAttribute(val resId: StampConfig.STANDARD_STAMP_RES? = null, val rect: Rect? = null, val config: TextStampConfig? = null) {
+    fun isStandardStamp(): Boolean {
+        return null != resId
+    }
+}

+ 3 - 0
reader/src/main/java/com/kdanmobile/reader/screen/data/TextBoxAttribute.kt

@@ -0,0 +1,3 @@
+package com.kdanmobile.reader.screen.data
+
+data class TextBoxAttribute(val isBold: Boolean, val isItalic: Boolean, val textAlignment: Int, val textColor: Int, val textSize: Int)

+ 9 - 0
reader/src/main/java/com/kdanmobile/reader/screen/view/edit/ShapeTabView.kt

@@ -88,6 +88,15 @@ class ShapeTabView : ConstraintLayout {
         tabLayout.setupWithViewPager(viewPager)
     }
 
+    fun getShapeAttribute() : ShapeAttribute {
+        val lineColor = 0xffff0000.toInt()
+        val lineWidth = seekBarShapeBorderThickness?.currentValue?.toFloat() ?: 3f
+        val lineAlpha = seekBarShapeBorderOpacity?.currentValue ?: 255
+        val fillColor = 0xff00ff00.toInt()
+        val fillAlpha = seekBarShapeFillOpacity?.currentValue ?: 0
+        return ShapeAttribute(shapeType, lineColor, lineWidth, lineAlpha, fillColor, fillAlpha)
+    }
+
     private inner class ViewPagerAdapter(context: Context, private val tabs: ArrayList<String>) : PagerAdapter() {
         private val layoutInflater = LayoutInflater.from(context)
 

+ 38 - 0
reader/src/main/java/com/kdanmobile/reader/screen/view/edit/StampTabView.kt

@@ -1,6 +1,7 @@
 package com.kdanmobile.reader.screen.view.edit
 
 import android.content.Context
+import android.graphics.Rect
 import android.support.constraint.ConstraintLayout
 import android.support.v4.view.PagerAdapter
 import android.support.v7.widget.GridLayoutManager
@@ -10,7 +11,10 @@ import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import com.kdanmobile.kmpdfkit.annotation.stamp.StampConfig.STANDARD_STAMP_RES
+import com.kdanmobile.kmpdfkit.annotation.stamp.TextStampConfig
+import com.kdanmobile.kmpdfkit.annotation.stamp.view.KMPDFStampTextView
 import com.kdanmobile.reader.R
+import com.kdanmobile.reader.screen.data.StampAttribute
 import kotlinx.android.synthetic.main.view_viewer_edit_item_stamp_custom.view.*
 import kotlinx.android.synthetic.main.view_viewer_edit_item_stamp_standard.view.*
 import kotlinx.android.synthetic.main.view_viewer_edit_tab_stamp.view.*
@@ -47,6 +51,40 @@ class StampTabView : ConstraintLayout {
         tabLayout.setupWithViewPager(viewPager)
     }
 
+    fun getStampAttribute() : StampAttribute {
+        return when (viewPager.currentItem == 0) {
+            true -> {
+                val stampType = CustomStampType.values()[selectedCustomStampIndex]
+                //  not work?
+                val rect = Rect(0, 0, 100, 100)
+//                val timeText = "${cb_date.text}${if (cb_date.text.isNotEmpty()) " " else ""}${cb_time.text}"
+                val timeText: String
+                //  not work?
+                val timeType = if (cb_time.isChecked) {
+                    if (cb_date.isChecked) {
+                        timeText = "${cb_date.text} ${cb_time.text}"
+                        KMPDFStampTextView.TimeType.TIME_AND_DATE
+                    } else {
+                        timeText = "${cb_time.text}"
+                        KMPDFStampTextView.TimeType.TIME
+                    }
+                } else {
+                    if (cb_date.isChecked) {
+                        timeText = "${cb_date.text}"
+                        KMPDFStampTextView.TimeType.DATE
+                    } else {
+                        timeText = ""
+                        KMPDFStampTextView.TimeType.NULL
+                    }
+                }
+                StampAttribute(rect = rect, config = TextStampConfig(stampType.lineColor, stampType.backgroundColor, stampType.textColor, et_input.text.toString(), timeText, stampType.shape, timeType))
+            }
+            false -> {
+                StampAttribute(STANDARD_STAMP_RES.values()[selectedStandardStampIndex])
+            }
+        }
+    }
+
     private inner class StampViewPagerAdapter(private val tabs: ArrayList<String>): PagerAdapter() {
         private val layoutInflater = LayoutInflater.from(context)
 

+ 9 - 0
reader/src/main/java/com/kdanmobile/reader/screen/view/edit/TextBoxTabView.kt

@@ -74,4 +74,13 @@ class TextBoxTabView : ConstraintLayout {
             }
         }
     }
+
+    fun getTextBoxAttribute(): TextBoxAttribute {
+        val isBold = textAttrView_textBox.isTextBold
+        val isItalic = textAttrView_textBox.isTextItalic
+        val textAlignment = textAttrView_textBox.alignment
+        val textColor = 0xff000000.toInt()
+        val textSize = seekBar_textBox_textSize.currentValue
+        return TextBoxAttribute(isBold, isItalic, textAlignment, textColor, textSize)
+    }
 }