public interface IWarningCallback
Example: Example: Example:
// Load the document to render
Document doc = new Document(getMyDir() + "Document.docx");
// Create a new class implementing IWarningCallback and assign it to the PdfSaveOptions class
ExRendering.HandleDocumentWarnings callback = new ExRendering.HandleDocumentWarnings();
doc.setWarningCallback(callback);
// We can choose the default font to use in the case of any missing fonts
FontSettings.getDefaultInstance().getSubstitutionSettings().getDefaultFontSubstitution().setDefaultFontName("Arial");
// For testing we will set Aspose.Words to look for fonts only in a folder which doesn't exist. Since Aspose.Words won't
// find any fonts in the specified directory, then during rendering the fonts in the document will be substituted with the default
// font specified under FontSettings.DefaultFontName. We can pick up on this substitution using our callback
FontSettings.getDefaultInstance().setFontsFolder("", false);
// Pass the save options along with the save path to the save method
doc.save(getArtifactsDir() + "Font.SubstitutionNotification.pdf");
public void handleBinaryRasterWarnings() throws Exception {
Document doc = new Document(getMyDir() + "WMF with image.docx");
MetafileRenderingOptions metafileRenderingOptions = new MetafileRenderingOptions();
metafileRenderingOptions.setEmulateRasterOperations(false);
metafileRenderingOptions.setRenderingMode(MetafileRenderingMode.VECTOR_WITH_FALLBACK);
// If Aspose.Words cannot correctly render some of the metafile records to vector graphics then Aspose.Words renders this metafile to a bitmap
HandleDocumentWarnings callback = new HandleDocumentWarnings();
doc.setWarningCallback(callback);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setMetafileRenderingOptions(metafileRenderingOptions);
doc.save(getArtifactsDir() + "PdfSaveOptions.HandleBinaryRasterWarnings.pdf", saveOptions);
Assert.assertEquals(callback.mWarnings.getCount(), 1);
Assert.assertTrue(callback.mWarnings.get(0).getDescription().contains("R2_XORPEN"));
}
public static 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(final WarningInfo info) {
//For now type of warnings about unsupported metafile records changed from DataLoss/UnexpectedContent to MinorFormattingLoss
if (info.getWarningType() == WarningType.MINOR_FORMATTING_LOSS) {
System.out.println("Unsupported operation: " + info.getDescription());
this.mWarnings.warning(info);
}
}
public WarningInfoCollection mWarnings = new WarningInfoCollection();
}
@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();
}
Method Summary | ||
---|---|---|
abstract void | warning(WarningInfo info) | |
Aspose.Words invokes this method when it encounters some issue during document loading or saving that might result in loss of formatting or data fidelity. |
Method Detail |
---|
warning | |
public abstract void warning(WarningInfo info) |
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(); }