WarningCallback
Contents
[
Hide
]DocumentBase.WarningCallback property
Called during various document processing procedures when an issue is detected that might result in data or formatting fidelity loss.
public IWarningCallback WarningCallback { get; set; }
Remarks
Document may generate warnings at any stage of its existence, so it’s important to setup warning callback as early as possible to avoid the warnings loss. E.g. such properties as PageCount
actually build the document layout which is used later for rendering, and the layout warnings may be lost if warning callback is specified just for the rendering calls later.
Examples
Shows how to set the property for finding the closest match for a missing font from the available font sources.
// Open a document that contains text formatted with a font that does not exist in any of our font sources.
Document doc = new Document(MyDir + "Missing font.docx");
// Assign a callback for handling font substitution warnings.
WarningInfoCollection warningCollector = new WarningInfoCollection();
doc.WarningCallback = warningCollector;
// Set a default font name and enable font substitution.
FontSettings fontSettings = new FontSettings();
fontSettings.SubstitutionSettings.DefaultFontSubstitution.DefaultFontName = "Arial";
fontSettings.SubstitutionSettings.FontInfoSubstitution.Enabled = true;
// Original font metrics should be used after font substitution.
doc.LayoutOptions.KeepOriginalFontMetrics = true;
// We will get a font substitution warning if we save a document with a missing font.
doc.FontSettings = fontSettings;
doc.Save(ArtifactsDir + "FontSettings.EnableFontSubstitution.pdf");
foreach (WarningInfo info in warningCollector)
{
if (info.WarningType == WarningType.FontSubstitution)
Console.WriteLine(info.Description);
}
Shows how to use the IWarningCallback interface to monitor font substitution warnings.
public void SubstitutionWarning()
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Times New Roman";
builder.Writeln("Hello world!");
FontSubstitutionWarningCollector callback = new FontSubstitutionWarningCollector();
doc.WarningCallback = callback;
// Store the current collection of font sources, which will be the default font source for every document
// for which we do not specify a different font source.
FontSourceBase[] originalFontSources = FontSettings.DefaultInstance.GetFontsSources();
// For testing purposes, we will set Aspose.Words to look for fonts only in a folder that does not exist.
FontSettings.DefaultInstance.SetFontsFolder(string.Empty, false);
// When rendering the document, there will be no place to find the "Times New Roman" font.
// This will cause a font substitution warning, which our callback will detect.
doc.Save(ArtifactsDir + "FontSettings.SubstitutionWarning.pdf");
FontSettings.DefaultInstance.SetFontsSources(originalFontSources);
Assert.That(callback.FontSubstitutionWarnings[0].WarningType == WarningType.FontSubstitution, Is.True);
Assert.That(callback.FontSubstitutionWarnings[0].Description
.Equals(
"Font 'Times New Roman' has not been found. Using 'Fanwood' font instead. Reason: first available font."), Is.True);
}
private class FontSubstitutionWarningCollector : IWarningCallback
{
/// <summary>
/// Called every time a warning occurs during loading/saving.
/// </summary>
public void Warning(WarningInfo info)
{
if (info.WarningType == WarningType.FontSubstitution)
FontSubstitutionWarnings.Warning(info);
}
public WarningInfoCollection FontSubstitutionWarnings = new WarningInfoCollection();
}
See Also
- interface IWarningCallback
- class DocumentBase
- namespace Aspose.Words
- assembly Aspose.Words