Pdf添加文字水印设置字体提示找不到

我使用的是
implementation (
group: ‘com.aspose’,
name: ‘aspose-pdf’,
version: ‘20.11’,
classifier: ‘android.via.java’)
代码如下
fun pdfshuiying(){
var fileinput= FileInputStream(path)
var document:Document
try {
document = Document(fileinput)
} catch (e: java.lang.Exception) {
MyLogUtils.testLog(“转换失败!”)
return
}
val textStamp=TextStamp(“测试一下文字”)
textStamp.setBackground(false);
textStamp.setXIndent(200.0);//设置位置
textStamp.setYIndent(300.0);
textStamp.setRotateAngle(50.0);//设置旋转角度
textStamp.getTextState().setFont(FontRepository.findFont(SDCardUtils.getSDCardPathByEnvironment() + “/ysds/font/PMingLiU.ttf”));
textStamp.getTextState().setFontSize(80.0F);//设置字体大小
textStamp.getTextState().setFontStyle(FontStyles.Italic);
textStamp.getTextState().foregroundColor=z1(android.graphics.Color.rgb(255, 0, 0), true)
val docFileName = File(App.APP_DOWN_PATH+“shuiying.pdf”)
if(!docFileName.exists()){
docFileName.mkdirs()
}
for (i in 1…document!!.pages.size()) {
document.pages.get_Item(i).addStamp(textStamp)
}
try {
document.save(docFileName.toString())
} catch (e: java.lang.Exception) {
MyLogUtils.testLog(“转换失败!”)
return
}
MyLogUtils.testLog(“转换成功!”)
}
报错信息
java.lang.IllegalStateException: Font /storage/emulated/0/ysds/font/PMingLiU.ttf was not found
手机中/storage/emulated/0/ysds/font/PMingLiU.ttf路径下的文件是存在的

@zpswz

请确保指定的字体已正确安装并存在于您使用 API 的系统中。此外,您还可以使用以下代码片段设置字体文件夹,以便 API 可以查看该文件夹以找到使用的字体:

com.aspose.pdf.Document.setLocalFontPaths(fontPaths);

通过setLocalFontPaths(fontPaths)可以找到字体,但是出现一个新问题
val textStamp=TextStamp(“测试一下文字”)当这个语句中参数为中文时
document.pages.get_Item(i).addStamp(textStamp)报错
报错如下
class com.aspose.pdf.internal.ms.System.ArgumentException: 测试一下 font was not found

@zpswz

看起来您只指定了一个目录,其中只放置了一种您想在代码中使用的字体。请注意,API 在生成 PDF 时需要所有必要的字体。因此,请将所有字体放在指定目录下或将单个字体文件放在系统原有字体目录下。如果问题仍然存在,请告知我们。

目录下包含了多个字体文件,在word转pdf时设置这个目录可以正常将文字显示出来,这个报错如何知道他是不是缺少字体?

@zpswz

您也愿意分享完整的堆栈跟踪吗?我们需要对其进行详细调查。我们将使用堆栈跟踪信息进行进一步调查。

完整的日志信息如下
2022-07-08 17:52:07.681 19305-19760/com.example.yinleme.ysds E/AndroidRuntime: FATAL EXCEPTION: Thread-30
Process: com.example.yinleme.ysds, PID: 19305
class com.aspose.pdf.internal.ms.System.ArgumentException: 测试文字 font was not found
com.aspose.pdf.internal.p33.z1.m277(Unknown Source:99)
com.aspose.pdf.internal.p33.z1.m1(Unknown Source:17)
com.aspose.pdf.TextSegment.setText(Unknown Source:55)
com.aspose.pdf.TextFragment.setText(Unknown Source:26)
com.aspose.pdf.TextStamp.put(Unknown Source:145)
com.aspose.pdf.Page.addStamp(Unknown Source:8)
com.example.yinleme.zhuanzhuandashi.activity.ui.activity.kt.TestPdfActivity.pdfshuiying(TestPdfActivity.kt:373)
com.example.yinleme.zhuanzhuandashi.activity.ui.activity.kt.TestPdfActivity$onCreate$3$1.run(TestPdfActivity.kt:84)
java.lang.Thread.run(Thread.java:919)
at com.aspose.pdf.internal.p33.z1.m277(Unknown Source:99)
at com.aspose.pdf.internal.p33.z1.m1(Unknown Source:17)
at com.aspose.pdf.TextSegment.setText(Unknown Source:55)
at com.aspose.pdf.TextFragment.setText(Unknown Source:26)
at com.aspose.pdf.TextStamp.put(Unknown Source:145)
at com.aspose.pdf.Page.addStamp(Unknown Source:8)
at com.example.yinleme.zhuanzhuandashi.activity.ui.activity.kt.TestPdfActivity.pdfshuiying(TestPdfActivity.kt:373)
at com.example.yinleme.zhuanzhuandashi.activity.ui.activity.kt.TestPdfActivity$onCreate$3$1.run(TestPdfActivity.kt:84)
at java.lang.Thread.run(Thread.java:919)
2022-07-08 17:52:07.682 565-565/? E/SELinux: avc: denied { find } for service=miui.mqsas.MQSService pid=19305 uid=10197 scontext=u:r:untrusted_app_27:s0:c512,c768 tcontext=u:object_r:mqs_service:s0 tclass=service_manager permissive=0
2022-07-08 17:52:07.682 19305-19760/com.example.yinleme.ysds E/MQSEventManagerDelegate: failed to get MQSService.

@zpswz

我们已在我们的问题跟踪系统中将问题记录为 PDFANDROID-631 以供进一步调查。我们将调查其详细信息,并随时向您发布其更正状态。请耐心等待,给我们一些时间。

对此造成的不便,我们表示歉意。