Aspose.Words - 使用Java设置字体替代以生成PDF文件
{
"title": "Aspose.Words - 使用Java设置字体替代以生成PDF文件",
"description": "本文档介绍了如何在使用 Aspose.Words 将 Word 文档转换为 PDF 文件时,使用 Java 代码设置字体替代以确保生成的 PDF 文件中字体正确显示。",
"keywords": "Aspose.Words, Java, PDF, 字体替代, FontSettings, 文件转换",
"content": ""使用 Aspose.Words 将 Word 文档转换为 PDF 文件时,有时可能会遇到字体替换问题。这通常是因为在目标系统中缺少必需的字体。为了解决这个问题,可以使用 FontSettings 类来设置字体替代,确保生成的 PDF 文件中字体正确显示。\n\n以下是用 Java 代码设置字体替代的示例:\n\njava\n// 初始化字体设置\nFontSettings fontSettings = new FontSettings();\nFontSourceBase[] fontSources = new FontSourceBase[] {\n // 添加字体文件的路径\n new FolderFontSource("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\fonts")\n};\nfontSettings.setFontsSources(fontSources);\n\n// 应用字体设置\ncom.aspose.words.Document doc = new com.aspose.words.Document("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\科锐国际调研纪要.doc");\ndoc.setFontSettings(fontSettings);\n\n// 设置样式\ndoc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setSize(10.5);\ndoc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setBold(true);\ndoc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getFont().setName("宋体");\ndoc.getStyles().getByStyleIdentifier(StyleIdentifier.TOC_1).getParagraphFormat().setLineSpacing(18);\n\n// 保存为PDF格式\ndoc.save("D:\\work\\project\\prit-research-comment\\src\\main\\resources\\科锐国际调研纪要.pdf", SaveFormat.PDF);\n\n\n请确保将 "D:\\work\\project\\prit-research-comment\\src\\main\\resources\\fonts" 替换为包含字体文件的实际文件夹路径。\n\n通过设置字体替代,可以确保生成的 PDF 文件中所有字体都能够正确显示,即使目标系统缺少某些字体。\n\n## 相关信息\n\n* Aspose.Words 文档: https://reference.aspose.com/words/java/\n* Aspose.Words 开发者论坛: https://forum.aspose.com/c/words\n\n## 作者\n\n本文档由 Aspose.Words 开发团队编写。\n\n## 版权\n\n版权所有 (c) 2023 Aspose Pty Ltd. 保留所有权利。\n\n本文档仅供参考,不得用于任何商业目的。"}
原文地址: https://www.cveoy.top/t/topic/pL0Y 著作权归作者所有。请勿转载和采集!