Selaa lähdekoodia

PDFTool(Android) - 修改flattened 保存名称

liuxiaolong 10 kuukautta sitten
vanhempi
commit
c81639e95e

+ 24 - 4
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/basic/fragment/CBasicPDFFragment.java

@@ -16,6 +16,7 @@ import android.os.Environment;
 import android.text.TextUtils;
 
 import androidx.core.content.ContextCompat;
+import androidx.fragment.app.FragmentActivity;
 
 import com.compdfkit.core.annotation.CPDFBorderStyle;
 import com.compdfkit.core.annotation.CPDFLineAnnotation;
@@ -39,6 +40,7 @@ import com.compdfkit.tools.common.contextmenu.impl.CSearchReplaceContextMenuView
 import com.compdfkit.tools.common.contextmenu.impl.CSignatureContextMenuView;
 import com.compdfkit.tools.common.utils.CFileUtils;
 import com.compdfkit.tools.common.utils.CPermissionUtil;
+import com.compdfkit.tools.common.utils.dialog.CLoadingDialog;
 import com.compdfkit.tools.common.views.pdfproperties.pdfstyle.CStyleType;
 import com.compdfkit.tools.common.views.pdfproperties.pdfstyle.manager.CStyleManager;
 import com.compdfkit.tools.common.views.pdfview.CPDFViewCtrl;
@@ -108,7 +110,7 @@ public class CBasicPDFFragment extends CPermissionFragment {
                 .setShape(CStyleType.ANNOT_LINE, defaultColor, 255, defaultColor, 255, 5, null)
                 .setShapeArrow(defaultColor, 255, defaultColor, 255, 5,
                         CPDFLineAnnotation.LineType.LINETYPE_NONE, CPDFLineAnnotation.LineType.LINETYPE_ARROW,
-                        new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid,5,new float[]{8.0F, 0F}))
+                        new CPDFBorderStyle(CPDFBorderStyle.Style.Border_Solid, 5, new float[]{8.0F, 0F}))
                 .setInkAttribute(defaultColor, 255, 10, 10)
                 .setFreeText(defaultColor, 255, 50)
                 .init(pdfView, true);
