【问题标题】:Android - How can I access the properties of a PDF page?Android - 如何访问 PDF 页面的属性?
【发布时间】:2020-12-10 04:11:06
【问题描述】:

我需要访问我的打印机应用程序的 PDF 页面的属性。

这些属性是;

  1. 页面大小和宽度
  2. 页边距(请看照片)

如何以编程方式获取这些属性?

注意:我没有放任何代码示例,因为我现在只有一个“文件”类型的 PDF 文件。

【问题讨论】:

  • 边距不是 PDF 概念。不能从元数据或类似的东西中查询它。如果您真的需要知道这一点,唯一的解决方案是查看页面上的文本(和或其他元素),并找出最接近页面顶部的文本。请记住,可能还有页面内容在页面之外,您可能不想考虑到这一点。

标签: java android file kotlin pdf


【解决方案1】:

根据文档 (https://developer.android.com/training/data-storage/shared/documents-files#kotlin),当您拥有文档的 URI 时,您可以访问其元数据。这个 sn-p 抓取由 URI 指定的文档的元数据,并记录它

val contentResolver = applicationContext.contentResolver

fun dumpImageMetaData(uri: Uri) {

    // The query, because it only applies to a single document, returns only
    // one row. There's no need to filter, sort, or select fields,
    // because we want all fields for one document.
    val cursor: Cursor? = contentResolver.query(
            uri, null, null, null, null, null)

    cursor?.use {
        // moveToFirst() returns false if the cursor has 0 rows. Very handy for
        // "if there's anything to look at, look at it" conditionals.
        if (it.moveToFirst()) {

            // Note it's called "Display Name". This is
            // provider-specific, and might not necessarily be the file name.
            val displayName: String =
                    it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
            Log.i(TAG, "Display Name: $displayName")

            val sizeIndex: Int = it.getColumnIndex(OpenableColumns.SIZE)
            // If the size is unknown, the value stored is null. But because an
            // int can't be null, the behavior is implementation-specific,
            // and unpredictable. So as
            // a rule, check if it's null before assigning to an int. This will
            // happen often: The storage API allows for remote files, whose
            // size might not be locally known.
            val size: String = if (!it.isNull(sizeIndex)) {
                // Technically the column stores an int, but cursor.getString()
                // will do the conversion automatically.
                it.getString(sizeIndex)
            } else {
                "Unknown"
            }
            Log.i(TAG, "Size: $size")
        }
    }
}

【讨论】:

  • 提供有关页面大小的信息。但它并没有给出最高利润。我需要这个来知道从哪里开始打印。
猜你喜欢
  • 1970-01-01
  • 2019-12-23
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 2011-01-10
  • 2017-04-22
相关资源
最近更新 更多