Skip to content

Open-source Android PDF and document toolkit with PDF tools, Office viewing, XLSX editing, scanning, and image editing. Built with Kotlin and Jetpack Compose.

Share: export the edited document as PDF, page images (PNG) or text

chethan616e6c7a8e8b829
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerDialogs.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerDialogs.kt
index 5a2301b..a135362 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerDialogs.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerDialogs.kt
@@ -11,6 +11,8 @@ import com.chethan616.clearpdf.ui.components.GlassDropdownOption
import com.chethan616.clearpdf.ui.components.LiquidGlassDropdown
import androidx.compose.material.icons.rounded.Description
import androidx.compose.material.icons.rounded.PictureAsPdf
+import androidx.compose.material.icons.rounded.Image
+import androidx.compose.material.icons.rounded.TextSnippet
import com.chethan616.clearpdf.ui.components.GlassDialogField
import com.chethan616.clearpdf.ui.components.GlassDialogAction
import com.chethan616.clearpdf.ui.components.GlassDialog
@@ -355,7 +357,7 @@ internal fun TextEditDialog(
}
/** Which file the viewer's Share action should hand off. */
-internal enum class ShareFormat { ORIGINAL, PDF }
+internal enum class ShareFormat { ORIGINAL, PDF, IMAGES, TEXT }
/**
* Share/export chooser on the shared [GlassDialog] card. For a converted document (a .docx opened as
@@ -376,15 +378,15 @@ internal fun ExportShareDialog(
onDismiss: () -> Unit,
onShare: (format: ShareFormat, encrypt: Boolean, password: String) -> Unit
) {
+ val pdfOnly = originalExt == null
// Keyed on `visible` so every open starts fresh.
- var formatIndex by remember(visible) { mutableStateOf(0) }
+ var format by remember(visible) { mutableStateOf(if (pdfOnly) ShareFormat.PDF else ShareFormat.ORIGINAL) }
var encrypt by remember(visible) { mutableStateOf(false) }
var password by remember(visible) { mutableStateOf("") }
val passwordFocus = remember { FocusRequester() }
val soft = glassDialogInkSoft()
- val pdfOnly = originalExt == null
- val pdfSelected = pdfOnly || formatIndex == 1
+ val pdfSelected = format == ShareFormat.PDF
val canShare = !(pdfSelected && encrypt && password.isBlank())
LaunchedEffect(encrypt, pdfSelected) {
@@ -401,11 +403,7 @@ internal fun ExportShareDialog(
GlassDialogAction(
stringResource(R.string.viewer_share_button),
{
- if (canShare) onShare(
- if (pdfSelected) ShareFormat.PDF else ShareFormat.ORIGINAL,
- encrypt && pdfSelected,
- password
- )
+ if (canShare) onShare(format, encrypt && pdfSelected, password)
},
primary = true,
enabled = canShare
@@ -413,25 +411,28 @@ internal fun ExportShareDialog(
}
) {
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
- if (!pdfOnly) {
- BasicText(
- stringResource(R.string.viewer_share_format),
- style = TextStyle(soft, 13.sp, FontWeight.Medium)
- )
- // The share sheet's original bouncy glass dropdown (v3), back on the shared component.
- LiquidGlassDropdown(
- options = listOf(
- GlassDropdownOption(0, originalExt.orEmpty(), icon = Icons.Rounded.Description),
- GlassDropdownOption(1, stringResource(R.string.viewer_share_pdf), icon = Icons.Rounded.PictureAsPdf)
- ),
- selected = formatIndex,
- onSelect = { formatIndex = it },
- backdrop = backdrop,
- leadingIcon = if (formatIndex == 1) Icons.Rounded.PictureAsPdf else Icons.Rounded.Description,
- triggerSurface = glassDialogPlatter(LocalIsDarkMode.current),
- modifier = Modifier.fillMaxWidth()
- )
+ BasicText(
+ stringResource(R.string.viewer_share_format),
+ style = TextStyle(soft, 13.sp, FontWeight.Medium)
+ )
+ // The share sheet's original bouncy glass dropdown (v3), back on the shared component.
+ // Edits travel with every format: PDF, page images and text are all made from the
+ // edited document.
+ val options = buildList {
+ if (!pdfOnly) add(GlassDropdownOption(ShareFormat.ORIGINAL, originalExt.orEmpty(), icon = Icons.Rounded.Description))
+ add(GlassDropdownOption(ShareFormat.PDF, stringResource(R.string.viewer_share_pdf), icon = Icons.Rounded.PictureAsPdf))
+ add(GlassDropdownOption(ShareFormat.IMAGES, stringResource(R.string.viewer_share_images), icon = Icons.Rounded.Image))
+ add(GlassDropdownOption(ShareFormat.TEXT, stringResource(R.string.viewer_share_text), icon = Icons.Rounded.TextSnippet))
}
+ LiquidGlassDropdown(
+ options = options,
+ selected = format,
+ onSelect = { format = it },
+ backdrop = backdrop,
+ leadingIcon = options.firstOrNull { it.value == format }?.icon,
+ triggerSurface = glassDialogPlatter(LocalIsDarkMode.current),
+ modifier = Modifier.fillMaxWidth()
+ )
AnimatedVisibility(
visible = pdfSelected,
enter = fadeIn(tween(160)) + expandVertically(GlassMotion.settle()),
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
index ea66488..8f78fc9 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/screen/PdfViewerScreen.kt
@@ -245,6 +245,8 @@ fun PdfViewerScreen(
var editingAnnoId by remember { mutableStateOf<Long?>(null) }
// In-place edit of a line of the page's own text (Text tool tap on existing text).
var textEditTarget by remember { mutableStateOf<TextEditTarget?>(null) }
+ // Page images handed to the share sheet together (ACTION_SEND_MULTIPLE).
+ var shareMultiple: List<android.net.Uri> by remember { mutableStateOf(emptyList()) }
var editingAnnoPage by remember { mutableStateOf(0) }
var editingAnnoIsNote by remember { mutableStateOf(false) }
var annotationDraft by remember { mutableStateOf("") }
@@ -2046,7 +2048,38 @@ fun PdfViewerScreen(
)
else u
val shareDir = java.io.File(context.cacheDir, "shared").apply { mkdirs() }
+ // Unsaved edits travel with the share: build an edited copy first and
+ // make every PDF-derived format (PDF, images, text) from it.
+ val edited: android.net.Uri? = if (hasUnsavedEdits && format != ShareFormat.ORIGINAL) {
+ val overlays = buildExportOverlays(annotationsByPage, state.ocrBlocksByPage, pageCanvasSizes, pageBitmapSizes)
+ val name = state.fileName.substringBeforeLast('.').ifBlank { "document" }
+ .replace(Regex("[^A-Za-z0-9._-]"), "_")
+ viewModel.writeEditedCopy(context, overlays, java.io.File(shareDir, "${name}_edited.pdf"))
+ } else null
+ val sourcePdf = edited ?: state.document?.uri
when (format) {
+ ShareFormat.IMAGES -> sourcePdf?.let { pdf ->
+ val stem = state.fileName.substringBeforeLast('.').ifBlank { "page" }
+ .replace(Regex("[^A-Za-z0-9._-]"), "_")
+ val count = state.pageCount.coerceAtLeast(1)
+ val uris = ArrayList<android.net.Uri>()
+ for (p in 0 until count) {
+ val bmp = com.kyant.pdfcore.raster.PdfRasterizer.rasterizePageBitmap(context, pdf, p) ?: continue
+ val f = java.io.File(shareDir, "${stem}_${p + 1}.png")
+ f.outputStream().use { bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, it) }
+ bmp.recycle()
+ uris += androidx.core.content.FileProvider.getUriForFile(context, "${context.packageName}.provider", f)
+ }
+ shareMultiple = uris
+ uris.firstOrNull()?.let { it to "image/png" }
+ }
+ ShareFormat.TEXT -> sourcePdf?.let { pdf ->
+ val stem = state.fileName.substringBeforeLast('.').ifBlank { "document" }
+ .replace(Regex("[^A-Za-z0-9._-]"), "_")
+ val f = java.io.File(shareDir, "$stem.txt")
+ f.writeText(viewModel.extractAllText(context, pdf))
+ androidx.core.content.FileProvider.getUriForFile(context, "${context.packageName}.provider", f) to "text/plain"
+ }
ShareFormat.ORIGINAL -> state.originalUri?.let { orig ->
// Mirror the original into our own storage so the target app can
// actually read it — a SAF uri from another provider can't be
@@ -2061,7 +2094,7 @@ fun PdfViewerScreen(
context, "${context.packageName}.provider", out
) to (context.contentResolver.getType(orig) ?: "application/octet-stream")
}
- ShareFormat.PDF -> state.document?.uri?.let { pdf ->
+ ShareFormat.PDF -> sourcePdf?.let { pdf ->
if (encrypt && password.isNotBlank()) {
val out = java.io.File(shareDir, "protected_${System.currentTimeMillis()}.pdf")
val outUri = androidx.core.content.FileProvider.getUriForFile(
@@ -2078,7 +2111,15 @@ fun PdfViewerScreen(
}
if (payload != null) {
val (shareUri, mime) = payload
- val send = android.content.Intent(android.content.Intent.ACTION_SEND).apply {
+ val many = shareMultiple
+ shareMultiple = emptyList()
+ val send = if (many.size > 1) {
+ android.content.Intent(android.content.Intent.ACTION_SEND_MULTIPLE).apply {
+ type = mime
+ putParcelableArrayListExtra(android.content.Intent.EXTRA_STREAM, ArrayList(many))
+ addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ }
+ } else android.content.Intent(android.content.Intent.ACTION_SEND).apply {
type = mime
putExtra(android.content.Intent.EXTRA_STREAM, shareUri)
addFlags(android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION)
diff --git a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
index 80e7fc2..d42612d 100644
--- a/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
+++ b/app/src/main/java/com/chethan616/clearpdf/ui/viewmodel/PdfViewerViewModel.kt
@@ -750,6 +750,27 @@ class PdfViewerViewModel(private val openPdfUseCase: OpenPdfUseCase) : ViewModel
}
}
+ /**
+ * Writes the open document WITH all edits applied (text edits, drawings, images…) to [out] —
+ * the same pipeline as Save — so Share hands over what the user sees, not the untouched file.
+ * Blocking: call off the main thread.
+ */
+ fun writeEditedCopy(context: Context, overlaysByPage: Map<Int, List<ExportOverlay>>, out: File): Uri? {
+ val doc = _uiState.value.document ?: return null
+ val uri = FileProvider.getUriForFile(context, "${context.packageName}.provider", out)
+ return runCatching { exportWithPdfBox(context, doc, overlaysByPage, uri); uri }.getOrNull()
+ }
+
+ /** Plain text of every page of [pdf] in reading order. Blocking. */
+ fun extractAllText(context: Context, pdf: Uri): String {
+ com.kyant.pdfcore.internal.PdfBox.ensureInitialized(context)
+ return context.contentResolver.openInputStream(pdf)?.use { input ->
+ com.tom_roush.pdfbox.pdmodel.PDDocument.load(input).use { d ->
+ com.tom_roush.pdfbox.text.PDFTextStripper().apply { sortByPosition = true }.getText(d)
+ }
+ }.orEmpty()
+ }
+
private fun exportWithPdfBox(
context: Context,
doc: PdfDocument,
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index bfe1d4d..a291e4d 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -274,6 +274,8 @@
<string name="viewer_share_password_hint">Password</string>
<string name="viewer_share_button">Share</string>
<string name="viewer_share_pdf">PDF</string>
+ <string name="viewer_share_images">Images (PNG)</string>
+ <string name="viewer_share_text">Text (.txt)</string>
<string name="viewer_share_normal">Normal</string>
<string name="viewer_share_encrypted">Encrypted</string>