Browse Source

PDFTool(Android) - 示例新增内容编辑插入文本、图片

ComPDFKit-Youna 9 months ago
parent
commit
ce069d37f0

+ 2 - 1
ComPDFKit_Tools/src/main/AndroidManifest.xml

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
-<manifest xmlns:android="http://schemas.android.com/apk/res/android">
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools">
 
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-permission android:name="android.permission.CAMERA" />

+ 3 - 0
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/pdf/CPDFDocumentActivity.java

@@ -52,6 +52,9 @@ public class CPDFDocumentActivity extends CBasicPDFActivity {
         } else {
             configuration = (CPDFConfiguration) getIntent().getSerializableExtra(CPDFDocumentFragment.EXTRA_CONFIGURATION);
         }
+        if (configuration == null) {
+            configuration = CPDFConfigurationUtils.normalConfig(this, "tools_default_configuration.json");
+        }
         int themeId = CPDFApplyConfigUtil.getInstance().getThemeId(this, configuration);
         setTheme(themeId);
         setContentView(R.layout.tools_pdf_document_activity);

+ 2 - 0
Samples/src/main/java/com/compdfkit/samples/SampleApplication.java

@@ -19,6 +19,7 @@ import com.compdfkit.samples.samples.BatesTest;
 import com.compdfkit.samples.samples.BookmarkTest;
 import com.compdfkit.samples.samples.DigitalSignaturesTest;
 import com.compdfkit.samples.samples.DocumentInfoTest;
+import com.compdfkit.samples.samples.ContentEditorTest;
 import com.compdfkit.samples.samples.EncryptTest;
 import com.compdfkit.samples.samples.FlattenTest;
 import com.compdfkit.samples.samples.HeaderFooterTest;
@@ -72,5 +73,6 @@ public class SampleApplication extends Application {
         samplesList.add(new EncryptTest());
         samplesList.add(new PDFATest());
         samplesList.add(new FlattenTest());
+        samplesList.add(new ContentEditorTest());
     }
 }

+ 200 - 0
Samples/src/main/java/com/compdfkit/samples/samples/ContentEditorTest.java

