How to Implement Resource Prefix for Nested Resources

This article shows how to add resource prefix for nested resources using the Aspose.Tasks for .NET API.

Resource Prefix for Nested Resources

Aspose.Tasks for .NET provides ResourceSavingArgs.NestedUri property which allows saving nested resources (e.g. PNG inside SVG files) in the separate folder.

 1public class ResourcePrefixForNestedResources : ICssSavingCallback, IFontSavingCallback, IImageSavingCallback
 2{
 3    public static void Run()
 4    {
 5        Project project = new Project("New Project.mpp");
 6        HtmlSaveOptions options = GetSaveOptions(1);
 7        project.Save("document.html", options);
 8    }
 9
10    public void CssSaving(CssSavingArgs args)
11    {
12        FileStream stream = new FileStream("css/" + args.FileName, FileMode.Create);
13        args.Stream = stream;
14        args.KeepStreamOpen = false;
15        args.Uri = "css/" + args.FileName;
16    }
17
18    public void FontSaving(FontSavingArgs args)
19    {
20        FileStream stream = new FileStream("fonts/" + args.FileName, FileMode.Create);
21        args.Stream = stream;
22        args.KeepStreamOpen = false;
23        args.Uri = "fonts/" + args.FileName;
24    }
25
26    public void ImageSaving(ImageSavingArgs args)
27    {
28        if (args.FileName.EndsWith("png"))
29        {
30            FileStream stream1 = new FileStream("resources/nestedResources/" + args.FileName, FileMode.Create);
31            args.Stream = stream1;
32            args.KeepStreamOpen = false;
33            args.Uri = "resources/" + args.FileName;
34            args.NestedUri = "nestedResources/" + args.FileName;
35        }
36        else
37        {
38            FileStream stream2 = new FileStream("resources/" + args.FileName, FileMode.Create);
39            args.Stream = stream2;
40            args.KeepStreamOpen = false;
41            args.Uri = "resources/" + args.FileName;
42        }
43    }
44
45    private static HtmlSaveOptions GetSaveOptions(int pageNumber)
46    {
47        HtmlSaveOptions options = new HtmlSaveOptions
48        {
49            Pages = new List<int>(),
50            IncludeProjectNameInPageHeader = false,
51            IncludeProjectNameInTitle = false,
52            PageSize = PageSize.A3,
53            Timescale = Timescale.ThirdsOfMonths,
54            ReduceFooterGap = true,
55            FontFaceTypes = FontFaceType.Ttf,
56            ExportCss = ResourceExportType.AsFile,
57            ExportFonts = ResourceExportType.AsFile,
58            ExportImages = ResourceExportType.AsFile
59        };
60
61        ResourcePrefixForNestedResources program = new ResourcePrefixForNestedResources();
62        options.FontSavingCallback = program;
63        options.CssSavingCallback = program;
64        options.ImageSavingCallback = program;
65
66        options.Pages.Clear();
67        options.Pages.Add(pageNumber);
68
69        if (!Directory.Exists("fonts"))
70        {
71            Directory.CreateDirectory("fonts");
72        }
73
74        if (!Directory.Exists("resources"))
75        {
76            Directory.CreateDirectory("resources");
77        }
78
79        if (!Directory.Exists("nestedResources"))
80        {
81            Directory.CreateDirectory("resources/nestedResources");
82        }
83
84        if (!Directory.Exists("css"))
85        {
86            Directory.CreateDirectory("css");
87        }
88
89        return options;
90    }
91}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.