翻譯|使用教程|編輯:胡濤|2023-05-04 10:15:45.190|閱讀 117 次
概述:本文介紹如何在spire.doc中,保存為 .docx 文件格式時將私有字體嵌入到 Word 文檔中,歡迎查閱
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
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 文檔中,如果您有其他問題也可以繼續瀏覽本系列文章,獲取相關教程,你還可以給我留言或者加入我們的官方技術交流群。
歡迎下載|體驗更多E-iceblue產品
獲取更多信息請咨詢 ;技術交流Q群(767755948)
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn