如何解决Android-如何访问PDF页面的属性?
我需要访问打印机应用程序的PDF页面属性。
这些属性是;
- 页面大小和宽度
- 页边距(请参阅照片)
如何以编程方式获取这些属性?
注意:我现在没有任何代码示例,因为我现在只有一个“文件”类型的PDF文件。
解决方法
根据文档(https://developer.android.com/training/data-storage/shared/documents-files#kotlin),当您拥有文档的URI时,就可以访问其元数据。此代码段获取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)
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")
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。