How to Implement Resource Prefix for Nested Resources
When working with Microsoft Project files, projects may contain nested resources such as images or additional files embedded inside output formats (for example, PNG files inside SVGs). Managing these resources efficiently is important to ensure that exported files remain well-structured and portable. With Aspose.Tasks for .NET, you can control how nested resources are saved during export operations. The API provides the ResourceSavingArgs.NestedUri property, which allows developers to define a prefix or custom path for saving related resources into a separate folder. This is particularly useful when exporting to formats like SVG, HTML, or others that require multiple linked files.
Resource Prefix for Nested Resources
The ResourceSavingArgs.NestedUri property enables the configuration of resource prefixes for nested resources. This ensures that all related files (e.g., embedded images) are properly saved and referenced without path conflicts.
Code Example
The following code example demonstrates how to apply a resource prefix when exporting nested resources. This snippet shows how to intercept the saving process, assign a custom URI, and ensure that all nested files are stored in the correct directory with a defined naming convention.
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}FAQ
Q: What are nested resources in Microsoft Project exports?
- Nested resources are additional files such as images or embedded objects that are stored alongside the main project output (e.g., PNG files inside SVG documents).
Q: Do I need Microsoft Project installed to handle nested resources?
- No. Aspose.Tasks for .NET works independently of Microsoft Project and can handle resource export programmatically.
Q: In which formats is ResourceSavingArgs.NestedUri commonly used?
- It is especially useful when exporting to SVG, HTML, or other formats that include multiple linked files.
Q: Can I customize the folder structure for nested resources?
- Yes. You can define your own folder hierarchy and file naming scheme by setting the
NestedUriproperty accordingly.
Conclusion
Managing nested resources during export is essential for keeping project outputs organized and reliable. With the ResourceSavingArgs.NestedUri property in Aspose.Tasks for .NET, developers can easily define custom prefixes and folder structures, ensuring that all embedded files are stored correctly. This approach guarantees consistent, portable, and professional project file exports.