PageSavingCallback isn't called when saving Docx to PDF

I’m converting Word documents (.docx) to PDFs using the code below. I implemented a IPageSavingCallback class, but the PageSaving method is never called. I’m stuck on version 21.12, so any possible solution needs to be compatible with that version. Any ideas why my PageSaving method is not being called?

Aspose.Words.Document wordDoc = new Aspose.Words.Document(filename);

WordToPDFFeedback wordToPDFFeedback = new WordToPDFFeedback(filenameWithAllDescriptions);
Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
saveOptions.PageSavingCallback = wordToPDFFeedback;
			
wordDoc.Save(newFilename, saveOptions);
internal class WordToPDFFeedback(string _filenameWithDescription) : IPageSavingCallback
{
	//*** This is never called ***
	public void PageSaving(PageSavingArgs args)
	{
		Log.Information(
			"{_filenameWithDescription} progress :: {pageFilename} :: {pageIndex}",
			_filenameWithDescription, args.PageFileName, args.PageIndex
		);
	}
}

Thanks!

@aspears The callback is called properly on my side. Could you please create a simple console application that will demonstrate the issue. I have simplified code a bit for testing:

Aspose.Words.Document wordDoc = new Aspose.Words.Document(@"C:\Temp\in.docx");

WordToPDFFeedback wordToPDFFeedback = new WordToPDFFeedback();
Aspose.Words.Saving.PdfSaveOptions saveOptions = new Aspose.Words.Saving.PdfSaveOptions();
saveOptions.PageSavingCallback = wordToPDFFeedback;

wordDoc.Save(@"C:\Temp\out.pdf", saveOptions);
internal class WordToPDFFeedback : IPageSavingCallback
{
    public void PageSaving(PageSavingArgs args)
    {
        Console.WriteLine("progress :: {0} :: {1}", args.PageFileName, args.PageIndex);
    }
}

Here is console output:

progress :: C:\Temp\out_0.pdf :: 0
progress :: C:\Temp\out_1.pdf :: 1

Thank you for the response, turns out there was a bug in my code. Once I fixed it, the PageSaving method is called. However, this raises a new problem. It seems like it is trying to save each page to its own file. If I don’t omit the saveOptions.PageSavingCallback = wordToPDFFeedback; line, it saves just fine, otherwise it saves individual PDF files with the page number appended to the end. I tried setting the args.PageFileName, but then each page overwrites the previous page and I end up with a 1 page document. Am I using this interface incorrectly?

@aspears This is an expected behavior. Could you please explain the goal of using IPageSavingCallback? Probably in your case it would be more correct to use IPageLayoutCallback:

Aspose.Words.Document wordDoc = new Aspose.Words.Document(@"C:\Temp\in.docx");
wordDoc.LayoutOptions.Callback = new WordLayoutFeedback();
wordDoc.Save(@"C:\Temp\out.pdf");
internal class WordLayoutFeedback : IPageLayoutCallback
{
    public void Notify(PageLayoutCallbackArgs args)
    {
        if (args.Event == PageLayoutEvent.PartReflowFinished)
            Console.WriteLine($"progress :: {args.PageIndex}");
    }
}

Thank you, that is exactly what I was looking for.

1 Like