Aspose.Diagram字体操作

使用字体

Aspose.Diagram 在 Windows 上查找 TrueType 字体的位置

Aspose.Diagram 搜索字体Windows\字体文件夹。此默认设置在大多数情况下都有效,因此仅在确实需要时才指定您自己的字体文件夹。

如何明确指定字体文件夹

Aspose.Diagram API 已公开 setFontDirs 方法Diagram类来指定字体文件夹,如下所述。

  1. Diagram.setFontDirs 方法将字符串数组作为参数,因此开发人员可以使用此方法指定许多字体目录。

演示如何设置 Aspose.Diagram 以在呈现或嵌入字体时在多个文件夹中查找 TrueType 字体。

编程范例

下面的代码示例演示了如何设置 Aspose.Diagram 以在呈现或嵌入字体时在多个文件夹中查找 TrueType 字体。

在呈现过程中接收丢失字体和字体替换的通知

Aspose.Diagram API requires access to the accurate font in order to properly render the drawing to PDF format. If the required font is not available on the machine, then Aspose.Diagram API renders any instance of that font using the default font or the closest available font on the machine, since this substitution can change the look of the rendered drawing, developers may need to be notified when a font is missing and with what font it will be replaced.

缺少字体的通知和字体替换编程示例

在呈现期间通知字体替换:

  1. 创建一个类来实现警告回调
  2. 将其传递给 PdfSaveOptions.setWarningCallback(com.aspose.diagram.IWarningCallback) 属性。

在保存图形的实例警告回调如果绘图存在任何潜在的保真度问题,则调用。在这种情况下,我们选择只处理字体替换的警告并将警告打印到屏幕上。下面的示例演示了如何通过使用接收字体替换通知警告回调.

Java

 // load the document to render.

Diagram diagram = new Diagram("C:\\temp\\Output.vsdx");


// initialize PdfSaveOptions object

com.aspose.diagram.PdfSaveOptions saveOp = new com.aspose.diagram.PdfSaveOptions();

// create a new class implementing IWarningCallback which collect any warnings produced during drawing save.

HandleDocumentWarnings callback = new HandleDocumentWarnings();

saveOp.setWarningCallback(callback);



// pass the save options along with the save path to the save method.

diagram.save("C:\\temp\\Rendering.MissingFontNotification Out.pdf", saveOp);

实施 IWarningCallback

最后一步是创建实现的类警告回调界面。此类将向控制台打印出任何字体替换警告。下面的示例演示了如何实现 IWarningCallback 以在文档保存期间收到有关任何字体替换的通知。

Java

 public class HandleDocumentWarnings implements IWarningCallback {

     /**

     * Our callback only needs to implement the "Warning" method. This method is

     * called whenever there is a potential issue during document processing.

     * The callback can be set to listen for warnings generated during document

     * load and/or document save.

     */

     public void warning(WarningInfo info) {

         // We are only interested in fonts being substituted.

         if (info.getWarningType() == WarningType.FONT_SUBSTITUTION) {

         System.out.println("Font substitution: " + info.getDescription());

     }

 }

}