java.lang.Object
com.aspose.words.WarningInfoCollection
- All Implemented Interfaces:
- IWarningCallback, java.lang.Iterable
public class WarningInfoCollection
- extends java.lang.Object
Represents a typed collection of WarningInfo objects.
You can use this collection object as the simplest form of IWarningCallback implementation to gather
all warnings that Aspose.Words generates during a load or save operation. Create an instance of this class and assign it
to the LoadOptions.WarningCallback or DocumentBase.WarningCallback property.
Example:
Shows how to set the property for finding the closest match font among the available font sources instead missing font.
@Test
public void enableFontSubstitution() throws Exception {
Document doc = new Document(getMyDir() + "Missing font.docx");
// Assign a custom warning callback
HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
doc.setWarningCallback(substitutionWarningHandler);
// Set a default font name and enable font substitution
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
// When saving the document with the missing font, we should get a warning
doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.EnableFontSubstitution.pdf");
// List all warnings using an enumerator
Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator();
try /*JAVA: was using*/ {
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
} finally {
if (warnings != null) warnings.remove();
}
// Warnings are stored in this format
Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource());
Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.",
substitutionWarningHandler.FontWarnings.get(0).getDescription());
// The warning info collection can also be cleared like this
substitutionWarningHandler.FontWarnings.clear();
Assert.assertNull(substitutionWarningHandler.FontWarnings);
}
public static class HandleDocumentSubstitutionWarnings implements IWarningCallback {
/// <summary>
/// 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.
/// </summary>
public void warning(WarningInfo info) {
// We are only interested in fonts being substituted
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
- See Also:
- WarningInfo, IWarningCallback
Property Getters/Setters Summary |
int | getCount() | |
|
Gets the number of elements contained in the collection.
|
WarningInfo | get(int index) | |
|
Gets an item at the specified index.
|
Method Summary |
void | clear() | |
Removes all elements from the collection.
|
java.util.Iterator<WarningInfo> | iterator() | |
Returns an iterator object that can be used to iterate over all items in the collection.
|
void | warning(WarningInfo info) | |
Implements the IWarningCallback interface. Adds a warning to this collection.
|
WarningInfoCollection
public WarningInfoCollection()
-
Property Getters/Setters Detail |
getCount | |
public int getCount()
|
-
Gets the number of elements contained in the collection.
-
Gets an item at the specified index.
- Parameters:
index
- Zero-based index of the item.
clear | |
public void clear() |
-
Removes all elements from the collection.
Example:
Shows how to set the property for finding the closest match font among the available font sources instead missing font.
@Test
public void enableFontSubstitution() throws Exception {
Document doc = new Document(getMyDir() + "Missing font.docx");
// Assign a custom warning callback
HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
doc.setWarningCallback(substitutionWarningHandler);
// Set a default font name and enable font substitution
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
// When saving the document with the missing font, we should get a warning
doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.EnableFontSubstitution.pdf");
// List all warnings using an enumerator
Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator();
try /*JAVA: was using*/ {
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
} finally {
if (warnings != null) warnings.remove();
}
// Warnings are stored in this format
Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource());
Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.",
substitutionWarningHandler.FontWarnings.get(0).getDescription());
// The warning info collection can also be cleared like this
substitutionWarningHandler.FontWarnings.clear();
Assert.assertNull(substitutionWarningHandler.FontWarnings);
}
public static class HandleDocumentSubstitutionWarnings implements IWarningCallback {
/// <summary>
/// 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.
/// </summary>
public void warning(WarningInfo info) {
// We are only interested in fonts being substituted
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
iterator | |
public java.util.Iterator<WarningInfo> iterator() |
-
Returns an iterator object that can be used to iterate over all items in the collection.
Example:
Shows how to set the property for finding the closest match font among the available font sources instead missing font.
@Test
public void enableFontSubstitution() throws Exception {
Document doc = new Document(getMyDir() + "Missing font.docx");
// Assign a custom warning callback
HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
doc.setWarningCallback(substitutionWarningHandler);
// Set a default font name and enable font substitution
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
// When saving the document with the missing font, we should get a warning
doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.EnableFontSubstitution.pdf");
// List all warnings using an enumerator
Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator();
try /*JAVA: was using*/ {
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
} finally {
if (warnings != null) warnings.remove();
}
// Warnings are stored in this format
Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource());
Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.",
substitutionWarningHandler.FontWarnings.get(0).getDescription());
// The warning info collection can also be cleared like this
substitutionWarningHandler.FontWarnings.clear();
Assert.assertNull(substitutionWarningHandler.FontWarnings);
}
public static class HandleDocumentSubstitutionWarnings implements IWarningCallback {
/// <summary>
/// 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.
/// </summary>
public void warning(WarningInfo info) {
// We are only interested in fonts being substituted
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
-
Implements the IWarningCallback interface. Adds a warning to this collection.
Example:
Shows how to set the property for finding the closest match font among the available font sources instead missing font.
@Test
public void enableFontSubstitution() throws Exception {
Document doc = new Document(getMyDir() + "Missing font.docx");
// Assign a custom warning callback
HandleDocumentSubstitutionWarnings substitutionWarningHandler = new HandleDocumentSubstitutionWarnings();
doc.setWarningCallback(substitutionWarningHandler);
// Set a default font name and enable font substitution
FontSettings fontSettings = new FontSettings();
fontSettings.getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
fontSettings.getSubstitutionSettings().getFontInfoSubstitution().setEnabled(true);
// When saving the document with the missing font, we should get a warning
doc.setFontSettings(fontSettings);
doc.save(getArtifactsDir() + "Font.EnableFontSubstitution.pdf");
// List all warnings using an enumerator
Iterator<WarningInfo> warnings = substitutionWarningHandler.FontWarnings.iterator();
try /*JAVA: was using*/ {
while (warnings.hasNext())
System.out.println(warnings.next().getDescription());
} finally {
if (warnings != null) warnings.remove();
}
// Warnings are stored in this format
Assert.assertEquals(WarningSource.LAYOUT, substitutionWarningHandler.FontWarnings.get(0).getSource());
Assert.assertEquals("Font '28 Days Later' has not been found. Using 'Calibri' font instead. Reason: alternative name from document.",
substitutionWarningHandler.FontWarnings.get(0).getDescription());
// The warning info collection can also be cleared like this
substitutionWarningHandler.FontWarnings.clear();
Assert.assertNull(substitutionWarningHandler.FontWarnings);
}
public static class HandleDocumentSubstitutionWarnings implements IWarningCallback {
/// <summary>
/// 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.
/// </summary>
public void warning(WarningInfo info) {
// We are only interested in fonts being substituted
if (info.getWarningType() == WarningType.FONT_SUBSTITUTION)
FontWarnings.warning(info);
}
public WarningInfoCollection FontWarnings = new WarningInfoCollection();
}
See Also:
Aspose.Words Documentation - the home page for the Aspose.Words Product Documentation.
Aspose.Words Support Forum - our preferred method of support.