- Code: Select all
public void replaceText(PdfDocument document, Map<String, String> replaceMap) {
PdfPageCollection pages = document.getPages();
replaceMap.forEach((key, value) -> {
for (int i = 0; i < pages.getCount(); i++) {
PdfPageBase page = pages.get(i);
PdfTextFindCollection textFindCollection = page.findText(key, true, false);
PdfTextFind[] finds = textFindCollection.getFinds();
if (finds.length > 0) {
try {
finds[0].applyRecoverString(value);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
});
}
public static void main(String[] args) throws IOException {
spirePdfUtils.replaceText(pdfDocument, Map.of("grade", "222"));
}
报错有两个:
第一:
Exception in thread "main" com.spire.office.packages.sprwala: Neutral cultures cannot be used in formatting and parsing and therefore cannot be set as the thread's current culture
at com.spire.office.packages.sprozja.spr©┦—(trf:234)
at com.spire.pdf.PdfPageBase.findText(Unknown Source)
at com.spire.pdf.PdfPageBase.findText(Unknown Source)
第二:
2025-07-15 03:02:56 |ERROR |https-jsse-nio-8089-exec-10 |DirectJDKLog.java:175 |org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].[dispatcherServlet] |Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: java.lang.RuntimeException: class com.spire.office.packages.sprdraa: Fail to save. Saving of requested font type is not supported.
com.spire.office.packages.sprtlaa.spr▓⌮—(Unknown Source)
com.spire.pdf.graphics.PdfTrueTypeFont.<init>(Unknown Source)
com.spire.pdf.graphics.PdfTrueTypeFont.<init>(Unknown Source)
com.spire.pdf.graphics.PdfTrueTypeFont.<init>(Unknown Source)
com.spire.pdf.graphics.PdfTrueTypeFont.<init>(Unknown Source)
com.spire.pdf.general.find.PdfTextFind.spr┊┍—(Unknown Source)
com.spire.pdf.general.find.PdfTextFind.applyRecoverString(Unknown Source)
如何才能解决?