Cant get callback to work

Hi There,

I’m unable to get the DocumentPartSaving callback to be invoked. Here’s a simple code:

public class HTMLDocumentFormatting : IDocumentPartSavingCallback
{
    public void DocumentPartSaving(DocumentPartSavingArgs args)
    {
        Document doc = args.Document;
    }
}

public partial class FormEditor
{
    public string CreateHTMLFile(Document doc, string tmpFile)
    {
        string htmlFile = Path.GetDirectoryName(tmpFile) + "\"" + Path.GetFileNameWithoutExtension(tmpFile) + ".html";

        HtmlSaveOptions htmlOptions = new HtmlSaveOptions(SaveFormat.Html);

        htmlOptions.ExportRoundtripInformation = true;

        htmlOptions.ExportTocPageNumbers = false;
        htmlOptions.ExportDocumentProperties = true;
        htmlOptions.ExportPageSetup = true;
        htmlOptions.PrettyFormat = true;
        htmlOptions.ExportListLabels = ExportListLabels.AsInlineText;
        htmlOptions.DocumentPartSavingCallback = new HTMLDocumentFormatting();

        htmlOptions.CssStyleSheetType = CssStyleSheetType.Inline;

        htmlOptions.ExportImagesAsBase64 = true;

        doc.Save(htmlFile, htmlOptions);

        return htmlFile;
    }
}

While I was able to get a similar example for CssSaving to work, I can’t seem get the code of DocumentPartSaving to be invoked while saving. The original document is a big one containing 42 pages document with multiple sections etc.

What am I doing wrong?

Thanks,
Itzik

Hi Itzik,

Thanks for your inquiry. Please use HtmlSaveOptions.DocumentSplitCriteria property in your code to invoke DocumentPartSaving.

When Aspose.Words saves a document to HTML or related formats and DocumentSplitCriteria is specified, the document is split into parts and by default, each document part is saved into a separate file.

Class DocumentPartSavingArgs allows you to control how each document part will be saved. It allows to redefine how file names are generated or to completely circumvent saving of document parts into files by providing your own stream objects.

HtmlSaveOptions htmlOptions = new HtmlSaveOptions(SaveFormat.Html);
htmlOptions.ExportRoundtripInformation = true;
htmlOptions.ExportTocPageNumbers = false;
htmlOptions.ExportDocumentProperties = true;
htmlOptions.ExportPageSetup = true;
htmlOptions.PrettyFormat = true;
htmlOptions.ExportListLabels = ExportListLabels.AsInlineText;
htmlOptions.DocumentPartSavingCallback = new HTMLDocumentFormatting();
htmlOptions.CssStyleSheetType = CssStyleSheetType.Inline;
htmlOptions.ExportImagesAsBase64 = true;
htmlOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
Document doc = new Document(MyDir + "in.docx");
doc.Save(MyDir + "Out.html", htmlOptions);
public class HTMLDocumentFormatting : IDocumentPartSavingCallback
{
    public int i = 1;
    public void DocumentPartSaving(DocumentPartSavingArgs args)
    {
        args.DocumentPartFileName = "FileName" + i + ".html";
        i++;
    }
}