Aspose::Words::Saving::FontSavingArgs class

FontSavingArgs class

Provides data for the FontSaving() event. To learn more, visit the Save a Document documentation article.

class FontSavingArgs : public System::Object

Methods

MethodDescription
get_Bold() constIndicates whether the current font is bold.
get_Document() constGets the document object that is being saved.
get_FontFamilyName() constIndicates the current font family name.
get_FontFileName() constGets or sets the file name (without path) where the font will be saved to.
get_FontStream() constAllows to specify the stream where the font will be saved to.
get_IsExportNeeded() constAllows to specify whether the current font will be exported as a font resource. Default is true.
get_IsSubsettingNeeded() constAllows to specify whether the current font will be subsetted before exporting as a font resource.
get_Italic() constIndicates whether the current font is italic.
get_KeepFontStreamOpen() constSpecifies whether Aspose.Words should keep the stream open or close it after saving a font.
get_OriginalFileName() constGets the original font file name with an extension.
get_OriginalFileSize() constGets the original font file size.
GetType() const override
Is(const System::TypeInfo&) const override
set_FontFileName(const System::String&)Setter for Aspose::Words::Saving::FontSavingArgs::get_FontFileName.
set_FontStream(const System::SharedPtr<System::IO::Stream>&)Setter for Aspose::Words::Saving::FontSavingArgs::get_FontStream.
set_FontStream(std::basic_ostream<CharType, Traits>&)
set_IsExportNeeded(bool)Setter for Aspose::Words::Saving::FontSavingArgs::get_IsExportNeeded.
set_IsSubsettingNeeded(bool)Setter for Aspose::Words::Saving::FontSavingArgs::get_IsSubsettingNeeded.
set_KeepFontStreamOpen(bool)Setter for Aspose::Words::Saving::FontSavingArgs::get_KeepFontStreamOpen.
static Type()

Remarks

When Aspose.Words saves a document to HTML or related formats and ExportFontResources is set to true, it saves each font subject for export into a separate file.

FontSavingArgs controls whether particular font resource should be exported and how.

FontSavingArgs also allows to redefine how font file names are generated or to completely circumvent saving of fonts into files by providing your own stream objects.

To decide whether to save a particular font resource, use the IsExportNeeded property.

To save fonts into streams instead of files, use the FontStream property.

Examples

Shows how to define custom logic for exporting fonts when saving to HTML.

void SaveExportedFonts()
{
    auto doc = MakeObject<Document>(MyDir + u"Rendering.docx");

    // Configure a SaveOptions object to export fonts to separate files.
    // Set a callback that will handle font saving in a custom manner.
    auto options = MakeObject<HtmlSaveOptions>();
    options->set_ExportFontResources(true);
    options->set_FontSavingCallback(MakeObject<ExHtmlSaveOptions::HandleFontSaving>());

    // The callback will export .ttf files and save them alongside the output document.
    doc->Save(ArtifactsDir + u"HtmlSaveOptions.SaveExportedFonts.html", options);

    std::function<bool(String s)> endsWithTtf = [](String s)
    {
        return s.EndsWith(u".ttf");
    };

    for (String fontFilename : System::Array<String>::FindAll(System::IO::Directory::GetFiles(ArtifactsDir), endsWithTtf))
    {
        std::cout << fontFilename << std::endl;
    }

}

class HandleFontSaving : public IFontSavingCallback
{
private:
    void FontSaving(SharedPtr<FontSavingArgs> args) override
    {
        std::cout << "Font:\t" << args->get_FontFamilyName();
        if (args->get_Bold())
        {
            std::cout << ", bold";
        }
        if (args->get_Italic())
        {
            std::cout << ", italic";
        }
        std::cout << "\nSource:\t" << args->get_OriginalFileName() << ", " << args->get_OriginalFileSize() << " bytes\n" << std::endl;

        // We can also access the source document from here.
        ASSERT_TRUE(args->get_Document()->get_OriginalFileName().EndsWith(u"Rendering.docx"));

        ASSERT_TRUE(args->get_IsExportNeeded());
        ASSERT_TRUE(args->get_IsSubsettingNeeded());

        // There are two ways of saving an exported font.
        // 1 -  Save it to a local file system location:
        args->set_FontFileName(args->get_OriginalFileName().Split(MakeArray<char16_t>({System::IO::Path::DirectorySeparatorChar}))->LINQ_Last());

        // 2 -  Save it to a stream:
        args->set_FontStream(MakeObject<System::IO::FileStream>(
            ArtifactsDir + args->get_OriginalFileName().Split(MakeArray<char16_t>({System::IO::Path::DirectorySeparatorChar}))->LINQ_Last(),
            System::IO::FileMode::Create));
        ASSERT_FALSE(args->get_KeepFontStreamOpen());
    }
};

See Also