Spire.Doc for .NET是一款专门对 Word 文档进行操作的 .NET 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
现在 Spire.Doc 支持在保存为 .docx 文件格式时将字体文件中的私有字体嵌入到 Word 文档中。本文将向您展示如何使用 Spire.Doc 完成此任务的详细步骤。
为了演示,我们使用了字体文件 DeeDeeFlowers.ttf。
在接下来的部分中,我们会将上述文件中的字体嵌入到 Word 文档中,并使用它来创建文本。
第 1 步:创建一个空白 Word 文档。
Document document = new Document();
第 2 步:在文档中添加一个节和一个段落。
Section section = document.AddSection(); Paragraph p = section.AddParagraph();
第 3 步:在段落中添加文本,然后设置文本的字体名称和字体大小。
TextRange range = p.AppendText("Let life be beautiful like summer flowers\n" +"Life, thin and light-off time and time again\n" + "Frivolous tireless"); range.CharacterFormat.FontName = "DeeDeeFlowers"; range.CharacterFormat.FontSize = 20;
第 4 步:通过将 EmbedFontsInFile 属性的布尔值设置为 true,允许在文档中嵌入字体。
document.EmbedFontsInFile = true;
第 5 步:将字体文件中的私有字体嵌入到文档中。
document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf"));
第 6 步:另存为 .docx 文件格式。
document.SaveToFile("result.docx", FileFormat.Docx);
运行代码后,我们将得到以下输出:
完整代码:
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; namespace Embed_private_font_into_Word { class Program { static void Main(string[] args) { Document document = new Document(); Section section = document.AddSection(); Paragraph p = section.AddParagraph(); TextRange range = p.AppendText("Let life be beautiful like summer flowers\n" +"Life, thin and light-off time and time again\n" + "Frivolous tireless"); range.CharacterFormat.FontName = "DeeDeeFlowers"; range.CharacterFormat.FontSize = 20; document.EmbedFontsInFile = true; document.PrivateFontList.Add(new PrivateFontPath("DeeDeeFlowers", @"E:\Program Files\DeeDeeFlowers.ttf")); document.SaveToFile("result.docx", FileFormat.Docx); } } }
以上便如何保存为 .docx 文件格式时将私有字体嵌入到 Word 文档中,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。