Convert Project Data to HTML in C++
Microsoft Project (MSP) allows developers to save project data (MPP/XML) to HTML. Aspose.Tasks for C++ API also allows you to save project data to the same formats similar to MSP. This is achieved using the standard Save method exposed by the Project class.
Saving Project Data as HTML
Aspose.Tasks for C++ can export project data to HTML format. It allows to save all the data to HTML or export only required pages to HTML by using the SaveOptions as shown in the following code samples.
1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
2System::SharedPtr<HtmlSaveOptions> option = System::MakeObject<HtmlSaveOptions>();
3project->Save(dataDir + u"SaveProjectDataAsHTML_out.html", option);
4
5// OR
6
7// Adding only one page (page number 2)
8option = System::MakeObject<HtmlSaveOptions>();
9option->get_Pages()->Add(2);
10project->Save(dataDir + u"SaveProjectDataAsHTML2_out.html", option);
Controlling Document Header Name during Export to HTML
1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
2System::SharedPtr<HtmlSaveOptions> htmlSaveOptions = System::MakeObject<HtmlSaveOptions>();
3
4// Determines whether to include project name in HTML title (true by default)
5htmlSaveOptions->set_IncludeProjectNameInTitle(false);
6
7// Determines whether to include project name in HTML page header (true by default)
8htmlSaveOptions->set_IncludeProjectNameInPageHeader(false);
9
10htmlSaveOptions->set_Pages(System::MakeObject<System::Collections::Generic::List<int32_t>>());
11htmlSaveOptions->get_Pages()->Add(1);
12project->Save(dataDir + u"ControlHeaderNameDuringHTMLExport_out.html", htmlSaveOptions);
Saving Fonts, Images and CSS Styles Separately
Aspose.Tasks for C++ API gives you enough power to save fonts, images, and CSS styles information separately to files while saving Project Data to HTML. This is achieved using the HtmlSaveOptions class by providing information about CSS, Font and Image destinations.
1void ResourcePrefixForNestedResources::Run()
2{
3 // The path to the documents directory.
4 System::String dataDir = RunExamples::GetDataDir(System::Reflection::MethodBase::GetCurrentMethod(ASPOSE_CURRENT_FUNCTION)->get_DeclaringType().get_FullName());
5
6 {
7 System::SharedPtr<System::IO::FileStream> fs = System::MakeObject<System::IO::FileStream>(dataDir + u"Project1.mpp", System::IO::FileMode::Open);
8 // Clearing resources under 'using' statement
9 System::Details::DisposeGuard<1> __dispose_guard_1({ fs});
10 // ------------------------------------------
11
12 try
13 {
14 System::SharedPtr<Project> project = System::MakeObject<Project>(fs);
15 System::SharedPtr<HtmlSaveOptions> options = GetSaveOptions(1);
16 {
17 System::SharedPtr<System::IO::FileStream> stream = System::MakeObject<System::IO::FileStream>(dataDir + u"document.html", System::IO::FileMode::Create);
18 // Clearing resources under 'using' statement
19 System::Details::DisposeGuard<1> __dispose_guard_0({ stream});
20 // ------------------------------------------
21
22 try
23 {
24 project->Save(stream, options);
25 }
26 catch(...)
27 {
28 __dispose_guard_0.SetCurrentException(std::current_exception());
29 }
30 }
31 }
32 catch(...)
33 {
34 __dispose_guard_1.SetCurrentException(std::current_exception());
35 }
36 }
37}
38#if defined(__MSVC__)
39#pragma warning( pop )
40#elif defined(__GNUC__)
41#pragma GCC diagnostic pop
42#endif
43
44void ResourcePrefixForNestedResources::CssSaving(System::SharedPtr<CssSavingArgs> args)
45{
46 System::SharedPtr<System::IO::FileStream> stream = System::MakeObject<System::IO::FileStream>(dataDir() + u"css/" + args->get_FileName(), System::IO::FileMode::Create);
47 args->set_Stream(stream);
48 args->set_KeepStreamOpen(false);
49 args->set_Uri(dataDir() + u"css/" + args->get_FileName());
50}
51
52void ResourcePrefixForNestedResources::FontSaving(System::SharedPtr<FontSavingArgs> args)
53{
54 System::SharedPtr<System::IO::FileStream> stream = System::MakeObject<System::IO::FileStream>(dataDir() + u"fonts/" + args->get_FileName(), System::IO::FileMode::Create);
55 args->set_Stream(stream);
56 args->set_KeepStreamOpen(false);
57 args->set_Uri(dataDir() + u"fonts/" + args->get_FileName());
58}
59
60void ResourcePrefixForNestedResources::ImageSaving(System::SharedPtr<ImageSavingArgs> args)
61{
62 if (args->get_FileName().EndsWith(u"png"))
63 {
64 System::SharedPtr<System::IO::FileStream> stream1 = System::MakeObject<System::IO::FileStream>(dataDir() + u"resources/nestedResources/" + args->get_FileName(), System::IO::FileMode::Create);
65 args->set_Stream(stream1);
66 args->set_KeepStreamOpen(false);
67 args->set_Uri(dataDir() + u"resources/" + args->get_FileName());
68 //args->set_NestedUri(dataDir() + u"nestedResources/" + args->get_FileName());
69 }
70 else
71 {
72 System::SharedPtr<System::IO::FileStream> stream2 = System::MakeObject<System::IO::FileStream>(dataDir() + u"resources/" + args->get_FileName(), System::IO::FileMode::Create);
73 args->set_Stream(stream2);
74 args->set_KeepStreamOpen(false);
75 args->set_Uri(dataDir() + u"resources/" + args->get_FileName());
76 }
77}
78
79System::SharedPtr<Aspose::Tasks::Saving::HtmlSaveOptions> ResourcePrefixForNestedResources::GetSaveOptions(int32_t pageNumber)
80{
81 auto saveOptions = [&]{ auto tmp_0 = System::MakeObject<HtmlSaveOptions>(); tmp_0->set_Pages(System::MakeObject<System::Collections::Generic::List<int32_t>>()); tmp_0->set_IncludeProjectNameInPageHeader(false); tmp_0->set_IncludeProjectNameInTitle(false); tmp_0->set_PageSize(Aspose::Tasks::Visualization::PageSize::A3); tmp_0->set_Timescale(Aspose::Tasks::Visualization::Timescale::ThirdsOfMonths); tmp_0->set_ReduceFooterGap(true); tmp_0->set_FontFaceTypes(Aspose::Tasks::FontFaceType::Ttf); tmp_0->set_ExportCss(Aspose::Tasks::ResourceExportType::AsFile); tmp_0->set_ExportFonts(Aspose::Tasks::ResourceExportType::AsFile); tmp_0->set_ExportImages(Aspose::Tasks::ResourceExportType::AsFile); return tmp_0; }();
82
83 System::SharedPtr<ResourcePrefixForNestedResources> program = System::MakeObject<ResourcePrefixForNestedResources>();
84 saveOptions->set_FontSavingCallback(program);
85 saveOptions->set_CssSavingCallback(program);
86 saveOptions->set_ImageSavingCallback(program);
87
88 saveOptions->get_Pages()->Clear();
89 saveOptions->get_Pages()->Add(pageNumber);
90
91 if (!System::IO::Directory::Exists(dataDir() + u"fonts"))
92 {
93 System::IO::Directory::CreateDirectory_(dataDir() + u"fonts");
94 }
95
96 if (!System::IO::Directory::Exists(dataDir() + u"resources"))
97 {
98 System::IO::Directory::CreateDirectory_(dataDir() + u"resources");
99 }
100
101 if (!System::IO::Directory::Exists(dataDir() + u"nestedResources"))
102 {
103 System::IO::Directory::CreateDirectory_(dataDir() + u"resources/nestedResources");
104 }
105
106 if (!System::IO::Directory::Exists(dataDir() + u"css"))
107 {
108 System::IO::Directory::CreateDirectory_(dataDir() + u"css");
109 }
110
111 return saveOptions;
112}
Adding Page Prefix in CSS Classes While Exporting to HTML
1System::SharedPtr<Project> project = System::MakeObject<Project>(dataDir + u"CreateProject2.mpp");
2
3System::SharedPtr<HtmlSaveOptions> options = [&]{ auto tmp_0 = System::MakeObject<HtmlSaveOptions>(); tmp_0->set_CssStylePrefix(u"test_prefix"); return tmp_0; }();
4
5project->Save(dataDir + u"TestCssStylePrefix_out.html", options);