ปรับแต่งฟอนต์ PowerPoint บน .NET

Overview

Aspose.Slides ช่วยให้คุณใช้ฟอนต์กำหนดเองในงานนำเสนอโดยไม่ต้องติดตั้งบนระบบปฏิบัติการ คุณสามารถโหลดฟอนต์จากโฟลเดอร์กำหนดเอง, จัดหาฟอนต์สำหรับงานนำเสนอเฉพาะผ่าน document‑level font sources, หรือโหลดฟอนต์ภายนอกโดยตรงจากข้อมูลไบนารี

ฟอนต์ที่โหลดจะถูกใช้เมื่อเรนเดอร์หรือส่งออกงานนำเสนอ เช่น ไปเป็น PDF, รูปภาพ, และรูปแบบอื่นที่รองรับ ซึ่งช่วยให้ผลลัพธ์ของงานนำเสนอคงที่ในสภาพแวดล้อมต่าง ๆ บทความนี้ยังอธิบายวิธีตรวจสอบโฟลเดอร์ฟอนต์ที่ Aspose.Slides ใช้และวิธีล้างแคชฟอนต์หลังจากทำงานกับฟอนต์ภายนอก

การลงทะเบียนฟอนต์กำหนดเองสำหรับการเรนเดอร์แตกต่างจากการฝังฟอนต์ลงในไฟล์ PPTX หากต้องการให้ฟอนต์ถูกเก็บไว้ภายในงานนำเสนอเอง ให้ใช้คุณสมบัติการฝังฟอนต์อย่างชัดเจน

ธีมของงานนำเสนอสามารถอ้างอิงฟอนต์ตระกูลต่าง ๆ สำหรับระบบการเขียนแต่ละระบบ การแมปเหล่านี้เก็บชื่อฟอนต์แต่ไม่ได้ติดตั้งหรือโหลดไฟล์ฟอนต์ ดูที่Script‑Specific Theme Fontsเพื่อจัดการการแมป และใช้ตัวเลือกการโหลดด้านล่างเพื่อทำให้ฟอนต์ที่อ้างอิงพร้อมใช้งานสำหรับการเรนเดอร์ที่สอดคล้องกัน

Load Custom Fonts

Aspose.Slides ช่วยให้คุณโหลดฟอนต์ที่ใช้ในงานนำเสนอโดยไม่ต้องติดตั้งบนระบบ ซึ่งส่งผลต่อผลลัพธ์การส่งออก—เช่น PDF, รูปภาพ, และรูปแบบอื่นที่รองรับ—เพื่อให้เอกสารที่ได้มีลักษณะสม่ำเสมอในสภาพแวดล้อมต่าง ๆ ฟอนต์จะถูกโหลดจากไดเร็กทอรีกำหนดเอง

  1. ระบุโฟลเดอร์หนึ่งหรือหลายโฟลเดอร์ที่มีไฟล์ฟอนต์
  2. เรียกเมธอดสแตติกFontsLoader.LoadExternalFontsเพื่อโหลดฟอนต์จากโฟลเดอร์เหล่านั้น
  3. โหลดและเรนเดอร์/ส่งออกงานนำเสนอ
  4. เรียกFontsLoader.ClearCacheเพื่อล้างแคชฟอนต์

ตัวอย่างโค้ดต่อไปแสดงกระบวนการโหลดฟอนต์:

using Aspose.Slides;
using Aspose.Slides.Export;

// กำหนดโฟลเดอร์ที่บรรจุไฟล์ฟอนต์กำหนดเอง.
string[] fontFolders = { @"C:\MyFonts", @"D:\Fonts" };

// โหลดฟอนต์กำหนดเองจากโฟลเดอร์ที่ระบุ.
FontsLoader.LoadExternalFonts(fontFolders);

using Presentation presentation = new Presentation("sample.pptx");

// เรนเดอร์/ส่งออกงานนำเสนอ (เช่น ไปยัง PDF, รูปภาพ, หรือรูปแบบอื่น) ด้วยฟอนต์ที่โหลดแล้ว.
presentation.Save("output.pdf", SaveFormat.Pdf);

// ล้างแคชฟอนต์หลังจากงานเสร็จสิ้น.
FontsLoader.ClearCache();

Get Custom Font Folders

