Interface IDocumentSavingCallback
Interface IDocumentSavingCallback
Namespace: Aspose.Words.Saving
Assembly: Aspose.Words.dll (25.12.0)
Implement this interface if you want to have your own custom method called during saving a document.
public interface IDocumentSavingCallbackExamples
Shows how to manage a document while saving to html.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: Html, Mhtml, Epub.
HtmlSaveOptions saveOptions = new HtmlSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"HtmlSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.1d;
}Shows how to manage a document while saving to docx.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: Docx, FlatOpc, Docm, Dotm, Dotx.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"OoxmlSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.01d;
}Shows how to manage a document while saving to xamlflow.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: XamlFlow, XamlFlowPack.
XamlFlowSaveOptions saveOptions = new XamlFlowSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"XamlFlowSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.01d;
}Methods
Notify(DocumentSavingArgs)
This is called to notify of document saving progress.
void Notify(DocumentSavingArgs args)Parameters
args DocumentSavingArgs
An argument of the event.
Examples
Shows how to manage a document while saving to html.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: Html, Mhtml, Epub.
HtmlSaveOptions saveOptions = new HtmlSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"HtmlSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.1d;
}Shows how to manage a document while saving to docx.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: Docx, FlatOpc, Docm, Dotm, Dotx.
OoxmlSaveOptions saveOptions = new OoxmlSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"OoxmlSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.01d;
}Shows how to manage a document while saving to xamlflow.
public void ProgressCallback(SaveFormat saveFormat, string ext)
{
Document doc = new Document(MyDir + "Big document.docx");
// Following formats are supported: XamlFlow, XamlFlowPack.
XamlFlowSaveOptions saveOptions = new XamlFlowSaveOptions(saveFormat)
{
ProgressCallback = new SavingProgressCallback()
};
var exception = Assert.Throws<OperationCanceledException>(() =>
doc.Save(ArtifactsDir + $"XamlFlowSaveOptions.ProgressCallback.{ext}", saveOptions));
Assert.That(exception?.Message.Contains("EstimatedProgress"), Is.True);
}
/// <summary>
/// Saving progress callback. Cancel a document saving after the "MaxDuration" seconds.
/// </summary>
public class SavingProgressCallback : IDocumentSavingCallback
{
/// <summary>
/// Ctr.
/// </summary>
public SavingProgressCallback()
{
mSavingStartedAt = DateTime.Now;
}
/// <summary>
/// Callback method which called during document saving.
/// </summary>
/// <param name="args">Saving arguments.</param>
public void Notify(DocumentSavingArgs args)
{
DateTime canceledAt = DateTime.Now;
double ellapsedSeconds = (canceledAt - mSavingStartedAt).TotalSeconds;
if (ellapsedSeconds > MaxDuration)
throw new OperationCanceledException($"EstimatedProgress = {args.EstimatedProgress}; CanceledAt = {canceledAt}");
}
/// <summary>
/// Date and time when document saving is started.
/// </summary>
private readonly DateTime mSavingStartedAt;
/// <summary>
/// Maximum allowed duration in sec.
/// </summary>
private const double MaxDuration = 0.01d;
}Remarks
The primary uses for this interface is to allow application code to obtain progress status and abort saving process.
An exception should be threw from the progress callback for abortion and it should be caught in the consumer code.