Convert OneNote to Html using user saving callbacks

Time to time it is useful to have full control over storing all Html generated content. For this reason Aspose.Note have an option to provide user-defined callbacks for saving of pages( PageSavingArgs), style sheets( CssSavingArgs), images( ImageSavingArgs) and fonts( FontSavingArgs).

Save OneNote to Html using user saving callbacks

The following code example demonstrates how to convert OneNote to Html with storing all resources(css/fonts/images) by using user-defined callbacks.

The code below creates ‘documentFolder’ folder containing document.html, ‘css’ folder with ‘style.css’ file, ‘images’ folder with images and ‘fonts’ folder with fonts. ‘style.css’ file will contain at the end the following string “/* This line is appended to stream manually by user */”

 1    class UserSavingCallbacks : ICssSavingCallback, IFontSavingCallback, IImageSavingCallback
 2    {
 3        public string RootFolder { get; set; }
 4
 5        public bool KeepCssStreamOpened { get; set; }
 6
 7        public string CssFolder { get; set; }
 8
 9        public Stream CssStream { get; private set; }
10
11        public string FontsFolder { get; set; }
12
13        public string ImagesFolder { get; set; }
14
15        public void FontSaving(FontSavingArgs args)
16        {
17            string uri;
18            Stream stream;
19            this.CreateResourceInFolder(this.FontsFolder, args.FileName, out uri, out stream);
20            args.Stream = stream;
21            args.Uri = Path.Combine("..", uri).Replace("\\", "\\\\");
22        }
23
24        public void CssSaving(CssSavingArgs args)
25        {
26            string uri;
27            Stream stream;
28            this.CreateResourceInFolder(this.CssFolder, args.FileName, out uri, out stream);
29            args.Stream = this.CssStream = stream;
30            args.KeepStreamOpen = this.KeepCssStreamOpened;
31            args.Uri = uri;
32        }
33
34        public void ImageSaving(ImageSavingArgs args)
35        {
36            string uri;
37            Stream stream;
38            this.CreateResourceInFolder(this.ImagesFolder, args.FileName, out uri, out stream);
39            args.Stream = stream;
40            args.Uri = uri;
41        }
42
43        private void CreateResourceInFolder(string folder, string filename, out string uri, out Stream stream)
44        {
45            var relativePath = folder;
46            var fullPath = Path.Combine(this.RootFolder, relativePath);
47            if (!Directory.Exists(fullPath))
48            {
49                Directory.CreateDirectory(fullPath);
50            }
51
52            stream = File.Create(Path.Combine(fullPath, filename));
53            uri = Path.Combine(relativePath, filename);
54        }
55    }
56    
57        public static void SaveAsHTMLToMemoryStreamWithCallBacksToSaveResources()
58        {
59            // This code creates documentFolder containing document.html, css folder with style.css file, images folder with images and fonts folder with fonts.
60            // style.css file will contains at the end the following string "/* This line is appended to stream manually by user */"
61            var savingCallbacks = new UserSavingCallbacks()
62                                  {
63                                      RootFolder = "documentFolder",
64                                      CssFolder = "css",
65                                      KeepCssStreamOpened = true,
66                                      ImagesFolder = "images",
67                                      FontsFolder = "fonts"
68                                  };
69            var options = new HtmlSaveOptions
70                          {
71                              FontFaceTypes = FontFaceType.Ttf,
72                              CssSavingCallback = savingCallbacks,
73                              FontSavingCallback = savingCallbacks,
74                              ImageSavingCallback = savingCallbacks
75                          };
76
77            if (!Directory.Exists(savingCallbacks.RootFolder))
78            {
79                Directory.CreateDirectory(savingCallbacks.RootFolder);
80            }
81
82            string dataDir = RunExamples.GetDataDir_LoadingAndSaving();
83            var document = new Document(Path.Combine(dataDir, "Aspose.one"));
84
85            using (var stream = File.Create(Path.Combine(savingCallbacks.RootFolder, "document.html")))
86            {
87                document.Save(stream, options);
88            }
89
90            using (var writer = new StreamWriter(savingCallbacks.CssStream))
91            {
92                writer.WriteLine();
93                writer.WriteLine("/* This line is appended to stream manually by user */");
94            }
95        }
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.