Aspose.Slides มีเมธอดGetFontFoldersเพื่อให้คุณค้นหาโฟลเดอร์ฟอนต์ เมธอดนี้คืนค่าโฟลเดอร์ที่เพิ่มผ่านเมธอดLoadExternalFontsและโฟลเดอร์ฟอนต์ของระบบ

โค้ด C# ด้านล่างแสดงวิธีใช้GetFontFolders:

using Aspose.Slides;

// บรรทัดนี้แสดงโฟลเดอร์ที่ตรวจสอบไฟล์ฟอนต์.
// เหล่านั้นคือโฟลเดอร์ที่เพิ่มผ่านเมธอด LoadExternalFonts และโฟลเดอร์ฟอนต์ของระบบ.
string[] fontFolders = FontsLoader.GetFontFolders();

Specify Custom Fonts Used with a Presentation

Aspose.Slides มีคุณสมบัติDocumentLevelFontSourcesเพื่อให้คุณระบุฟอนต์ภายนอกที่ใช้กับงานนำเสนอ

โค้ด C# ด้านล่างแสดงวิธีใช้คุณสมบัติDocumentLevelFontSources:

using Aspose.Slides;

byte[] memoryFont1 = File.ReadAllBytes("customfonts\\CustomFont1.ttf");
byte[] memoryFont2 = File.ReadAllBytes("customfonts\\CustomFont2.ttf");

LoadOptions loadOptions = new LoadOptions();
loadOptions.DocumentLevelFontSources.FontFolders = new string[] { "assets\\fonts", "global\\fonts" };
loadOptions.DocumentLevelFontSources.MemoryFonts = new byte[][] { memoryFont1, memoryFont2 };
using (IPresentation presentation = new Presentation("MyPresentation.pptx", loadOptions))
{
    // ทำงานกับงานนำเสนอ
    // CustomFont1, CustomFont2, และฟอนต์จากโฟลเดอร์ assets\fonts และ global\fonts รวมถึงโฟลเดอร์ย่อยของพวกมัน สามารถใช้ได้ในงานนำเสนอ
}

Manage Fonts Externally

Aspose.Slides มีเมธอดLoadExternalFont(byte[] data)เพื่อให้คุณโหลดฟอนต์ภายนอกจากข้อมูลไบนารี

โค้ด C# ด้านล่างแสดงกระบวนการโหลดฟอนต์จากอาร์เรย์ไบต์ :

using Aspose.Slides;

FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALN.TTF"));
FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALNBI.TTF"));
FontsLoader.LoadExternalFont(File.ReadAllBytes("ARIALNI.TTF"));

try
{
    using (Presentation pres = new Presentation(""))
    {
        // ฟอนต์ภายนอกที่โหลดในช่วงอายุของงานนำเสนอ
    }
}
finally
{
    FontsLoader.ClearCache();
}

หากต้องการให้ฟอนต์ที่ลงทะเบียนตรงกันได้รับความสำคัญสูงสุดระหว่างการส่งออกโดยไม่ต้องลบฟอนต์ที่ฝังอยู่ ให้ดูที่Prefer Registered Fonts over Embedded Fonts (.NET 26.11 หรือใหม่กว่า)

FAQ

Do custom fonts affect export to all formats (PDF, PNG, SVG, HTML)?

Yes. Connected fonts are used by the renderer across all export formats.

Are custom fonts automatically embedded into the resulting PPTX?

No. Registering a font for rendering is not the same as embedding it into a PPTX. If you need the font carried inside the presentation file, you must use the explicitembedding features.

Can I control fallback behavior when a custom font lacks certain glyphs?

Yes. Configurefont substitution,replacement rules, andfallback sets to define exactly which font is used when the requested glyph is missing.

Can I use fonts in Linux/Docker containers without installing them system‑wide?

Yes. Point to your own font folders or load fonts from byte arrays. This removes any dependency on system font directories in the container image.

Note for Linux/Docker: When calling FontsLoader.LoadExternalFonts, ensure that every entry in the directories array contains a non‑empty path to an existing directory. If an environment variable used to construct a font path is undefined or empty, Aspose.Slides may attempt to resolve the empty value as a full path, resulting in System.ArgumentException.

What about licensing—can I embed any custom font without restrictions?

You are responsible for font licensing compliance. Terms vary; some licenses prohibit embedding or commercial use. Always review the font’s EULA before distributing outputs.