@@ -0,0 +1,200 @@
+package com.compdfkit.samples.samples;
+
+
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Color;
+import android.graphics.Point;
+import android.graphics.RectF;
+import android.media.ExifInterface;
+
+import com.compdfkit.core.document.CPDFDocument;
+import com.compdfkit.core.edit.CPDFEditCharItem;
+import com.compdfkit.core.edit.CPDFEditImageArea;
+import com.compdfkit.core.edit.CPDFEditPage;
+import com.compdfkit.core.edit.CPDFEditTextArea;
+import com.compdfkit.core.page.CPDFPage;
+import com.compdfkit.samples.PDFSamples;
+import com.compdfkit.samples.R;
+import com.compdfkit.samples.util.FileUtils;
+import com.compdfkit.samples.util.OutputListener;
+import com.compdfkit.ui.reader.CPDFPageView;
+
+import java.io.File;
+
+public class ContentEditorTest extends PDFSamples {
+
+    public ContentEditorTest(){
+        setTitle(R.string.content_editor_title);
+        setDescription(R.string.content_editor_desc);
+    }
+
+    @Override
+    protected void run(OutputListener outputListener) {
+        super.run(outputListener);
+        printHead();
+        // content editor insert text
+        // Create a new document
+        CPDFDocument document = CPDFDocument.createDocument(context);
+        // Samples 1: Insert text on the first page of the document through content editing
+        insertText(document);
+
+        // Samples 2: Insert a picture on the second page of the document through content editing
+        insertImage(document);
+        // Save the generated PDF document
+        File file = new File(outputDir(), "ContentEditor/InsertTextTest.pdf");
+        saveSamplePDF(document, file, true);
+        printFooter();
+    }
+
+
+    private void insertText(CPDFDocument document){
+        // Create a blank page with A4 size (595 x 842)
+        document.insertBlankPage(0, 595, 842);
+        // Get the first page of the document
+        CPDFPage page = document.pageAtIndex(0);
+        // Set the position for adding text (top-left point)
+        Point point = new Point(10, 10);
+        RectF area = new RectF(point.x, point.y, point.x, point.y);
+        RectF size = page.getSize();
+
+        // Convert the text area to the actual position on the page
+        area = page.convertRectToPage(false, size.width(), size.height(), area);
+
+        // Define the width and height of the text area
+        int textAreaWidth = 200;
+        int textAreaHeight = 40;
+
+        int pageWidth = (int) size.width();
+        if (page.getRotation() == CPDFPageView.PageRotateType.PAGE_ROTATE_90.toInt() ||
+                page.getRotation() == CPDFPageView.PageRotateType.PAGE_ROTATE_270.toInt()) {
+            pageWidth = (int) size.height();
+        }
+
+        // Ensure the left start position of the text area does not exceed the page width
+        if (area.left + textAreaWidth > pageWidth) {
+            area.left = pageWidth - textAreaWidth;
+        }
+
+        // Ensure the top position of the text area is not less than 0 and does not exceed the page range
+        if (area.top - textAreaHeight < 0) {
+            area.top = textAreaHeight;
+        }
+
+        // Determine the insertion position of the text area
+        RectF rect = new RectF(area.left, area.top, area.left + textAreaWidth, area.top - textAreaHeight);
+
+        // Get the edit page object
+        CPDFEditPage cpdfEditPage = page.getEditPage(false);
+        cpdfEditPage.beginEdit(CPDFEditPage.LoadTextImage);
+
+        if (cpdfEditPage == null || !cpdfEditPage.isValid()) {
+            outputListener.println("CPDFEditPage is INVALID");
+            return;
+        }
+
+        // Define the font, font size, and text color
+        String fontName = "Arial";
+        int fontSize = 30;
+        int textColor = Color.RED;
+
+        // Create a new text area
+        CPDFEditTextArea editTextArea = cpdfEditPage.createNewTextArea(rect, fontName, fontSize, textColor, 255, false, false, CPDFEditTextArea.PDFEditAlignType.PDFEditAlignLeft);
+
+        // Get the start and end positions for text insertion
+        CPDFEditCharItem begin = editTextArea.getBeginCharPlace();
+        CPDFEditCharItem end = editTextArea.getEndCharPlace();
+
+        // Define the content to be inserted
+        String content = "Hello ComPDFKit";
+
+        outputListener.println("Insert text on the first page: " + content);
+        // Insert the content into the text area
+        editTextArea.insertTextRange(begin.getPlace(), end.getPlace(), content);
+    }
+
+
+    private void insertImage(CPDFDocument document){
+        // Create a blank page with A4 size (595 x 842)
+        document.insertBlankPage(1, 595, 842);
+        // Get the first page of the document
+        CPDFPage page = document.pageAtIndex(1);
+
+        // Get the edit page object
+        CPDFEditPage cpdfEditPage = page.getEditPage(false);
+
+        // Start edit mode
+        cpdfEditPage.beginEdit(CPDFEditPage.LoadImage);
+
+        if (cpdfEditPage == null || !cpdfEditPage.isValid()) {
+            outputListener.println("CPDFEditPage is INVALID");
+            return;
+        }
+
+        Point point = new Point(10, 10);
+        RectF area = new RectF(point.x, point.y, point.x, point.y);
+        RectF size = page.getSize();
+
+        int height = 200;
+        int width = 200;
+        // Convert the text area to the actual position on the page
+        area = page.convertRectToPage(false, size.width(), size.height(), area);
+
+        String imagePath = FileUtils.getAssetsTempFile(context, "ComPDFKit.png");
+
+        BitmapFactory.Options options = new BitmapFactory.Options();
+        options.inJustDecodeBounds = true;
+        BitmapFactory.decodeFile(imagePath, options);
+        int bitmapwidth = options.outWidth;
+        int bitmapheight = options.outHeight;
+
+        float ratio = bitmapwidth * 1.0f / bitmapheight;
+        if (ratio < 1) {
+            height = (int)(width / ratio);
+        } else {
+            width = (int)(height * ratio);
+        }
+
+        int pagewidth = (int)size.width();
+        int pageheight = (int)size.height();
+        if (page.getRotation() == CPDFPageView.PageRotateType.PAGE_ROTATE_90.toInt() || page.getRotation() == CPDFPageView.PageRotateType.PAGE_ROTATE_270.toInt()) {
+            pagewidth = (int)size.height();
+            pageheight = (int)size.width();
+        }
+
+        if (width > pagewidth) {
+            width = pagewidth;
+            height = (int)(width / ratio);
+            area.left = 0;
+            if (area.top - height < 0) {
+                area.top = height;
+            }
+        } else if (height > pageheight) {
+            height = pageheight;
+            width = (int)(height * ratio);
+            area.top = pageheight;
+            if (area.left + width > pagewidth) {
+                area.left = pagewidth - width;
+            }
+        } else {
+            if (area.left + width > pagewidth) {
+                area.left = pagewidth - width;
+            }
+            if (area.top - height < 0) {
+                area.top = height;
+            }
+        }
+
+        RectF rect = new RectF(area.left, area.top, area.left + width, area.top - height);
+        outputListener.println("Insert image on the second page: assets/ComPDFKit.png" );
+        try{
+            CPDFEditImageArea editImageArea = cpdfEditPage.createNewImageArea(rect, imagePath, null);
+            if (editImageArea == null || !editImageArea.isValid()){
+                outputListener.println("Failed to insert picture!!!");
+            }
+            cpdfEditPage.endEdit();
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+    }
+}

+ 4 - 0
Samples/src/main/res/values/strings.xml

@@ -65,6 +65,10 @@
     <string name="digital_signature_title">DigitalSignature</string>
     <string name="digital_signature_desc">This sample demonstrates the creation of digital certificates, the generation of digital signatures, the verification of digital certificates, the validation of digital signatures, the reading of digital signature information, certificate trust, and signature removal functionality.</string>
 
+    <string name="content_editor_title">ContentEditor</string>
+    <string name="content_editor_desc">This example shows how to insert text and images through the content editing function.</string>
+
+
     <string name="tools_allowed">Allowed</string>
     <string name="tools_not_allowed">Not Allowed</string>
 

+ 2 - 0
Samples_kotlin/src/main/java/com/compdfkit/samples/SampleApplication.kt

@@ -14,6 +14,7 @@ import com.compdfkit.samples.samples.AnnotationTest
 import com.compdfkit.samples.samples.BackgroundTest
 import com.compdfkit.samples.samples.BatesTest
 import com.compdfkit.samples.samples.BookmarkTest
+import com.compdfkit.samples.samples.ContentEditorTest
 import com.compdfkit.samples.samples.DigitalSignaturesTest
 import com.compdfkit.samples.samples.DocumentInfoTest
 import com.compdfkit.samples.samples.EncryptTest
@@ -59,6 +60,7 @@ class SampleApplication : Application() {
         samplesList.add(EncryptTest())
         samplesList.add(PDFATest())
         samplesList.add(FlattenTest())
+        samplesList.add(ContentEditorTest())
     }
 
     companion object {

+ 192 - 0
Samples_kotlin/src/main/java/com/compdfkit/samples/samples/ContentEditorTest.kt

@@ -0,0 +1,192 @@
+package com.compdfkit.samples.samples
+
+import android.graphics.BitmapFactory
+import android.graphics.Color
+import android.graphics.Point
+import android.graphics.RectF
+import com.compdfkit.core.document.CPDFDocument
+import com.compdfkit.core.edit.CPDFEditPage
+import com.compdfkit.core.edit.CPDFEditTextArea
+import com.compdfkit.samples.PDFSamples
+import com.compdfkit.samples.R
+import com.compdfkit.samples.util.FileUtils
+import com.compdfkit.samples.util.OutputListener
+import com.compdfkit.ui.reader.CPDFPageView
+import java.io.File
+
+
+class ContentEditorTest : PDFSamples() {
+
+    init {
+        setTitle(R.string.content_editor_title)
+        setDescription(R.string.content_editor_desc)
+    }
+
+    override fun run(outputListener: OutputListener?) {
+        super.run(outputListener)
+        printHead()
+        // content editor insert text
+        // Create a new document
+        val document = CPDFDocument.createDocument(context())
+        // Samples 1: Insert text on the first page of the document through content editing
+        insertText(document)
+
+        // Samples 2: Insert a picture on the second page of the document through content editing
+        insertImage(document)
+        // Save the generated PDF document
+        val file = File(outputDir(), "ContentEditor/Insert_Text_Image_Test.pdf")
+        saveSamplePDF(document, file, true)
+        printFooter()
+    }
+
+
+    private fun insertText(document: CPDFDocument) {
+        // Create a blank page with A4 size (595 x 842)
+        document.insertBlankPage(0, 595f, 842f)
+        // Get the first page of the document
+        val page = document.pageAtIndex(0)
+        val size = page.size
+
+        // Convert the text area to the actual position on the page
+        var area = run {
+            // Set the position for adding text (top-left point)
+            val point = Point(10, 10)
+            val pointArea = RectF(point.x.toFloat(), point.y.toFloat(), point.x.toFloat(), point.y.toFloat())
+            page.convertRectToPage(false, size.width(), size.height(), pointArea)
+        }
+
+        // Define the width and height of the text area
+        val textAreaWidth = 200
+        val textAreaHeight = 40
+        var pageWidth = size.width().toInt()
+        if (page.rotation == CPDFPageView.PageRotateType.PAGE_ROTATE_90.toInt() ||
+            page.rotation == CPDFPageView.PageRotateType.PAGE_ROTATE_270.toInt()
+        ) {
+            pageWidth = size.height().toInt()
+        }
+
+        // Ensure the left start position of the text area does not exceed the page width
+        if (area.left + textAreaWidth > pageWidth) {
+            area.left = (pageWidth - textAreaWidth).toFloat()
+        }
+
+        // Ensure the top position of the text area is not less than 0 and does not exceed the page range
+        if (area.top - textAreaHeight < 0) {
+            area.top = textAreaHeight.toFloat()
+        }
+
+        // Determine the insertion position of the text area
+        val rect = RectF(area.left, area.top, area.left + textAreaWidth, area.top - textAreaHeight)
+
+        // Get the edit page object
+        val cpdfEditPage = page.getEditPage(false)
+        cpdfEditPage.beginEdit(CPDFEditPage.LoadTextImage)
+        if (cpdfEditPage == null || !cpdfEditPage.isValid) {
+            outputListener?.println("CPDFEditPage is INVALID")
+            return
+        }
+
+        // Define the font, font size, and text color
+        val fontName = "Arial"
+        val fontSize = 30
+        val textColor = Color.RED
+
+        // Create a new text area
+        val editTextArea = cpdfEditPage.createNewTextArea(
+            rect,
+            fontName,
+            fontSize.toFloat(),
+            textColor,
+            255,
+            false,
+            false,
+            CPDFEditTextArea.PDFEditAlignType.PDFEditAlignLeft
+        )
+
+        // Get the start and end positions for text insertion
+        val begin = editTextArea.beginCharPlace
+        val end = editTextArea.endCharPlace
+
+        // Define the content to be inserted
+        val content = "Hello ComPDFKit"
+        outputListener?.println("Insert text on the first page: $content")
+        // Insert the content into the text area
+        editTextArea.insertTextRange(begin.getPlace(), end.getPlace(), content)
+    }
+
+
+    private fun insertImage(document: CPDFDocument) {
+        // Create a blank page with A4 size (595 x 842)
+        document.insertBlankPage(1, 595F, 842F)
+        // Get the first page of the document
+        val page = document.pageAtIndex(1)
+
+        // Get the edit page object
+        val cpdfEditPage = page.getEditPage(false)
+
+        // Start edit mode
+        cpdfEditPage.beginEdit(CPDFEditPage.LoadImage)
+        if (cpdfEditPage == null || !cpdfEditPage.isValid) {
+            outputListener?.println("CPDFEditPage is INVALID")
+            return
+        }
+        val point = Point(10, 10)
+        var area = RectF(point.x.toFloat(), point.y.toFloat(), point.x.toFloat(), point.y.toFloat())
+        val size = page.size
+        var height = 200
+        var width = 200
+        // Convert the text area to the actual position on the page
+        area = page.convertRectToPage(false, size.width(), size.height(), area)
+        val imagePath = FileUtils.getAssetsTempFile(context(), "ComPDFKit.png")
+        val options = BitmapFactory.Options()
+        options.inJustDecodeBounds = true
+        BitmapFactory.decodeFile(imagePath, options)
+        val bitmapWidth = options.outWidth
+        val bitmapHeight = options.outHeight
+        val ratio = bitmapWidth * 1.0F / bitmapHeight
+        if (ratio < 1) {
+            height = (width / ratio).toInt()
+        } else {
+            width = (height * ratio).toInt()
+        }
+        var pagewidth = size.width().toInt()
+        var pageheight = size.height().toInt()
+        if (page.rotation == CPDFPageView.PageRotateType.PAGE_ROTATE_90.toInt() || page.rotation == CPDFPageView.PageRotateType.PAGE_ROTATE_270.toInt()) {
+            pagewidth = size.height().toInt()
+            pageheight = size.width().toInt()
+        }
+        if (width > pagewidth) {
+            width = pagewidth
+            height = (width / ratio).toInt()
+            area.left = 0f
+            if (area.top - height < 0) {
+                area.top = height.toFloat()
+            }
+        } else if (height > pageheight) {
+            height = pageheight
+            width = (height * ratio).toInt()
+            area.top = pageheight.toFloat()
+            if (area.left + width > pagewidth) {
+                area.left = (pagewidth - width).toFloat()
+            }
+        } else {
+            if (area.left + width > pagewidth) {
+                area.left = (pagewidth - width).toFloat()
+            }
+            if (area.top - height < 0) {
+                area.top = height.toFloat()
+            }
+        }
+        val rect = RectF(area.left, area.top, area.left + width, area.top - height)
+        outputListener?.println("Insert image on the second page: assets/ComPDFKit.png")
+        try {
+            val editImageArea = cpdfEditPage.createNewImageArea(rect, imagePath, null)
+            if (editImageArea == null || !editImageArea.isValid) {
+                outputListener?.println("Failed to insert picture!!!")
+            }
+            cpdfEditPage.endEdit()
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+    }
+}

+ 3 - 0
Samples_kotlin/src/main/res/values/strings.xml

@@ -65,6 +65,9 @@
     <string name="digital_signature_title">DigitalSignature</string>
     <string name="digital_signature_desc">This sample demonstrates the creation of digital certificates, the generation of digital signatures, the verification of digital certificates, the validation of digital signatures, the reading of digital signature information, certificate trust, and signature removal functionality.</string>
 
+    <string name="content_editor_title">ContentEditor</string>
+    <string name="content_editor_desc">This example shows how to insert text and images through the content editing function.</string>
+
     <string name="tools_allowed">Allowed</string>
     <string name="tools_not_allowed">Not Allowed</string>