@@ -160,7 +162,7 @@ public class CBasicPDFFragment extends CPermissionFragment {
     protected void sharePDF(CPDFViewCtrl pdfView) {
         pdfView.exitEditMode();
         CPDFDocument document = pdfView.getCPdfReaderView().getPDFDocument();
-        if (document == null){
+        if (document == null) {
             return;
         }
         boolean isExternalFile = !TextUtils.isEmpty(document.getAbsolutePath()) &&
@@ -178,8 +180,8 @@ public class CBasicPDFFragment extends CPermissionFragment {
                 CFileUtils.shareFile(getContext(), getString(R.string.tools_share_to), "application/pdf", pdfUri);
             }
         }, e -> {
-            if (e instanceof CPDFDocumentException){
-                if (!document.isCanWrite() && document.hasRepaired()){
+            if (e instanceof CPDFDocumentException) {
+                if (!document.isCanWrite() && document.hasRepaired()) {
                     pdfView.showWritePermissionsDialog(document);
                 }
             }
@@ -195,4 +197,22 @@ public class CBasicPDFFragment extends CPermissionFragment {
         pageEditDialogFragment.setOnBackListener(backListener);
         pageEditDialogFragment.show(getChildFragmentManager(), "pageEditDialogFragment");
     }
+
+    private CLoadingDialog loadingDialog;
+
+    protected void showLoadingDialog() {
+        if (loadingDialog != null && loadingDialog.isVisible()) {
+            loadingDialog.dismiss();
+        }
+        if (getContext() instanceof FragmentActivity) {
+            loadingDialog = CLoadingDialog.newInstance();
+            loadingDialog.show(((FragmentActivity) getContext()).getSupportFragmentManager(), "loadingDialog");
+        }
+    }
+
+    protected void dismissLoadingDialog() {
+        if (loadingDialog != null) {
+            loadingDialog.dismiss();
+        }
+    }
 }

+ 29 - 19
ComPDFKit_Tools/src/main/java/com/compdfkit/tools/common/pdf/CPDFDocumentFragment.java

@@ -647,28 +647,38 @@ public class CPDFDocumentFragment extends CBasicPDFFragment {
                 CToastUtil.showLongToast(getContext(), R.string.tools_save_failed);
                 return;
             }
-            File file = new File(dir, document.getFileName());
+            showLoadingDialog();
+            File file = new File(dir, CFileUtils.getFileNameNoExtension(document.getFileName()) + getString(R.string.tools_flattened_suffix));
             file = CFileUtils.renameNameSuffix(file);
-            boolean result = document.flattenAllPages(CPDFPage.PDFFlattenOption.FLAT_NORMALDISPLAY);
-            if (result) {
-                try {
-                    boolean saveResult = document.saveAs(file.getAbsolutePath(), false);
-                    if (saveResult) {
-                        CToastUtil.showLongToast(getContext(), R.string.tools_save_success);
-                        Intent intent = new Intent(getContext(), CPDFDocumentActivity.class);
-                        intent.putExtra(CPDFDocumentFragment.EXTRA_FILE_PATH, file.getAbsolutePath());
-                        intent.putExtra(CPDFDocumentFragment.EXTRA_FILE_PASSWORD, document.getPassword());
-                        intent.putExtra(CPDFDocumentFragment.EXTRA_CONFIGURATION, cpdfConfiguration);
-                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-                        getContext().startActivity(intent);
-                        if (getActivity() != null) {
-                            getActivity().finish();
-                        }
+            File finalFile = file;
+            CThreadPoolUtils.getInstance().executeIO(() ->{
+                boolean result = document.flattenAllPages(CPDFPage.PDFFlattenOption.FLAT_NORMALDISPLAY);
+                if (result) {
+                    try {
+                        boolean saveResult = document.saveAs(finalFile.getAbsolutePath(), false);
+                        CThreadPoolUtils.getInstance().executeMain(()->{
+                            dismissLoadingDialog();
+                            if (saveResult) {
+                                CToastUtil.showLongToast(getContext(), R.string.tools_save_success);
+                                Intent intent = new Intent(getContext(), CPDFDocumentActivity.class);
+                                intent.putExtra(CPDFDocumentFragment.EXTRA_FILE_PATH, finalFile.getAbsolutePath());
+                                intent.putExtra(CPDFDocumentFragment.EXTRA_FILE_PASSWORD, document.getPassword());
+                                intent.putExtra(CPDFDocumentFragment.EXTRA_CONFIGURATION, cpdfConfiguration);
+                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                                getContext().startActivity(intent);
+                                if (getActivity() != null) {
+                                    getActivity().finish();
+                                }
+                            }
+                        });
+                    } catch (Exception e) {
+                        e.printStackTrace();
                     }
-                } catch (Exception e) {
-                    e.printStackTrace();
+                }else {
+                    dismissLoadingDialog();
                 }
-            }
+            });
+
         });
         directoryDialog.show(getChildFragmentManager(), "dirDialog");
     }

+ 2 - 0
ComPDFKit_Tools/src/main/res/values-zh-rCN/tools_strings.xml

@@ -481,5 +481,7 @@
     <string name="tools_setting">设置</string>
     <string name="tools_effective_immediately_after_setting">设置后立即生效</string>
     <string name="tools_loading">加载中…</string>
+    <string name="tools_flattened_suffix">_扁平化文档.pdf</string>
+
 
 </resources>

+ 1 - 0
ComPDFKit_Tools/src/main/res/values/tools_strings.xml

@@ -487,4 +487,5 @@
     <string name="tools_setting">Setting</string>
     <string name="tools_effective_immediately_after_setting">Effective immediately after setting</string>
     <string name="tools_loading">Loading…</string>
+    <string name="tools_flattened_suffix">_Flattened.pdf</string>
 </resources>