How to Implement Resource Prefix for Nested Resources

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

Resource Prefix for Nested Resources

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

  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}
Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.