IDocumentPartSavingCallbackDocumentPartSaving Method |
Namespace: Aspose.Words.Saving
public void DocumentParts() { // Open a document to be converted to html Document doc = new Document(MyDir + "Rendering.docx"); string outFileName = "SavingCallback.DocumentParts.Rendering.html"; // We can use an appropriate SaveOptions subclass to customize the conversion process HtmlSaveOptions options = new HtmlSaveOptions(); // We can use it to split a document into smaller parts, in this instance split by section breaks // Each part will be saved into a separate file, creating many files during the conversion process instead of just one options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak; // We can set a callback to name each document part file ourselves options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria); // If we convert a document that contains images into html, we will end up with one html file which links to several images // Each image will be in the form of a file in the local file system // There is also a callback that can customize the name and file system location of each image options.ImageSavingCallback = new SavedImageRename(outFileName); // The DocumentPartSaving() and ImageSaving() methods of our callbacks will be run at this time doc.Save(ArtifactsDir + outFileName, options); } /// <summary> /// Renames saved document parts that are produced when an HTML document is saved while being split according to a criteria. /// </summary> private class SavedDocumentPartRename : IDocumentPartSavingCallback { public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria) { mOutFileName = outFileName; mDocumentSplitCriteria = documentSplitCriteria; } void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args) { Assert.True(args.Document.OriginalFileName.EndsWith("Rendering.docx")); string partType = ""; switch (mDocumentSplitCriteria) { case DocumentSplitCriteria.PageBreak: partType = "Page"; break; case DocumentSplitCriteria.ColumnBreak: partType = "Column"; break; case DocumentSplitCriteria.SectionBreak: partType = "Section"; break; case DocumentSplitCriteria.HeadingParagraph: partType = "Paragraph from heading"; break; } string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}"; // We can designate the filename and location of each output file either by filename args.DocumentPartFileName = partFileName; // Or we can make a new stream and choose the location of the file at construction args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create); Assert.True(args.DocumentPartStream.CanWrite); Assert.False(args.KeepDocumentPartStreamOpen); } private int mCount; private readonly string mOutFileName; private readonly DocumentSplitCriteria mDocumentSplitCriteria; } /// <summary> /// Renames saved images that are produced when an HTML document is saved. /// </summary> public class SavedImageRename : IImageSavingCallback { public SavedImageRename(string outFileName) { mOutFileName = outFileName; } void IImageSavingCallback.ImageSaving(ImageSavingArgs args) { // Same filename and stream functions as above in IDocumentPartSavingCallback apply here string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}"; args.ImageFileName = imageFileName; args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create); Assert.True(args.ImageStream.CanWrite); Assert.True(args.IsImageAvailable); Assert.False(args.KeepImageStreamOpen); } private int mCount; private readonly string mOutFileName; }