Class HtmlSaveOptions
Namespace: Aspose.Words.Saving
Assembly: Aspose.Words.dll (25.12.0)
Can be used to specify additional options when saving a document into the Aspose.Words.SaveFormat.Html, Aspose.Words.SaveFormat.Mhtml, Aspose.Words.SaveFormat.Epub, Aspose.Words.SaveFormat.Azw3 or Aspose.Words.SaveFormat.Mobi format.
To learn more, visit the Specify Save Options documentation article.
public class HtmlSaveOptions : SaveOptionsInheritance
object ← SaveOptions ← HtmlSaveOptions
Inherited Members
SaveOptions.CreateSaveOptions(SaveFormat) , SaveOptions.CreateSaveOptions(string) , SaveOptions.SaveFormat , SaveOptions.ExportGeneratorName , SaveOptions.TempFolder , SaveOptions.PrettyFormat , SaveOptions.UseAntiAliasing , SaveOptions.UseHighQualityRendering , SaveOptions.DmlRenderingMode , SaveOptions.DmlEffectsRenderingMode , SaveOptions.ImlRenderingMode , SaveOptions.DefaultTemplate , SaveOptions.UpdateFields , SaveOptions.UpdateLastSavedTimeProperty , SaveOptions.UpdateLastPrintedProperty , SaveOptions.UpdateCreatedTimeProperty , SaveOptions.MemoryOptimization , SaveOptions.UpdateAmbiguousTextFont , SaveOptions.Dml3DEffectsRenderingMode , SaveOptions.ProgressCallback , SaveOptions.AllowEmbeddingPostScriptFonts , SaveOptions.CustomTimeZoneInfo , object.GetType() , object.MemberwiseClone() , object.ToString() , object.Equals(object?) , object.Equals(object?, object?) , object.ReferenceEquals(object?, object?) , object.GetHashCode()
Examples
Shows how to specify the folder for storing linked images after saving to .html.
Document doc = new Document(MyDir + "Rendering.docx");
string imagesDir = Path.Combine(ArtifactsDir, "SaveHtmlWithOptions");
if (Directory.Exists(imagesDir))
Directory.Delete(imagesDir, true);
Directory.CreateDirectory(imagesDir);
// Set an option to export form fields as plain text instead of HTML input elements.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
ExportTextInputFormFieldAsText = true,
ImagesFolder = imagesDir
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.SaveHtmlWithOptions.html", options);Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);Shows how to split a document into parts and save them.
public void DocumentPartsFileNames()
{
Document doc = new Document(MyDir + "Rendering.docx");
string outFileName = "SavingCallback.DocumentPartsFileNames.html";
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// If we save the document normally, there will be one output HTML
// document with all the source document's contents.
// Set the "DocumentSplitCriteria" property to "DocumentSplitCriteria.SectionBreak" to
// save our document to multiple HTML files: one for each section.
options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak;
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria);
// If we convert a document that contains images into html, we will end up with one html file which links to several images.
// Each image will be in the form of a file in the local file system.
// There is also a callback that can customize the name and file system location of each image.
options.ImageSavingCallback = new SavedImageRename(outFileName);
doc.Save(ArtifactsDir + outFileName, options);
}
/// <summary>
/// Sets custom filenames for output documents that the saving operation splits a document into.
/// </summary>
private class SavedDocumentPartRename : IDocumentPartSavingCallback
{
public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria)
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
string partType = string.Empty;
switch (mDocumentSplitCriteria)
{
case DocumentSplitCriteria.PageBreak:
partType = "Page";
break;
case DocumentSplitCriteria.ColumnBreak:
partType = "Column";
break;
case DocumentSplitCriteria.SectionBreak:
partType = "Section";
break;
case DocumentSplitCriteria.HeadingParagraph:
partType = "Paragraph from heading";
break;
}
string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output part file:
args.DocumentPartFileName = partFileName;
// 2 - Create a custom stream for the output part file:
args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create);
Assert.That(args.DocumentPartStream.CanWrite, Is.True);
Assert.That(args.KeepDocumentPartStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
private readonly DocumentSplitCriteria mDocumentSplitCriteria;
}
/// <summary>
/// Sets custom filenames for image files that an HTML conversion creates.
/// </summary>
public class SavedImageRename : IImageSavingCallback
{
public SavedImageRename(string outFileName)
{
mOutFileName = outFileName;
}
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output image file:
args.ImageFileName = imageFileName;
// 2 - Create a custom stream for the output image file:
args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create);
Assert.That(args.ImageStream.CanWrite, Is.True);
Assert.That(args.IsImageAvailable, Is.True);
Assert.That(args.KeepImageStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
}Constructors
HtmlSaveOptions()
Initializes a new instance of this class that can be used to save a document in the Aspose.Words.SaveFormat.Html format.
public HtmlSaveOptions()Examples
Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);HtmlSaveOptions(SaveFormat)
Initializes a new instance of this class that can be used to save a document in the Aspose.Words.SaveFormat.Html, Aspose.Words.SaveFormat.Mhtml, Aspose.Words.SaveFormat.Epub, Aspose.Words.SaveFormat.Azw3 or Aspose.Words.SaveFormat.Mobi format.
public HtmlSaveOptions(SaveFormat saveFormat)Parameters
saveFormat SaveFormat
Can be Aspose.Words.SaveFormat.Html, Aspose.Words.SaveFormat.Mhtml, Aspose.Words.SaveFormat.Epub, Aspose.Words.SaveFormat.Azw3 or Aspose.Words.SaveFormat.Mobi.
Examples
Shows how to save a document to a specific version of HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
HtmlVersion = htmlVersion,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.HtmlVersions.html", options);
// Our HTML documents will have minor differences to be compatible with different HTML versions.
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.HtmlVersions.html");
switch (htmlVersion)
{
case HtmlVersion.Html5:
Assert.That(outDocContents.Contains("<a id=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<a id=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<table style=\"padding:0pt; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
break;
case HtmlVersion.Xhtml:
Assert.That(outDocContents.Contains("<a name=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<ul type=\"disc\" style=\"margin:0pt; padding-left:0pt\">"), Is.True);
Assert.That(outDocContents.Contains("<table cellspacing=\"0\" cellpadding=\"0\" style=\"-aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\""), Is.True);
break;
}Properties
AllowNegativeIndent
Specifies whether negative left and right indents of paragraphs are normalized
when saving to HTML, MHTML or EPUB. Default value is false.
public bool AllowNegativeIndent { get; set; }Property Value
Examples
Shows how to preserve negative indents in the output .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table with a negative indent, which will push it to the left past the left page boundary.
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1");
builder.InsertCell();
builder.Write("Row 1, Cell 2");
builder.EndTable();
table.LeftIndent = -36;
table.PreferredWidth = PreferredWidth.FromPoints(144);
builder.InsertBreak(BreakType.ParagraphBreak);
// Insert a table with a positive indent, which will push the table to the right.
table = builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1");
builder.InsertCell();
builder.Write("Row 1, Cell 2");
builder.EndTable();
table.LeftIndent = 36;
table.PreferredWidth = PreferredWidth.FromPoints(144);
// When we save a document to HTML, Aspose.Words will only preserve negative indents
// such as the one we have applied to the first table if we set the "AllowNegativeIndent" flag
// in a SaveOptions object that we will pass to "true".
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
AllowNegativeIndent = allowNegativeIndent,
TableWidthOutputMode = HtmlElementSizeOutputMode.RelativeOnly
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.NegativeIndent.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.NegativeIndent.html");
if (allowNegativeIndent)
{
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:-41.65pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:30.35pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
}
else
{
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:30.35pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
}Remarks
When negative indent is not allowed, it is exported as zero margin to HTML. When negative indent is allowed, a paragraph might appear partially outside of the browser window.
CssClassNamePrefix
Specifies a prefix which is added to all CSS class names. Default value is an empty string and generated CSS class names have no common prefix.
public string CssClassNamePrefix { get; set; }Property Value
Examples
Shows how to save a document to HTML, and add a prefix to all of its CSS class names.
Document doc = new Document(MyDir + "Paragraphs.docx");
HtmlSaveOptions saveOptions = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
CssClassNamePrefix = "myprefix-"
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.CssClassNamePrefix.html", saveOptions);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.CssClassNamePrefix.html");
Assert.That(outDocContents.Contains("<p class=\"myprefix-Header\">"), Is.True);
Assert.That(outDocContents.Contains("<p class=\"myprefix-Footer\">"), Is.True);
outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.CssClassNamePrefix.css");
Assert.That(outDocContents.Contains(".myprefix-Footer { margin-bottom:0pt; line-height:normal; font-family:Arial; font-size:11pt; -aw-style-name:footer }"), Is.True);
Assert.That(outDocContents.Contains(".myprefix-Header { margin-bottom:0pt; line-height:normal; font-family:Arial; font-size:11pt; -aw-style-name:header }"), Is.True);Remarks
If this value is not empty, all CSS classes generated by Aspose.Words will start with the specified prefix. This might be useful, for example, if you add custom CSS to generated documents and want to prevent class name conflicts.
If the value is not null or empty, it must be a valid CSS identifier.
Exceptions
The value is not empty and is not a valid CSS identifier.
CssSavingCallback
Allows to control how CSS styles are saved when a document is saved to HTML, MHTML or EPUB.
public ICssSavingCallback CssSavingCallback { get; set; }Property Value
Examples
Shows how to work with CSS stylesheets that an HTML conversion creates.
public void ExternalCssFilenames()
{
Document doc = new Document(MyDir + "Rendering.docx");
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// Set the "CssStylesheetType" property to "CssStyleSheetType.External" to
// accompany a saved HTML document with an external CSS stylesheet file.
options.CssStyleSheetType = CssStyleSheetType.External;
// Below are two ways of specifying directories and filenames for output CSS stylesheets.
// 1 - Use the "CssStyleSheetFileName" property to assign a filename to our stylesheet:
options.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";
// 2 - Use a custom callback to name our stylesheet:
options.CssSavingCallback =
new CustomCssSavingCallback(ArtifactsDir + "SavingCallback.ExternalCssFilenames.css", true, false);
doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
}
/// <summary>
/// Sets a custom filename, along with other parameters for an external CSS stylesheet.
/// </summary>
private class CustomCssSavingCallback : ICssSavingCallback
{
public CustomCssSavingCallback(string cssDocFilename, bool isExportNeeded, bool keepCssStreamOpen)
{
mCssTextFileName = cssDocFilename;
mIsExportNeeded = isExportNeeded;
mKeepCssStreamOpen = keepCssStreamOpen;
}
public void CssSaving(CssSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
args.IsExportNeeded = mIsExportNeeded;
args.KeepCssStreamOpen = mKeepCssStreamOpen;
Assert.That(args.CssStream.CanWrite, Is.True);
}
private readonly string mCssTextFileName;
private readonly bool mIsExportNeeded;
private readonly bool mKeepCssStreamOpen;
}CssStyleSheetFileName
Specifies the path and the name of the Cascading Style Sheet (CSS) file written when a document is exported to HTML. Default is an empty string.
public string CssStyleSheetFileName { get; set; }Property Value
Examples
Shows how to work with CSS stylesheets that an HTML conversion creates.
public void ExternalCssFilenames()
{
Document doc = new Document(MyDir + "Rendering.docx");
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// Set the "CssStylesheetType" property to "CssStyleSheetType.External" to
// accompany a saved HTML document with an external CSS stylesheet file.
options.CssStyleSheetType = CssStyleSheetType.External;
// Below are two ways of specifying directories and filenames for output CSS stylesheets.
// 1 - Use the "CssStyleSheetFileName" property to assign a filename to our stylesheet:
options.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";
// 2 - Use a custom callback to name our stylesheet:
options.CssSavingCallback =
new CustomCssSavingCallback(ArtifactsDir + "SavingCallback.ExternalCssFilenames.css", true, false);
doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
}
/// <summary>
/// Sets a custom filename, along with other parameters for an external CSS stylesheet.
/// </summary>
private class CustomCssSavingCallback : ICssSavingCallback
{
public CustomCssSavingCallback(string cssDocFilename, bool isExportNeeded, bool keepCssStreamOpen)
{
mCssTextFileName = cssDocFilename;
mIsExportNeeded = isExportNeeded;
mKeepCssStreamOpen = keepCssStreamOpen;
}
public void CssSaving(CssSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
args.IsExportNeeded = mIsExportNeeded;
args.KeepCssStreamOpen = mKeepCssStreamOpen;
Assert.That(args.CssStream.CanWrite, Is.True);
}
private readonly string mCssTextFileName;
private readonly bool mIsExportNeeded;
private readonly bool mKeepCssStreamOpen;
}Remarks
This property has effect only when saving a document to HTML format and external CSS style sheet is requested using Aspose.Words.Saving.HtmlSaveOptions.CssStyleSheetType.
If this property is empty, the CSS file will be saved into the same folder and with the same name as the HTML document but with the ".css" extension.
If only path but no file name is specified in this property, the CSS file will be saved into the specified folder and will have the same name as the HTML document but with the ".css" extension.
If the folder specified by this property doesn't exist, it will be created automatically before the CSS file is saved.
Another way to specify a folder where external CSS file is saved is to use Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias Aspose.Words.Saving.HtmlSaveOptions.CssStyleSheetTypeCssStyleSheetType
Specifies how CSS (Cascading Style Sheet) styles are exported to HTML, MHTML or EPUB. Default value is Aspose.Words.Saving.CssStyleSheetType.Inline for HTML/MHTML and Aspose.Words.Saving.CssStyleSheetType.External for EPUB.
public CssStyleSheetType CssStyleSheetType { get; set; }Property Value
Examples
Shows how to work with CSS stylesheets that an HTML conversion creates.
public void ExternalCssFilenames()
{
Document doc = new Document(MyDir + "Rendering.docx");
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// Set the "CssStylesheetType" property to "CssStyleSheetType.External" to
// accompany a saved HTML document with an external CSS stylesheet file.
options.CssStyleSheetType = CssStyleSheetType.External;
// Below are two ways of specifying directories and filenames for output CSS stylesheets.
// 1 - Use the "CssStyleSheetFileName" property to assign a filename to our stylesheet:
options.CssStyleSheetFileName = ArtifactsDir + "SavingCallback.ExternalCssFilenames.css";
// 2 - Use a custom callback to name our stylesheet:
options.CssSavingCallback =
new CustomCssSavingCallback(ArtifactsDir + "SavingCallback.ExternalCssFilenames.css", true, false);
doc.Save(ArtifactsDir + "SavingCallback.ExternalCssFilenames.html", options);
}
/// <summary>
/// Sets a custom filename, along with other parameters for an external CSS stylesheet.
/// </summary>
private class CustomCssSavingCallback : ICssSavingCallback
{
public CustomCssSavingCallback(string cssDocFilename, bool isExportNeeded, bool keepCssStreamOpen)
{
mCssTextFileName = cssDocFilename;
mIsExportNeeded = isExportNeeded;
mKeepCssStreamOpen = keepCssStreamOpen;
}
public void CssSaving(CssSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
args.CssStream = new FileStream(mCssTextFileName, FileMode.Create);
args.IsExportNeeded = mIsExportNeeded;
args.KeepCssStreamOpen = mKeepCssStreamOpen;
Assert.That(args.CssStream.CanWrite, Is.True);
}
private readonly string mCssTextFileName;
private readonly bool mIsExportNeeded;
private readonly bool mKeepCssStreamOpen;
}Remarks
Saving CSS style sheet into an external file is only supported when saving to HTML. When you are exporting to one of the container formats (EPUB or MHTML) and specifying Aspose.Words.Saving.CssStyleSheetType.External, CSS file will be encapsulated into the output package.
See Also
HtmlSaveOptions . CssStyleSheetFileName
DocumentPartSavingCallback
Allows to control how document parts are saved when a document is saved to HTML or EPUB.
public IDocumentPartSavingCallback DocumentPartSavingCallback { get; set; }Property Value
Examples
Shows how to split a document into parts and save them.
public void DocumentPartsFileNames()
{
Document doc = new Document(MyDir + "Rendering.docx");
string outFileName = "SavingCallback.DocumentPartsFileNames.html";
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// If we save the document normally, there will be one output HTML
// document with all the source document's contents.
// Set the "DocumentSplitCriteria" property to "DocumentSplitCriteria.SectionBreak" to
// save our document to multiple HTML files: one for each section.
options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak;
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria);
// If we convert a document that contains images into html, we will end up with one html file which links to several images.
// Each image will be in the form of a file in the local file system.
// There is also a callback that can customize the name and file system location of each image.
options.ImageSavingCallback = new SavedImageRename(outFileName);
doc.Save(ArtifactsDir + outFileName, options);
}
/// <summary>
/// Sets custom filenames for output documents that the saving operation splits a document into.
/// </summary>
private class SavedDocumentPartRename : IDocumentPartSavingCallback
{
public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria)
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
string partType = string.Empty;
switch (mDocumentSplitCriteria)
{
case DocumentSplitCriteria.PageBreak:
partType = "Page";
break;
case DocumentSplitCriteria.ColumnBreak:
partType = "Column";
break;
case DocumentSplitCriteria.SectionBreak:
partType = "Section";
break;
case DocumentSplitCriteria.HeadingParagraph:
partType = "Paragraph from heading";
break;
}
string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output part file:
args.DocumentPartFileName = partFileName;
// 2 - Create a custom stream for the output part file:
args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create);
Assert.That(args.DocumentPartStream.CanWrite, Is.True);
Assert.That(args.KeepDocumentPartStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
private readonly DocumentSplitCriteria mDocumentSplitCriteria;
}
/// <summary>
/// Sets custom filenames for image files that an HTML conversion creates.
/// </summary>
public class SavedImageRename : IImageSavingCallback
{
public SavedImageRename(string outFileName)
{
mOutFileName = outFileName;
}
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output image file:
args.ImageFileName = imageFileName;
// 2 - Create a custom stream for the output image file:
args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create);
Assert.That(args.ImageStream.CanWrite, Is.True);
Assert.That(args.IsImageAvailable, Is.True);
Assert.That(args.KeepImageStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
}DocumentSplitCriteria
Specifies how the document should be split when saving to Aspose.Words.SaveFormat.Html, Aspose.Words.SaveFormat.Epub or Aspose.Words.SaveFormat.Azw3 format. Default is Aspose.Words.Saving.DocumentSplitCriteria.None for HTML and Aspose.Words.Saving.DocumentSplitCriteria.HeadingParagraph for EPUB and AZW3.
public DocumentSplitCriteria DocumentSplitCriteria { get; set; }Property Value
Examples
Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);Remarks
Normally you would want a document saved to HTML as a single file. But in some cases it is preferable to split the output into several smaller HTML pages. When saving to HTML format these pages will be output to individual files or streams. When saving to EPUB format they will be incorporated into corresponding packages.
A document cannot be split when saving in the MHTML format.
Aspose.Words.Saving.HtmlSaveOptions.DocumentSplitHeadingLevel Aspose.Words.Saving.HtmlSaveOptions.DocumentPartSavingCallbackDocumentSplitHeadingLevel
Specifies the maximum level of headings at which to split the document.
Default value is 2.
public int DocumentSplitHeadingLevel { get; set; }Property Value
Examples
Shows how to split an output HTML document by headings into several parts.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Every paragraph that we format using a "Heading" style can serve as a heading.
// Each heading may also have a heading level, determined by the number of its heading style.
// The headings below are of levels 1-3.
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 1"];
builder.Writeln("Heading #1");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 2"];
builder.Writeln("Heading #2");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("Heading #3");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 1"];
builder.Writeln("Heading #4");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 2"];
builder.Writeln("Heading #5");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("Heading #6");
// Create a HtmlSaveOptions object and set the split criteria to "HeadingParagraph".
// These criteria will split the document at paragraphs with "Heading" styles into several smaller documents,
// and save each document in a separate HTML file in the local file system.
// We will also set the maximum heading level, which splits the document to 2.
// Saving the document will split it at headings of levels 1 and 2, but not at 3 to 9.
HtmlSaveOptions options = new HtmlSaveOptions
{
DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph,
DocumentSplitHeadingLevel = 2
};
// Our document has four headings of levels 1 - 2. One of those headings will not be
// a split point since it is at the beginning of the document.
// The saving operation will split our document at three places, into four smaller documents.
doc.Save(ArtifactsDir + "HtmlSaveOptions.HeadingLevels.html", options);
doc = new Document(ArtifactsDir + "HtmlSaveOptions.HeadingLevels.html");
Assert.That(doc.GetText().Trim(), Is.EqualTo("Heading #1"));
doc = new Document(ArtifactsDir + "HtmlSaveOptions.HeadingLevels-01.html");
Assert.That(doc.GetText().Trim(), Is.EqualTo("Heading #2\r" +
"Heading #3"));
doc = new Document(ArtifactsDir + "HtmlSaveOptions.HeadingLevels-02.html");
Assert.That(doc.GetText().Trim(), Is.EqualTo("Heading #4"));
doc = new Document(ArtifactsDir + "HtmlSaveOptions.HeadingLevels-03.html");
Assert.That(doc.GetText().Trim(), Is.EqualTo("Heading #5\r" +
"Heading #6"));Remarks
When Aspose.Words.Saving.HtmlSaveOptions.DocumentSplitCriteria includes Aspose.Words.Saving.DocumentSplitCriteria.HeadingParagraph and this property is set to a value from 1 to 9, the document will be split at paragraphs formatted using Heading 1, Heading 2 , Heading 3 etc. styles up to the specified heading level.
By default, only Heading 1 and Heading 2 paragraphs cause the document to be split. Setting this property to zero will cause the document not to be split at heading paragraphs at all.
Aspose.Words.Saving.HtmlSaveOptions.DocumentSplitCriteria Aspose.Words.Saving.HtmlSaveOptions.DocumentPartSavingCallbackEncoding
Specifies the encoding to use when exporting to HTML, MHTML or EPUB.
Default value is new UTF8Encoding(false) (UTF-8 without BOM).
public Encoding Encoding { get; set; }Property Value
Examples
Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);ExportCidUrlsForMhtmlResources
Specifies whether to use CID (Content-ID) URLs to reference resources (images, fonts, CSS) included in MHTML
documents. Default value is false.
public bool ExportCidUrlsForMhtmlResources { get; set; }Property Value
Examples
Shows how to enable content IDs for output MHTML documents.
Document doc = new Document(MyDir + "Rendering.docx");
// Setting this flag will replace "Content-Location" tags
// with "Content-ID" tags for each resource from the input document.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Mhtml)
{
ExportCidUrlsForMhtmlResources = exportCidUrlsForMhtmlResources,
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ContentIdUrls.mht", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ContentIdUrls.mht");
if (exportCidUrlsForMhtmlResources)
{
Assert.That(outDocContents.Contains("Content-ID: <document.html>"), Is.True);
Assert.That(outDocContents.Contains("<link href=3D\"cid:styles.css\" type=3D\"text/css\" rel=3D\"stylesheet\" />"), Is.True);
Assert.That(outDocContents.Contains("@font-face { font-family:'Arial Black'; font-weight:bold; src:url('cid:arib=\r\nlk.ttf') }"), Is.True);
Assert.That(outDocContents.Contains("<img src=3D\"cid:image.003.jpeg\" width=3D\"350\" height=3D\"180\" alt=3D\"\" />"), Is.True);
}
else
{
Assert.That(outDocContents.Contains("Content-Location: document.html"), Is.True);
Assert.That(outDocContents.Contains("<link href=3D\"styles.css\" type=3D\"text/css\" rel=3D\"stylesheet\" />"), Is.True);
Assert.That(outDocContents.Contains("@font-face { font-family:'Arial Black'; font-weight:bold; src:url('ariblk.t=\r\ntf') }"), Is.True);
Assert.That(outDocContents.Contains("<img src=3D\"image.003.jpeg\" width=3D\"350\" height=3D\"180\" alt=3D\"\" />"), Is.True);
}Remarks
This option affects only documents being saved to MHTML.
By default, resources in MHTML documents are referenced by file name (for example, "image.png"), which are matched against "Content-Location" headers of MIME parts.
This option enables an alternative method, where references to resource files are written as CID (Content-ID) URLs (for example, "cid:image.png") and are matched against "Content-ID" headers.
In theory, there should be no difference between the two referencing methods and either of them should work fine in any browser or mail agent. In practice, however, some agents fail to fetch resources by file name. If your browser or mail agent refuses to load resources included in an MTHML document (doesn't show images or doesn't load CSS styles), try exporting the document with CID URLs.
ExportDocumentProperties
Specifies whether to export built-in and custom document properties to HTML, MHTML or EPUB.
Default value is false.
public bool ExportDocumentProperties { get; set; }Property Value
Examples
Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);ExportDropDownFormFieldAsText
Controls how drop-down form fields are saved to HTML or MHTML.
Default value is false.
public bool ExportDropDownFormFieldAsText { get; set; }Property Value
Examples
Shows how to get drop-down combo box form fields to blend in with paragraph text when saving to html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a document builder to insert a combo box with the value "Two" selected.
builder.InsertComboBox("MyComboBox", new[] { "One", "Two", "Three" }, 1);
// The "ExportDropDownFormFieldAsText" flag of this SaveOptions object allows us to
// control how saving the document to HTML treats drop-down combo boxes.
// Setting it to "true" will convert each combo box into simple text
// that displays the combo box's currently selected value, effectively freezing it.
// Setting it to "false" will preserve the functionality of the combo box using <select> and <option> tags.
HtmlSaveOptions options = new HtmlSaveOptions();
options.ExportDropDownFormFieldAsText = exportDropDownFormFieldAsText;
doc.Save(ArtifactsDir + "HtmlSaveOptions.DropDownFormField.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.DropDownFormField.html");
if (exportDropDownFormFieldAsText)
Assert.That(outDocContents.Contains(
"<span>Two</span>"), Is.True);
else
Assert.That(outDocContents.Contains(
"<select name=\"MyComboBox\">" +
"<option>One</option>" +
"<option selected=\"selected\">Two</option>" +
"<option>Three</option>" +
"</select>"), Is.True);Remarks
When set to true, exports drop-down form fields as normal text.
When false, exports drop-down form fields as SELECT element in HTML.
When exporting to EPUB, text drop-down form fields are always saved as text due to requirements of this format.
ExportFontResources
Specifies whether font resources should be exported to HTML, MHTML or EPUB.
Default is false.
public bool ExportFontResources { get; set; }Property Value
Examples
Shows how to define custom logic for exporting fonts when saving to HTML.
public void SaveExportedFonts()
{
Document doc = new Document(MyDir + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportFontResources = true,
FontSavingCallback = new HandleFontSaving()
};
// The callback will export .ttf files and save them alongside the output document.
doc.Save(ArtifactsDir + "HtmlSaveOptions.SaveExportedFonts.html", options);
foreach (string fontFilename in Array.FindAll(Directory.GetFiles(ArtifactsDir), s => s.EndsWith(".ttf")))
Console.WriteLine(fontFilename);
}
/// <summary>
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
/// </summary>
public class HandleFontSaving : IFontSavingCallback
{
void IFontSavingCallback.FontSaving(FontSavingArgs args)
{
Console.Write($"Font:\t{args.FontFamilyName}");
if (args.Bold) Console.Write(", bold");
if (args.Italic) Console.Write(", italic");
Console.WriteLine($"\nSource:\t{args.OriginalFileName}, {args.OriginalFileSize} bytes\n");
// We can also access the source document from here.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
Assert.That(args.IsExportNeeded, Is.True);
Assert.That(args.IsSubsettingNeeded, Is.True);
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.FontFileName = args.OriginalFileName.Split(Path.DirectorySeparatorChar).Last();
// 2 - Save it to a stream:
args.FontStream =
new FileStream(ArtifactsDir + args.OriginalFileName.Split(Path.DirectorySeparatorChar).Last(), FileMode.Create);
Assert.That(args.KeepFontStreamOpen, Is.False);
}
}Remarks
<p>Exporting font resources allows for consistent document rendering independent of the fonts available
in a given user's environment.</p>
<p>If Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources is set to <code>true</code>, main HTML document will refer to every font via
the CSS 3 <b>@font-face</b> at-rule and fonts will be output as separate files. When exporting to IDPF EPUB or MHTML
formats, fonts will be embedded into the corresponding package along with other subsidiary files.</p>
<p>If Aspose.Words.Saving.HtmlSaveOptions.ExportFontsAsBase64 is set to <code>true</code>, fonts will not be saved to separate files.
Instead, they will be embedded into <b>@font-face</b> at-rules in Base64 encoding.</p>
<p>
<b>Important!</b> When exporting font resources, font licensing issues should be considered. Authors who want to use specific fonts via a downloadable
font mechanism must always carefully verify that their intended use is within the scope of the font license. Many commercial fonts presently do not allow web downloading of their fonts in any form. License agreements that cover some fonts specifically note that usage via @font-face rules in CSS style sheets is not allowed. Font subsetting can also violate license terms.
Aspose.Words.Saving.HtmlSaveOptions.FontResourcesSubsettingSizeThresholdExportFontsAsBase64
Specifies whether fonts resources should be embedded to HTML in Base64 encoding.
Default is false.
public bool ExportFontsAsBase64 { get; set; }Property Value
Examples
Shows how to embed fonts inside a saved HTML document.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportFontsAsBase64 = true,
CssStyleSheetType = CssStyleSheetType.Embedded,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportFontsAsBase64.html", options);Shows how to save a .html document with images embedded inside it.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportImagesAsBase64 = exportImagesAsBase64,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportImagesAsBase64.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportImagesAsBase64.html");
Assert.That(exportImagesAsBase64
? outDocContents.Contains("<img src=\"data:image/png;base64")
: outDocContents.Contains("<img src=\"HtmlSaveOptions.ExportImagesAsBase64.001.png\""), Is.True);Remarks
By default, fonts are written to separate files. If this option is set to true, fonts will be embedded
into the document's CSS in Base64 encoding.
ExportHeadersFootersMode
Specifies how headers and footers are output to HTML, MHTML or EPUB. Default value is Aspose.Words.Saving.ExportHeadersFootersMode.PerSection for HTML/MHTML and Aspose.Words.Saving.ExportHeadersFootersMode.None for EPUB.
public ExportHeadersFootersMode ExportHeadersFootersMode { get; set; }Property Value
Examples
Shows how to omit headers/footers when saving a document to HTML.
Document doc = new Document(MyDir + "Header and footer types.docx");
// This document contains headers and footers. We can access them via the "HeadersFooters" collection.
Assert.That(doc.FirstSection.HeadersFooters[HeaderFooterType.HeaderFirst].GetText().Trim(), Is.EqualTo("First header"));
// Formats such as .html do not split the document into pages, so headers/footers will not function the same way
// they would when we open the document as a .docx using Microsoft Word.
// If we convert a document with headers/footers to html, the conversion will assimilate the headers/footers into body text.
// We can use a SaveOptions object to omit headers/footers while converting to html.
HtmlSaveOptions saveOptions =
new HtmlSaveOptions(SaveFormat.Html) { ExportHeadersFootersMode = ExportHeadersFootersMode.None };
doc.Save(ArtifactsDir + "HeaderFooter.ExportMode.html", saveOptions);
// Open our saved document and verify that it does not contain the header's text
doc = new Document(ArtifactsDir + "HeaderFooter.ExportMode.html");
Assert.That(doc.Range.Text.Contains("First header"), Is.False);Remarks
It is hard to meaningfully output headers and footers to HTML because HTML is not paginated.
When this property is Aspose.Words.Saving.ExportHeadersFootersMode.PerSection, Aspose.Words exports only primary headers and footers at the beginning and the end of each section.
When it is Aspose.Words.Saving.ExportHeadersFootersMode.FirstSectionHeaderLastSectionFooter only first primary header and the last primary footer (including linked to previous) are exported.
You can disable export of headers and footers altogether by setting this property to Aspose.Words.Saving.ExportHeadersFootersMode.None.
ExportImagesAsBase64
Specifies whether images are saved in Base64 format to the output HTML, MHTML or EPUB.
Default is false.
public bool ExportImagesAsBase64 { get; set; }Property Value
Examples
Shows how to embed fonts inside a saved HTML document.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportFontsAsBase64 = true,
CssStyleSheetType = CssStyleSheetType.Embedded,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportFontsAsBase64.html", options);Shows how to save a .html document with images embedded inside it.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportImagesAsBase64 = exportImagesAsBase64,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportImagesAsBase64.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportImagesAsBase64.html");
Assert.That(exportImagesAsBase64
? outDocContents.Contains("<img src=\"data:image/png;base64")
: outDocContents.Contains("<img src=\"HtmlSaveOptions.ExportImagesAsBase64.001.png\""), Is.True);Remarks
When this property is set to true images data are exported
directly into the img elements and separate files are not created.
ExportLanguageInformation
Specifies whether language information is exported to HTML, MHTML or EPUB.
Default is false.
public bool ExportLanguageInformation { get; set; }Property Value
Examples
Shows how to preserve language information when saving to .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use the builder to write text while formatting it in different locales.
builder.Font.LocaleId = new CultureInfo("en-US").LCID;
builder.Writeln("Hello world!");
builder.Font.LocaleId = new CultureInfo("en-GB").LCID;
builder.Writeln("Hello again!");
builder.Font.LocaleId = new CultureInfo("ru-RU").LCID;
builder.Write("Привет, мир!");
// When saving the document to HTML, we can pass a SaveOptions object
// to either preserve or discard each formatted text's locale.
// If we set the "ExportLanguageInformation" flag to "true",
// the output HTML document will contain the locales in "lang" attributes of <span> tags.
// If we set the "ExportLanguageInformation" flag to "false',
// the text in the output HTML document will not contain any locale information.
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportLanguageInformation = exportLanguageInformation,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportLanguageInformation.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportLanguageInformation.html");
if (exportLanguageInformation)
{
Assert.That(outDocContents.Contains("<span>Hello world!</span>"), Is.True);
Assert.That(outDocContents.Contains("<span lang=\"en-GB\">Hello again!</span>"), Is.True);
Assert.That(outDocContents.Contains("<span lang=\"ru-RU\">Привет, мир!</span>"), Is.True);
}
else
{
Assert.That(outDocContents.Contains("<span>Hello world!</span>"), Is.True);
Assert.That(outDocContents.Contains("<span>Hello again!</span>"), Is.True);
Assert.That(outDocContents.Contains("<span>Привет, мир!</span>"), Is.True);
}Remarks
When this property is set to true Aspose.Words outputs lang HTML attribute on the document
elements that specify language. This can be needed to preserve language related semantics.
ExportListLabels
Controls how list labels are output to HTML, MHTML or EPUB. Default value is Aspose.Words.Saving.ExportListLabels.Auto.
public ExportListLabels ExportListLabels { get; set; }Property Value
Examples
Shows how to configure list exporting to HTML.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Aspose.Words.Lists.List docList = doc.Lists.Add(ListTemplate.NumberDefault);
builder.ListFormat.List = docList;
builder.Writeln("Default numbered list item 1.");
builder.Writeln("Default numbered list item 2.");
builder.ListFormat.ListIndent();
builder.Writeln("Default numbered list item 3.");
builder.ListFormat.RemoveNumbers();
docList = doc.Lists.Add(ListTemplate.OutlineHeadingsLegal);
builder.ListFormat.List = docList;
builder.Writeln("Outline legal heading list item 1.");
builder.Writeln("Outline legal heading list item 2.");
builder.ListFormat.ListIndent();
builder.Writeln("Outline legal heading list item 3.");
builder.ListFormat.ListIndent();
builder.Writeln("Outline legal heading list item 4.");
builder.ListFormat.ListIndent();
builder.Writeln("Outline legal heading list item 5.");
builder.ListFormat.RemoveNumbers();
// When saving the document to HTML, we can pass a SaveOptions object
// to decide which HTML elements the document will use to represent lists.
// Setting the "ExportListLabels" property to "ExportListLabels.AsInlineText"
// will create lists by formatting spans.
// Setting the "ExportListLabels" property to "ExportListLabels.Auto" will use the <p> tag
// to build lists in cases when using the <ol> and <li> tags may cause loss of formatting.
// Setting the "ExportListLabels" property to "ExportListLabels.ByHtmlTags"
// will use <ol> and <li> tags to build all lists.
HtmlSaveOptions options = new HtmlSaveOptions { ExportListLabels = exportListLabels };
doc.Save(ArtifactsDir + "HtmlSaveOptions.List.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.List.html");
switch (exportListLabels)
{
case ExportListLabels.AsInlineText:
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-left:72pt; margin-bottom:0pt; text-indent:-18pt; -aw-import:list-item; -aw-list-level-number:1; -aw-list-number-format:'%1.'; -aw-list-number-styles:'lowerLetter'; -aw-list-number-values:'1'; -aw-list-padding-sml:9.67pt\">" +
"<span style=\"-aw-import:ignore\">" +
"<span>a.</span>" +
"<span style=\"width:9.67pt; font:7pt 'Times New Roman'; display:inline-block; -aw-import:spaces\">       </span>" +
"</span>" +
"<span>Default numbered list item 3.</span>" +
"</p>"), Is.True);
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-left:43.2pt; margin-bottom:0pt; text-indent:-43.2pt; -aw-import:list-item; -aw-list-level-number:3; -aw-list-number-format:'%0.%1.%2.%3'; -aw-list-number-styles:'decimal decimal decimal decimal'; -aw-list-number-values:'2 1 1 1'; -aw-list-padding-sml:10.2pt\">" +
"<span style=\"-aw-import:ignore\">" +
"<span>2.1.1.1</span>" +
"<span style=\"width:10.2pt; font:7pt 'Times New Roman'; display:inline-block; -aw-import:spaces\">       </span>" +
"</span>" +
"<span>Outline legal heading list item 5.</span>" +
"</p>"), Is.True);
break;
case ExportListLabels.Auto:
Assert.That(outDocContents.Contains(
"<ol type=\"a\" style=\"margin-right:0pt; margin-left:0pt; padding-left:0pt\">" +
"<li style=\"margin-left:31.33pt; padding-left:4.67pt\">" +
"<span>Default numbered list item 3.</span>" +
"</li>" +
"</ol>"), Is.True);
break;
case ExportListLabels.ByHtmlTags:
Assert.That(outDocContents.Contains(
"<ol type=\"a\" style=\"margin-right:0pt; margin-left:0pt; padding-left:0pt\">" +
"<li style=\"margin-left:31.33pt; padding-left:4.67pt\">" +
"<span>Default numbered list item 3.</span>" +
"</li>" +
"</ol>"), Is.True);
break;
}ExportOriginalUrlForLinkedImages
Specifies whether original URL should be used as the URL of the linked images.
Default value is false.
public bool ExportOriginalUrlForLinkedImages { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
If value is set to trueAspose.Words.Drawing.ImageData.SourceFullName value is used
as the URL of linked images and linked images are not loaded into document's folder
or Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder.
If value is set to false linked images are loaded into document's folder
or Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder and URL of each linked image is constructed depending
on document's folder, Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder
and Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias properties.
ExportPageMargins
Specifies whether page margins is exported to HTML, MHTML or EPUB.
Default is false.
public bool ExportPageMargins { get; set; }Property Value
Examples
Shows how to show out-of-bounds objects in output HTML documents.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Use a builder to insert a shape with no wrapping.
Shape shape = builder.InsertShape(ShapeType.Cube, 200, 200);
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page;
shape.WrapType = WrapType.None;
// Negative shape position values may place the shape outside of page boundaries.
// If we export this to HTML, the shape will appear truncated.
shape.Left = -150;
// When saving the document to HTML, we can pass a SaveOptions object
// to decide whether to adjust the page to display out-of-bounds objects fully.
// If we set the "ExportPageMargins" flag to "true", the shape will be fully visible in the output HTML.
// If we set the "ExportPageMargins" flag to "false",
// our document will display the shape truncated as we would see it in Microsoft Word.
HtmlSaveOptions options = new HtmlSaveOptions { ExportPageMargins = exportPageMargins };
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportPageMargins.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportPageMargins.html");
if (exportPageMargins)
{
Assert.That(outDocContents.Contains("<style type=\"text/css\">div.Section_1 { margin:70.85pt }</style>"), Is.True);
Assert.That(outDocContents.Contains("<div class=\"Section_1\"><p style=\"margin-top:0pt; margin-left:150pt; margin-bottom:0pt\">"), Is.True);
}
else
{
Assert.That(outDocContents.Contains("style type=\"text/css\">"), Is.False);
Assert.That(outDocContents.Contains("<div><p style=\"margin-top:0pt; margin-left:220.85pt; margin-bottom:0pt\">"), Is.True);
}Remarks
Aspose.Words does not show area of page margins by default. If any elements are completely or partially clipped by the document edge the displayed area can be extended with this option.
ExportPageSetup
Specifies whether page setup is exported to HTML, MHTML or EPUB.
Default is false.
public bool ExportPageSetup { get; set; }Property Value
Examples
Shows how decide whether to preserve section structure/page setup information when saving to HTML.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Write("Section 1");
builder.InsertBreak(BreakType.SectionBreakNewPage);
builder.Write("Section 2");
PageSetup pageSetup = doc.Sections[0].PageSetup;
pageSetup.TopMargin = 36.0;
pageSetup.BottomMargin = 36.0;
pageSetup.PaperSize = PaperSize.A5;
// When saving the document to HTML, we can pass a SaveOptions object
// to decide whether to preserve or discard page setup settings.
// If we set the "ExportPageSetup" flag to "true", the output HTML document will contain our page setup configuration.
// If we set the "ExportPageSetup" flag to "false", the save operation will discard our page setup settings
// for the first section, and both sections will look identical.
HtmlSaveOptions options = new HtmlSaveOptions { ExportPageSetup = exportPageSetup };
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportPageSetup.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportPageSetup.html");
if (exportPageSetup)
{
Assert.That(outDocContents.Contains(
"<style type=\"text/css\">" +
"@page Section_1 { size:419.55pt 595.3pt; margin:36pt 70.85pt; -aw-footer-distance:35.4pt; -aw-header-distance:35.4pt }" +
"@page Section_2 { size:612pt 792pt; margin:70.85pt; -aw-footer-distance:35.4pt; -aw-header-distance:35.4pt }" +
"div.Section_1 { page:Section_1 }div.Section_2 { page:Section_2 }" +
"</style>"), Is.True);
Assert.That(outDocContents.Contains(
"<div class=\"Section_1\">" +
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<span>Section 1</span>" +
"</p>" +
"</div>"), Is.True);
}
else
{
Assert.That(outDocContents.Contains("style type=\"text/css\">"), Is.False);
Assert.That(outDocContents.Contains(
"<div>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<span>Section 1</span>" +
"</p>" +
"</div>"), Is.True);
}Remarks
Each Aspose.Words.Section in Aspose.Words document model provides page setup information via Aspose.Words.PageSetup class. When you export a document to HTML format you might need to keep this information for further usage. In particular, page setup might be important for rendering to paged media (printing) or subsequent conversion to the native Microsoft Word file formats (DOCX, DOC, RTF, WML).
In most cases HTML is intended for viewing in browsers where pagination is not performed. So this feature is inactive by default.
ExportRelativeFontSize
Specifies whether font sizes should be output in relative units when saving to HTML, MHTML or EPUB.
Default is false.
public bool ExportRelativeFontSize { get; set; }Property Value
Examples
Shows how to use relative font sizes when saving to .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Default font size, ");
builder.Font.Size = 24;
builder.Writeln("2x default font size,");
builder.Font.Size = 96;
builder.Write("8x default font size");
// When we save the document to HTML, we can pass a SaveOptions object
// to determine whether to use relative or absolute font sizes.
// Set the "ExportRelativeFontSize" flag to "true" to declare font sizes
// using the "em" measurement unit, which is a factor that multiplies the current font size.
// Set the "ExportRelativeFontSize" flag to "false" to declare font sizes
// using the "pt" measurement unit, which is the font's absolute size in points.
HtmlSaveOptions options = new HtmlSaveOptions { ExportRelativeFontSize = exportRelativeFontSize };
doc.Save(ArtifactsDir + "HtmlSaveOptions.RelativeFontSize.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.RelativeFontSize.html");
if (exportRelativeFontSize)
{
Assert.That(outDocContents.Contains(
"<body style=\"font-family:'Times New Roman'\">" +
"<div>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<span>Default font size, </span>" +
"</p>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt; font-size:2em\">" +
"<span>2x default font size,</span>" +
"</p>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt; font-size:8em\">" +
"<span>8x default font size</span>" +
"</p>" +
"</div>" +
"</body>"), Is.True);
}
else
{
Assert.That(outDocContents.Contains(
"<body style=\"font-family:'Times New Roman'; font-size:12pt\">" +
"<div>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<span>Default font size, </span>" +
"</p>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt; font-size:24pt\">" +
"<span>2x default font size,</span>" +
"</p>" +
"<p style=\"margin-top:0pt; margin-bottom:0pt; font-size:96pt\">" +
"<span>8x default font size</span>" +
"</p>" +
"</div>" +
"</body>"), Is.True);
}Remarks
In many existing documents (HTML, IDPF EPUB) font sizes are specified in relative units. This allows
applications to adjust text size when viewing/processing documents. For instance, Microsoft Internet Explorer
has "View->Text Size" submenu, Adobe Digital Editions has two buttons: Increase/Decrease Text Size.
If you expect this functionality to work then set Aspose.Words.Saving.HtmlSaveOptions.ExportRelativeFontSize property to true.
Aspose Words document model contains and operates only with absolute font size units. Relative units need additional logic to be recalculated from some initial (standard) size. Font size of Normal document style is taken as standard. For instance, if Normal has 12pt font and some text is 18pt then it will be output as 1.5em. to the HTML.
When this option is enabled, document elements other than text will still have absolute sizes. Also some
text-related attributes might be expressed absolutely. In particular, line spacing specified with "exactly" rule
might produce unwanted results when scaling text. So the source documents should be properly designed and tested
when exporting with Aspose.Words.Saving.HtmlSaveOptions.ExportRelativeFontSize set to true.
ExportRoundtripInformation
Specifies whether to write the roundtrip information when saving to HTML, MHTML or EPUB.
Default value is true for HTML and false for MHTML and EPUB.
public bool ExportRoundtripInformation { get; set; }Property Value
Examples
Shows how to preserve hidden elements when converting to .html.
Document doc = new Document(MyDir + "Rendering.docx");
// When converting a document to .html, some elements such as hidden bookmarks, original shape positions,
// or footnotes will be either removed or converted to plain text and effectively be lost.
// Saving with a HtmlSaveOptions object with ExportRoundtripInformation set to true will preserve these elements.
// When we save the document to HTML, we can pass a SaveOptions object to determine
// how the saving operation will export document elements that HTML does not support or use,
// such as hidden bookmarks and original shape positions.
// If we set the "ExportRoundtripInformation" flag to "true", the save operation will preserve these elements.
// If we set the "ExportRoundTripInformation" flag to "false", the save operation will discard these elements.
// We will want to preserve such elements if we intend to load the saved HTML using Aspose.Words,
// as they could be of use once again.
HtmlSaveOptions options = new HtmlSaveOptions { ExportRoundtripInformation = exportRoundtripInformation };
doc.Save(ArtifactsDir + "HtmlSaveOptions.RoundTripInformation.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.RoundTripInformation.html");
doc = new Document(ArtifactsDir + "HtmlSaveOptions.RoundTripInformation.html");
if (exportRoundtripInformation)
{
Assert.That(outDocContents.Contains("<div style=\"-aw-headerfooter-type:header-primary; clear:both\">"), Is.True);
Assert.That(outDocContents.Contains("<span style=\"-aw-import:ignore\"> </span>"), Is.True);
Assert.That(outDocContents.Contains(
"td colspan=\"2\" style=\"width:210.6pt; border-style:solid; border-width:0.75pt 6pt 0.75pt 0.75pt; " +
"padding-right:2.4pt; padding-left:5.03pt; vertical-align:top; -aw-border-bottom:0.5pt single #000000; " +
"-aw-border-left:0.5pt single #000000; -aw-border-right:6pt single #000000; -aw-border-top:0.5pt single #000000\">"), Is.True);
Assert.That(outDocContents.Contains(
"<li style=\"margin-left:30.2pt; padding-left:5.8pt; -aw-font-family:'Courier New'; -aw-font-weight:normal; -aw-number-format:'o'\">"), Is.True);
Assert.That(outDocContents.Contains(
"<img src=\"HtmlSaveOptions.RoundTripInformation.003.jpeg\" width=\"350\" height=\"180\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />"), Is.True);
Assert.That(outDocContents.Contains(
"<span>Page number </span>" +
"<span style=\"-aw-field-start:true\"></span>" +
"<span style=\"-aw-field-code:' PAGE \\\\* MERGEFORMAT '\"></span>" +
"<span style=\"-aw-field-separator:true\"></span>" +
"<span>1</span>" +
"<span style=\"-aw-field-end:true\"></span>"), Is.True);
Assert.That(doc.Range.Fields.Count(f => f.Type == FieldType.FieldPage), Is.EqualTo(1));
}
else
{
Assert.That(outDocContents.Contains("<div style=\"clear:both\">"), Is.True);
Assert.That(outDocContents.Contains("<span> </span>"), Is.True);
Assert.That(outDocContents.Contains(
"<td colspan=\"2\" style=\"width:210.6pt; border-style:solid; border-width:0.75pt 6pt 0.75pt 0.75pt; " +
"padding-right:2.4pt; padding-left:5.03pt; vertical-align:top\">"), Is.True);
Assert.That(outDocContents.Contains(
"<li style=\"margin-left:30.2pt; padding-left:5.8pt\">"), Is.True);
Assert.That(outDocContents.Contains(
"<img src=\"HtmlSaveOptions.RoundTripInformation.003.jpeg\" width=\"350\" height=\"180\" alt=\"\" />"), Is.True);
Assert.That(outDocContents.Contains(
"<span>Page number 1</span>"), Is.True);
Assert.That(doc.Range.Fields.Count(f => f.Type == FieldType.FieldPage), Is.EqualTo(0));
}Remarks
Saving of the roundtrip information allows to restore document properties such as tab stops, comments, headers and footers during the HTML documents loading back into a Aspose.Words.Document object.
When true, the roundtrip information is exported as -aw-* CSS properties
of the corresponding HTML elements.
When false, causes no roundtrip information to be output into produced files.
ExportShapesAsSvg
Controls whether Aspose.Words.Drawing.Shape nodes are converted to SVG images when saving
to HTML, MHTML, EPUB or AZW3.
Default value is false.
public bool ExportShapesAsSvg { get; set; }Property Value
Examples
Shows how to export shape as scalable vector graphics.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Shape textBox = builder.InsertShape(ShapeType.TextBox, 100.0, 60.0);
builder.MoveTo(textBox.FirstParagraph);
builder.Write("My text box");
// When we save the document to HTML, we can pass a SaveOptions object
// to determine how the saving operation will export text box shapes.
// If we set the "ExportTextBoxAsSvg" flag to "true",
// the save operation will convert shapes with text into SVG objects.
// If we set the "ExportTextBoxAsSvg" flag to "false",
// the save operation will convert shapes with text into images.
HtmlSaveOptions options = new HtmlSaveOptions { ExportShapesAsSvg = exportShapesAsSvg };
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportTextBox.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportTextBox.html");
if (exportShapesAsSvg)
{
Assert.That(outDocContents.Contains(
"<span style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\">" +
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"133\" height=\"80\">"), Is.True);
}
else
{
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.ExportTextBox.001.png\" width=\"136\" height=\"83\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"), Is.True);
}Remarks
If this option is set to true, Aspose.Words.Drawing.Shape nodes are exported as <svg> elements.
Otherwise, they are rendered to bitmaps and are exported as <img> elements.
ExportTextInputFormFieldAsText
Controls how text input form fields are saved to HTML or MHTML.
Default value is false.
public bool ExportTextInputFormFieldAsText { get; set; }Property Value
Examples
Shows how to specify the folder for storing linked images after saving to .html.
Document doc = new Document(MyDir + "Rendering.docx");
string imagesDir = Path.Combine(ArtifactsDir, "SaveHtmlWithOptions");
if (Directory.Exists(imagesDir))
Directory.Delete(imagesDir, true);
Directory.CreateDirectory(imagesDir);
// Set an option to export form fields as plain text instead of HTML input elements.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
ExportTextInputFormFieldAsText = true,
ImagesFolder = imagesDir
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.SaveHtmlWithOptions.html", options);Remarks
When set to true, exports text input form fields as normal text.
When false, exports Word text input form fields as INPUT elements in HTML.
When exporting to EPUB, text input form fields are always saved as text due to requirements of this format.
ExportTocPageNumbers
Specifies whether to write page numbers to table of contents when saving HTML, MHTML and EPUB.
Default value is false.
public bool ExportTocPageNumbers { get; set; }Property Value
Examples
Shows how to display page numbers when saving a document with a table of contents to .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table of contents, and then populate the document with paragraphs formatted using a "Heading"
// style that the table of contents will pick up as entries. Each entry will display the heading paragraph on the left,
// and the page number that contains the heading on the right.
FieldToc fieldToc = (FieldToc)builder.InsertField(FieldType.FieldTOC, true);
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 1"];
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Entry 1");
builder.Writeln("Entry 2");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Entry 3");
builder.InsertBreak(BreakType.PageBreak);
builder.Writeln("Entry 4");
fieldToc.UpdatePageNumbers();
doc.UpdateFields();
// HTML documents do not have pages. If we save this document to HTML,
// the page numbers that our TOC displays will have no meaning.
// When we save the document to HTML, we can pass a SaveOptions object to omit these page numbers from the TOC.
// If we set the "ExportTocPageNumbers" flag to "true",
// each TOC entry will display the heading, separator, and page number, preserving its appearance in Microsoft Word.
// If we set the "ExportTocPageNumbers" flag to "false",
// the save operation will omit both the separator and page number and leave the heading for each entry intact.
HtmlSaveOptions options = new HtmlSaveOptions { ExportTocPageNumbers = exportTocPageNumbers };
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportTocPageNumbers.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportTocPageNumbers.html");
if (exportTocPageNumbers)
{
Assert.That(outDocContents.Contains(
"<span>Entry 1</span>" +
"<span style=\"width:428.14pt; font-family:'Lucida Console'; font-size:10pt; display:inline-block; -aw-font-family:'Times New Roman'; " +
"-aw-tabstop-align:right; -aw-tabstop-leader:dots; -aw-tabstop-pos:469.8pt\">.......................................................................</span>" +
"<span>2</span>" +
"</p>"), Is.True);
}
else
{
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<span>Entry 2</span>" +
"</p>"), Is.True);
}ExportXhtmlTransitional
Specifies whether to write the DOCTYPE declaration when saving to HTML or MHTML.
When true, writes a DOCTYPE declaration in the document prior to the root element.
Default value is false.
When saving to EPUB or HTML5 (Aspose.Words.Saving.HtmlVersion.Html5) the DOCTYPE
declaration is always written.
public bool ExportXhtmlTransitional { get; set; }Property Value
Examples
Shows how to display a DOCTYPE heading when converting documents to the Xhtml 1.0 transitional standard.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
HtmlVersion = HtmlVersion.Xhtml,
ExportXhtmlTransitional = showDoctypeDeclaration,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportXhtmlTransitional.html", options);
// Our document will only contain a DOCTYPE declaration heading if we have set the "ExportXhtmlTransitional" flag to "true".
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportXhtmlTransitional.html");
string newLine = Environment.NewLine;
if (showDoctypeDeclaration)
Assert.That(outDocContents.Contains(
$"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>{newLine}" +
$"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">{newLine}" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"), Is.True);
else
Assert.That(outDocContents.Contains("<html>"), Is.True);Remarks
Aspose.Words always writes well formed HTML regardless of this setting.
When true, the beginning of the HTML output document will look like this:
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">Aspose.Words aims to output XHTML according to the XHTML 1.0 Transitional specification, but the output will not always validate against the DTD. Some structures inside a Microsoft Word document are hard or impossible to map to a document that will validate against the XHTML schema. For example, XHTML does not allow nested lists (UL cannot be nested inside another UL element), but in Microsoft Word document multilevel lists occur quite often.
FontResourcesSubsettingSizeThreshold
Controls which font resources need subsetting when saving to HTML, MHTML or EPUB.
Default is 0.
public int FontResourcesSubsettingSizeThreshold { get; set; }Property Value
Examples
Shows how to work with font subsetting.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Font.Name = "Arial";
builder.Writeln("Hello world!");
builder.Font.Name = "Times New Roman";
builder.Writeln("Hello world!");
builder.Font.Name = "Courier New";
builder.Writeln("Hello world!");
// When we save the document to HTML, we can pass a SaveOptions object configure font subsetting.
// Suppose we set the "ExportFontResources" flag to "true" and also name a folder in the "FontsFolder" property.
// In that case, the saving operation will create that folder and place a .ttf file inside
// that folder for each font that our document uses.
// Each .ttf file will contain that font's entire glyph set,
// which may potentially result in a very large file that accompanies the document.
// When we apply subsetting to a font, its exported raw data will only contain the glyphs that the document is
// using instead of the entire glyph set. If the text in our document only uses a small fraction of a font's
// glyph set, then subsetting will significantly reduce our output documents' size.
// We can use the "FontResourcesSubsettingSizeThreshold" property to define a .ttf file size, in bytes.
// If an exported font creates a size bigger file than that, then the save operation will apply subsetting to that font.
// Setting a threshold of 0 applies subsetting to all fonts,
// and setting it to "int.MaxValue" effectively disables subsetting.
string fontsFolder = ArtifactsDir + "HtmlSaveOptions.FontSubsetting.Fonts";
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportFontResources = true,
FontsFolder = fontsFolder,
FontResourcesSubsettingSizeThreshold = fontResourcesSubsettingSizeThreshold
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FontSubsetting.html", options);
string[] fontFileNames = Directory.GetFiles(fontsFolder).Where(s => s.EndsWith(".ttf")).ToArray();
Assert.That(fontFileNames.Length, Is.EqualTo(3));
foreach (string filename in fontFileNames)
{
// By default, the .ttf files for each of our three fonts will be over 700MB.
// Subsetting will reduce them all to under 30MB.
FileInfo fontFileInfo = new FileInfo(filename);
Assert.That(fontFileInfo.Length > 700000 || fontFileInfo.Length < 30000, Is.True);
Assert.That(System.Math.Max(fontResourcesSubsettingSizeThreshold, 30000) > new FileInfo(filename).Length, Is.True);
}Remarks
<p>
Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources allows exporting fonts as subsidiary files or as parts of the output
package. If the document uses many fonts, especially with large number of glyphs, then output size can grow
significantly. Font subsetting reduces the size of the exported font resource by filtering out glyphs that
are not used by the current document.</p>
<p>Font subsetting works as follows:</p>
<ul><li>By default, all exported fonts are subsetted.</li><li>Setting Aspose.Words.Saving.HtmlSaveOptions.FontResourcesSubsettingSizeThreshold to a positive value
instructs Aspose.Words to subset fonts which file size is larger than the specified value.</li><li>Setting the property to System.Int32.MaxValue
suppresses font subsetting.</li></ul>
<p>
<b>Important!</b> When exporting font resources, font licensing issues should be considered. Authors who want to use specific fonts via a downloadable
font mechanism must always carefully verify that their intended use is within the scope of the font license. Many commercial fonts presently do not allow web downloading of their fonts in any form. License agreements that cover some fonts specifically note that usage via @font-face rules in CSS style sheets is not allowed. Font subsetting can also violate license terms.
Aspose.Words.Saving.HtmlSaveOptions.ExportFontResourcesFontSavingCallback
Allows to control how fonts are saved when a document is saved to HTML, MHTML or EPUB.
public IFontSavingCallback FontSavingCallback { get; set; }Property Value
Examples
Shows how to define custom logic for exporting fonts when saving to HTML.
public void SaveExportedFonts()
{
Document doc = new Document(MyDir + "Rendering.docx");
// Configure a SaveOptions object to export fonts to separate files.
// Set a callback that will handle font saving in a custom manner.
HtmlSaveOptions options = new HtmlSaveOptions
{
ExportFontResources = true,
FontSavingCallback = new HandleFontSaving()
};
// The callback will export .ttf files and save them alongside the output document.
doc.Save(ArtifactsDir + "HtmlSaveOptions.SaveExportedFonts.html", options);
foreach (string fontFilename in Array.FindAll(Directory.GetFiles(ArtifactsDir), s => s.EndsWith(".ttf")))
Console.WriteLine(fontFilename);
}
/// <summary>
/// Prints information about exported fonts and saves them in the same local system folder as their output .html.
/// </summary>
public class HandleFontSaving : IFontSavingCallback
{
void IFontSavingCallback.FontSaving(FontSavingArgs args)
{
Console.Write($"Font:\t{args.FontFamilyName}");
if (args.Bold) Console.Write(", bold");
if (args.Italic) Console.Write(", italic");
Console.WriteLine($"\nSource:\t{args.OriginalFileName}, {args.OriginalFileSize} bytes\n");
// We can also access the source document from here.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
Assert.That(args.IsExportNeeded, Is.True);
Assert.That(args.IsSubsettingNeeded, Is.True);
// There are two ways of saving an exported font.
// 1 - Save it to a local file system location:
args.FontFileName = args.OriginalFileName.Split(Path.DirectorySeparatorChar).Last();
// 2 - Save it to a stream:
args.FontStream =
new FileStream(ArtifactsDir + args.OriginalFileName.Split(Path.DirectorySeparatorChar).Last(), FileMode.Create);
Assert.That(args.KeepFontStreamOpen, Is.False);
}
}FontsFolder
Specifies the physical folder where fonts are saved when exporting a document to HTML. Default is an empty string.
public string FontsFolder { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
When you save a Aspose.Words.Document in HTML format and Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources
is set to true, Aspose.Words needs to save fonts used in the document as standalone files.
Aspose.Words.Saving.HtmlSaveOptions.FontsFolder allows you to specify where the fonts will be saved and
Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias allows to specify how the font URIs will be constructed.
If you save a document into a file and provide a file name, Aspose.Words, by default, saves the fonts in the same folder where the document file is saved. Use Aspose.Words.Saving.HtmlSaveOptions.FontsFolder to override this behavior.
If you save a document into a stream, Aspose.Words does not have a folder where to save the fonts, but still needs to save the fonts somewhere. In this case, you need to specify an accessible folder in the Aspose.Words.Saving.HtmlSaveOptions.FontsFolder property or provide custom streams via the Aspose.Words.Saving.HtmlSaveOptions.FontSavingCallback event handler.
If the folder specified by Aspose.Words.Saving.HtmlSaveOptions.FontsFolder doesn't exist, it will be created automatically.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder is another way to specify a folder where fonts should be saved.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias Aspose.Words.Saving.HtmlSaveOptions.FontSavingCallbackFontsFolderAlias
Specifies the name of the folder used to construct font URIs written into an HTML document. Default is an empty string.
public string FontsFolderAlias { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
When you save a Aspose.Words.Document in HTML format and Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources
is set to true, Aspose.Words needs to save fonts used in the document as standalone files.
Aspose.Words.Saving.HtmlSaveOptions.FontsFolder allows you to specify where the fonts will be saved and
Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias allows to specify how the font URIs will be constructed.
If Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias is not an empty string, then the font URI written to HTML will be FontsFolderAlias + <font file name>.
If Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias is an empty string, then the font URI written to HTML will be FontsFolder + <font file name>.
If Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias is set to '.' (dot), then the font file name will be written to HTML without path regardless of other options.
Alternative way to specify the name of the folder to construct font URIs is to use Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias Aspose.Words.Saving.HtmlSaveOptions.ExportFontResources Aspose.Words.Saving.HtmlSaveOptions.FontsFolder Aspose.Words.Saving.HtmlSaveOptions.FontSavingCallbackHtmlVersion
Specifies version of HTML standard that should be used when saving the document to HTML or MHTML. Default value is Aspose.Words.Saving.HtmlVersion.Xhtml.
public HtmlVersion HtmlVersion { get; set; }Property Value
Examples
Shows how to display a DOCTYPE heading when converting documents to the Xhtml 1.0 transitional standard.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.Writeln("Hello world!");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
HtmlVersion = HtmlVersion.Xhtml,
ExportXhtmlTransitional = showDoctypeDeclaration,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ExportXhtmlTransitional.html", options);
// Our document will only contain a DOCTYPE declaration heading if we have set the "ExportXhtmlTransitional" flag to "true".
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ExportXhtmlTransitional.html");
string newLine = Environment.NewLine;
if (showDoctypeDeclaration)
Assert.That(outDocContents.Contains(
$"<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>{newLine}" +
$"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">{newLine}" +
"<html xmlns=\"http://www.w3.org/1999/xhtml\">"), Is.True);
else
Assert.That(outDocContents.Contains("<html>"), Is.True);Shows how to save a document to a specific version of HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
HtmlVersion = htmlVersion,
PrettyFormat = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.HtmlVersions.html", options);
// Our HTML documents will have minor differences to be compatible with different HTML versions.
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.HtmlVersions.html");
switch (htmlVersion)
{
case HtmlVersion.Html5:
Assert.That(outDocContents.Contains("<a id=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<a id=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<table style=\"padding:0pt; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
break;
case HtmlVersion.Xhtml:
Assert.That(outDocContents.Contains("<a name=\"_Toc76372689\"></a>"), Is.True);
Assert.That(outDocContents.Contains("<ul type=\"disc\" style=\"margin:0pt; padding-left:0pt\">"), Is.True);
Assert.That(outDocContents.Contains("<table cellspacing=\"0\" cellpadding=\"0\" style=\"-aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\""), Is.True);
break;
}ImageResolution
Specifies the output resolution for images when exporting to HTML, MHTML or EPUB.
Default is 96 dpi.
public int ImageResolution { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
This property effects raster images when Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize
is true and effects metafiles exported as raster images. Some image properties such as cropping
or rotation require saving transformed images and in this case transformed images are created in the given
resolution.
See Also
HtmlSaveOptions . ScaleImageToShapeSize
ImageSavingCallback
Allows to control how images are saved when a document is saved to HTML, MHTML or EPUB.
public IImageSavingCallback ImageSavingCallback { get; set; }Property Value
Examples
Shows how to split a document into parts and save them.
public void DocumentPartsFileNames()
{
Document doc = new Document(MyDir + "Rendering.docx");
string outFileName = "SavingCallback.DocumentPartsFileNames.html";
// Create an "HtmlFixedSaveOptions" object, which we can pass to the document's "Save" method
// to modify how we convert the document to HTML.
HtmlSaveOptions options = new HtmlSaveOptions();
// If we save the document normally, there will be one output HTML
// document with all the source document's contents.
// Set the "DocumentSplitCriteria" property to "DocumentSplitCriteria.SectionBreak" to
// save our document to multiple HTML files: one for each section.
options.DocumentSplitCriteria = DocumentSplitCriteria.SectionBreak;
// Assign a custom callback to the "DocumentPartSavingCallback" property to alter the document part saving logic.
options.DocumentPartSavingCallback = new SavedDocumentPartRename(outFileName, options.DocumentSplitCriteria);
// If we convert a document that contains images into html, we will end up with one html file which links to several images.
// Each image will be in the form of a file in the local file system.
// There is also a callback that can customize the name and file system location of each image.
options.ImageSavingCallback = new SavedImageRename(outFileName);
doc.Save(ArtifactsDir + outFileName, options);
}
/// <summary>
/// Sets custom filenames for output documents that the saving operation splits a document into.
/// </summary>
private class SavedDocumentPartRename : IDocumentPartSavingCallback
{
public SavedDocumentPartRename(string outFileName, DocumentSplitCriteria documentSplitCriteria)
{
mOutFileName = outFileName;
mDocumentSplitCriteria = documentSplitCriteria;
}
void IDocumentPartSavingCallback.DocumentPartSaving(DocumentPartSavingArgs args)
{
// We can access the entire source document via the "Document" property.
Assert.That(args.Document.OriginalFileName.EndsWith("Rendering.docx"), Is.True);
string partType = string.Empty;
switch (mDocumentSplitCriteria)
{
case DocumentSplitCriteria.PageBreak:
partType = "Page";
break;
case DocumentSplitCriteria.ColumnBreak:
partType = "Column";
break;
case DocumentSplitCriteria.SectionBreak:
partType = "Section";
break;
case DocumentSplitCriteria.HeadingParagraph:
partType = "Paragraph from heading";
break;
}
string partFileName = $"{mOutFileName} part {++mCount}, of type {partType}{Path.GetExtension(args.DocumentPartFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output part file:
args.DocumentPartFileName = partFileName;
// 2 - Create a custom stream for the output part file:
args.DocumentPartStream = new FileStream(ArtifactsDir + partFileName, FileMode.Create);
Assert.That(args.DocumentPartStream.CanWrite, Is.True);
Assert.That(args.KeepDocumentPartStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
private readonly DocumentSplitCriteria mDocumentSplitCriteria;
}
/// <summary>
/// Sets custom filenames for image files that an HTML conversion creates.
/// </summary>
public class SavedImageRename : IImageSavingCallback
{
public SavedImageRename(string outFileName)
{
mOutFileName = outFileName;
}
void IImageSavingCallback.ImageSaving(ImageSavingArgs args)
{
string imageFileName = $"{mOutFileName} shape {++mCount}, of type {args.CurrentShape.ShapeType}{Path.GetExtension(args.ImageFileName)}";
// Below are two ways of specifying where Aspose.Words will save each part of the document.
// 1 - Set a filename for the output image file:
args.ImageFileName = imageFileName;
// 2 - Create a custom stream for the output image file:
args.ImageStream = new FileStream(ArtifactsDir + imageFileName, FileMode.Create);
Assert.That(args.ImageStream.CanWrite, Is.True);
Assert.That(args.IsImageAvailable, Is.True);
Assert.That(args.KeepImageStreamOpen, Is.False);
}
private int mCount;
private readonly string mOutFileName;
}ImagesFolder
Specifies the physical folder where images are saved when exporting a document to HTML format. Default is an empty string.
public string ImagesFolder { get; set; }Property Value
Examples
Shows how to specify the folder for storing linked images after saving to .html.
Document doc = new Document(MyDir + "Rendering.docx");
string imagesDir = Path.Combine(ArtifactsDir, "SaveHtmlWithOptions");
if (Directory.Exists(imagesDir))
Directory.Delete(imagesDir, true);
Directory.CreateDirectory(imagesDir);
// Set an option to export form fields as plain text instead of HTML input elements.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
ExportTextInputFormFieldAsText = true,
ImagesFolder = imagesDir
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.SaveHtmlWithOptions.html", options);Remarks
When you save a Aspose.Words.Document in HTML format, Aspose.Words needs to save all images embedded in the document as standalone files. Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder allows you to specify where the images will be saved and Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias allows to specify how the image URIs will be constructed.
If you save a document into a file and provide a file name, Aspose.Words, by default, saves the images in the same folder where the document file is saved. Use Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder to override this behavior.
If you save a document into a stream, Aspose.Words does not have a folder where to save the images, but still needs to save the images somewhere. In this case, you need to specify an accessible folder in the Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder property or provide custom streams via the Aspose.Words.Saving.HtmlSaveOptions.ImageSavingCallback event handler.
If the folder specified by Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder doesn't exist, it will be created automatically.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder is another way to specify a folder where images should be saved.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias Aspose.Words.Saving.HtmlSaveOptions.ImageSavingCallbackImagesFolderAlias
Specifies the name of the folder used to construct image URIs written into an HTML document. Default is an empty string.
public string ImagesFolderAlias { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
When you save a Aspose.Words.Document in HTML format, Aspose.Words needs to save all images embedded in the document as standalone files. Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder allows you to specify where the images will be saved and Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias allows to specify how the image URIs will be constructed.
If Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias is not an empty string, then the image URI written to HTML will be ImagesFolderAlias + <image file name>.
If Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias is an empty string, then the image URI written to HTML will be ImagesFolder + <image file name>.
If Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias is set to '.' (dot), then the image file name will be written to HTML without path regardless of other options.
Alternative way to specify the name of the folder to construct image URIs is to use Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder Aspose.Words.Saving.HtmlSaveOptions.ImageSavingCallbackMetafileFormat
Specifies in what format metafiles are saved when exporting to HTML, MHTML, or EPUB. Default value is Aspose.Words.Saving.HtmlMetafileFormat.Png, meaning that metafiles are rendered to raster PNG images.
public HtmlMetafileFormat MetafileFormat { get; set; }Property Value
Examples
Shows how to convert SVG objects to a different format when saving HTML documents.
string html =
@"<html>
<svg xmlns='http://www.w3.org/2000/svg' width='500' height='40' viewBox='0 0 500 40'>
<text x='0' y='35' font-family='Verdana' font-size='35'>Hello world!</text>
</svg>
</html>";
// Use 'ConvertSvgToEmf' to turn back the legacy behavior
// where all SVG images loaded from an HTML document were converted to EMF.
// Now SVG images are loaded without conversion
// if the MS Word version specified in load options supports SVG images natively.
HtmlLoadOptions loadOptions = new HtmlLoadOptions { ConvertSvgToEmf = true };
Document doc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(html)), loadOptions);
// This document contains a <svg> element in the form of text.
// When we save the document to HTML, we can pass a SaveOptions object
// to determine how the saving operation handles this object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Png" to convert it to a PNG image.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.Svg" preserve it as a SVG object.
// Setting the "MetafileFormat" property to "HtmlMetafileFormat.EmfOrWmf" to convert it to a metafile.
HtmlSaveOptions options = new HtmlSaveOptions { MetafileFormat = htmlMetafileFormat };
doc.Save(ArtifactsDir + "HtmlSaveOptions.MetafileFormat.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.MetafileFormat.html");
switch (htmlMetafileFormat)
{
case HtmlMetafileFormat.Png:
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.png\" width=\"500\" height=\"40\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"), Is.True);
break;
case HtmlMetafileFormat.Svg:
Assert.That(outDocContents.Contains(
"<span style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\">" +
"<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"499\" height=\"40\">"), Is.True);
break;
case HtmlMetafileFormat.EmfOrWmf:
Assert.That(outDocContents.Contains(
"<p style=\"margin-top:0pt; margin-bottom:0pt\">" +
"<img src=\"HtmlSaveOptions.MetafileFormat.001.emf\" width=\"500\" height=\"40\" alt=\"\" " +
"style=\"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>"), Is.True);
break;
}Remarks
Metafiles are not natively displayed by HTML browsers. By default, Aspose.Words converts WMF and EMF images into PNG files when exporting to HTML. Other options are to convert metafiles to SVG images or to export them as is without conversion.
Some image transforms, in particular image cropping, will not be applied to metafile images if they are exported to HTML without conversion.
See Also
HtmlSaveOptions . ImageResolution , HtmlSaveOptions . ScaleImageToShapeSize
NavigationMapLevel
Specifies the maximum level of headings populated to the navigation map when exporting to EPUB, MOBI, or AZW3
formats. Default value is 3.
public int NavigationMapLevel { get; set; }Property Value
Examples
Shows how to generate table of contents for Azw3 documents.
Document doc = new Document(MyDir + "Big document.docx");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Azw3);
options.NavigationMapLevel = 2;
doc.Save(ArtifactsDir + "HtmlSaveOptions.CreateAZW3Toc.azw3", options);Shows how to generate table of contents for Mobi documents.
Document doc = new Document(MyDir + "Big document.docx");
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Mobi);
options.NavigationMapLevel = 5;
doc.Save(ArtifactsDir + "HtmlSaveOptions.CreateMobiToc.mobi", options);Shows how to filter headings that appear in the navigation panel of a saved Epub document.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Every paragraph that we format using a "Heading" style can serve as a heading.
// Each heading may also have a heading level, determined by the number of its heading style.
// The headings below are of levels 1-3.
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 1"];
builder.Writeln("Heading #1");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 2"];
builder.Writeln("Heading #2");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("Heading #3");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 1"];
builder.Writeln("Heading #4");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 2"];
builder.Writeln("Heading #5");
builder.ParagraphFormat.Style = builder.Document.Styles["Heading 3"];
builder.Writeln("Heading #6");
// Epub readers typically create a table of contents for their documents.
// Each paragraph with a "Heading" style in the document will create an entry in this table of contents.
// We can use the "NavigationMapLevel" property to set a maximum heading level.
// The Epub reader will not add headings with a level above the one we specify to the contents table.
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Epub);
options.NavigationMapLevel = 2;
// Our document has six headings, two of which are above level 2.
// The table of contents for this document will have four entries.
doc.Save(ArtifactsDir + "HtmlSaveOptions.EpubHeadings.epub", options);Remarks
The navigation map allows user agents to provide an easy way of navigation through the document structure. Usually navigation points correspond to headings in the document. In order to populate headings up to level N assign this value to Aspose.Words.Saving.HtmlSaveOptions.NavigationMapLevel.
By default, three levels of headings are populated: paragraphs of styles Heading 1, Heading 2 and Heading 3. You can set this property to a value from 1 to 9 in order to request the corresponding maximum level. Setting it to zero will reduce the navigation map to only the document root or roots of document parts.
OfficeMathOutputMode
Controls how OfficeMath objects are exported to HTML, MHTML or EPUB. Default value is Aspose.Words.Saving.HtmlOfficeMathOutputMode.Image.
public HtmlOfficeMathOutputMode OfficeMathOutputMode { get; set; }Property Value
Examples
Shows how to specify how to export Microsoft OfficeMath objects to HTML.
Document doc = new Document(MyDir + "Office math.docx");
// When we save the document to HTML, we can pass a SaveOptions object
// to determine how the saving operation handles OfficeMath objects.
// Setting the "OfficeMathOutputMode" property to "HtmlOfficeMathOutputMode.Image"
// will render each OfficeMath object into an image.
// Setting the "OfficeMathOutputMode" property to "HtmlOfficeMathOutputMode.MathML"
// will convert each OfficeMath object into MathML.
// Setting the "OfficeMathOutputMode" property to "HtmlOfficeMathOutputMode.Text"
// will represent each OfficeMath formula using plain HTML text.
HtmlSaveOptions options = new HtmlSaveOptions { OfficeMathOutputMode = htmlOfficeMathOutputMode };
doc.Save(ArtifactsDir + "HtmlSaveOptions.OfficeMathOutputMode.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.OfficeMathOutputMode.html");
switch (htmlOfficeMathOutputMode)
{
case HtmlOfficeMathOutputMode.Image:
Assert.That(Regex.Match(outDocContents,
"<p style=\"margin-top:0pt; margin-bottom:10pt\">" +
"<img src=\"HtmlSaveOptions.OfficeMathOutputMode.001.png\" width=\"163\" height=\"19\" alt=\"\" style=\"vertical-align:middle; " +
"-aw-left-pos:0pt; -aw-rel-hpos:column; -aw-rel-vpos:paragraph; -aw-top-pos:0pt; -aw-wrap-type:inline\" />" +
"</p>").Success, Is.True);
break;
case HtmlOfficeMathOutputMode.MathML:
Assert.That(Regex.Match(outDocContents,
"<p style=\"margin-top:0pt; margin-bottom:10pt; text-align:center\">" +
"<math xmlns=\"http://www.w3.org/1998/Math/MathML\">" +
"<mi>i</mi>" +
"<mo>[+]</mo>" +
"<mi>b</mi>" +
"<mo>-</mo>" +
"<mi>c</mi>" +
"<mo>≥</mo>" +
".*" +
"</math>" +
"</p>").Success, Is.True);
break;
case HtmlOfficeMathOutputMode.Text:
Assert.That(Regex.Match(outDocContents,
@"<p style=\""margin-top:0pt; margin-bottom:10pt; text-align:center\"">" +
@"<span style=\""font-family:'Cambria Math'\"">i[+]b-c≥iM[+]bM-cM </span>" +
"</p>").Success, Is.True);
break;
}RemoveJavaScriptFromLinks
Specifies whether JavaScript will be removed from links.
Default is false.
public bool RemoveJavaScriptFromLinks { get; set; }Property Value
Remarks
If this option is enabled, all links containing JavaScript (e.g., links with “javascript:” in the href attribute) will be replaced with “javascript:void(0)”. This can help prevent potential security risks, such as XSS attacks.
ReplaceBackslashWithYenSign
Specifies whether backslash characters should be replaced with yen signs.
Default value is false.
public bool ReplaceBackslashWithYenSign { get; set; }Property Value
Examples
Shows how to replace backslash characters with yen signs (Html).
Document doc = new Document(MyDir + "Korean backslash symbol.docx");
// By default, Aspose.Words mimics MS Word's behavior and doesn't replace backslash characters with yen signs in
// generated HTML documents. However, previous versions of Aspose.Words performed such replacements in certain
// scenarios. This flag enables backward compatibility with previous versions of Aspose.Words.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ReplaceBackslashWithYenSign = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.ReplaceBackslashWithYenSign.html", saveOptions);Remarks
By default, Aspose.Words mimics MS Word’s behavior and doesn’t replace backslash characters with yen signs in generated HTML documents. However, previous versions of Aspose.Words performed such replacements in certain scenarios. This flag enables backward compatibility with previous versions of Aspose.Words.
ResolveFontNames
Specifies whether font family names used in the document are resolved and substituted according to Aspose.Words.Document.FontSettings when being written into HTML-based formats.
public bool ResolveFontNames { get; set; }Property Value
Examples
Shows how to resolve all font names before writing them to HTML.
Document doc = new Document(MyDir + "Missing font.docx");
// This document contains text that names a font that we do not have.
Assert.That(doc.FontInfos["28 Days Later"], Is.Not.Null);
// If we have no way of getting this font, and we want to be able to display all the text
// in this document in an output HTML, we can substitute it with another font.
FontSettings fontSettings = new FontSettings
{
SubstitutionSettings =
{
DefaultFontSubstitution =
{
DefaultFontName = "Arial",
Enabled = true
}
}
};
doc.FontSettings = fontSettings;
HtmlSaveOptions saveOptions = new HtmlSaveOptions(SaveFormat.Html)
{
// By default, this option is set to 'False' and Aspose.Words writes font names as specified in the source document
ResolveFontNames = resolveFontNames
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.ResolveFontNames.html", saveOptions);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.ResolveFontNames.html");
Assert.That(resolveFontNames
? Regex.Match(outDocContents, "<span style=\"font-family:Arial\">").Success
: Regex.Match(outDocContents, "<span style=\"font-family:\'28 Days Later\'\">").Success, Is.True);Remarks
By default, this option is set to false and font family names are written to HTML as specified
in source documents. That is, Aspose.Words.Document.FontSettings are ignored and no resolution or substitution
of font family names is performed.
If this option is set to true, Aspose.Words uses Aspose.Words.Document.FontSettings to resolve
each font family name specified in a source document into the name of an available font family, performing
font substitution as required.
ResourceFolder
Specifies a physical folder where all resources like images, fonts, and external CSS are saved when a document is exported to HTML. Default is an empty string.
public string ResourceFolder { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder is the simplest way to specify a folder where all resources should be written. Another way is to use individual properties Aspose.Words.Saving.HtmlSaveOptions.FontsFolder, Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder, and Aspose.Words.Saving.HtmlSaveOptions.CssStyleSheetFileName.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder has a lower priority than folders specified via Aspose.Words.Saving.HtmlSaveOptions.FontsFolder, Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder, and Aspose.Words.Saving.HtmlSaveOptions.CssStyleSheetFileName. For example, if both Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder and Aspose.Words.Saving.HtmlSaveOptions.FontsFolder are specified, fonts will be saved to Aspose.Words.Saving.HtmlSaveOptions.FontsFolder, while images and CSS will be saved to Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder.
If the folder specified by Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder doesn't exist, it will be created automatically.
Aspose.Words.Saving.HtmlSaveOptions.FontsFolder Aspose.Words.Saving.HtmlSaveOptions.ImagesFolder Aspose.Words.Saving.HtmlSaveOptions.CssStyleSheetFileNameResourceFolderAlias
Specifies the name of the folder used to construct URIs of all resources written into an HTML document. Default is an empty string.
public string ResourceFolderAlias { get; set; }Property Value
Examples
Shows how to set folders and folder aliases for externally saved resources that Aspose.Words will create when saving a document to HTML.
Document doc = new Document(MyDir + "Rendering.docx");
HtmlSaveOptions options = new HtmlSaveOptions
{
CssStyleSheetType = CssStyleSheetType.External,
ExportFontResources = true,
ImageResolution = 72,
FontResourcesSubsettingSizeThreshold = 0,
FontsFolder = ArtifactsDir + "Fonts",
ImagesFolder = ArtifactsDir + "Images",
ResourceFolder = ArtifactsDir + "Resources",
FontsFolderAlias = "http://example.com/fonts",
ImagesFolderAlias = "http://example.com/images",
ResourceFolderAlias = "http://example.com/resources",
ExportOriginalUrlForLinkedImages = true
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.FolderAlias.html", options);Remarks
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias is the simplest way to specify how URIs for all resource files should be constructed. Same information can be specified for images and fonts separately via Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias and Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias properties, respectively. However, there is no individual property for CSS.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias has lower priority than Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias and Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAlias. For example, if both Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias and Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias are specified, fonts' URIs will be constructed using Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias, while URIs of images and CSS will be constructed using Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias.
If Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias is empty, the Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder property value will be used to construct resource URIs.
If Aspose.Words.Saving.HtmlSaveOptions.ResourceFolderAlias is set to '.' (dot), resource URIs will contain file names only, without any path.
Aspose.Words.Saving.HtmlSaveOptions.ResourceFolder Aspose.Words.Saving.HtmlSaveOptions.FontsFolderAlias Aspose.Words.Saving.HtmlSaveOptions.ImagesFolderAliasSaveFormat
Specifies the format in which the document will be saved if this save options object is used. Can be Aspose.Words.SaveFormat.Html, Aspose.Words.SaveFormat.Mhtml, Aspose.Words.SaveFormat.Epub, Aspose.Words.SaveFormat.Azw3 or Aspose.Words.SaveFormat.Mobi.
public override SaveFormat SaveFormat { get; set; }Property Value
Examples
Shows how to use a specific encoding when saving a document to .epub.
Document doc = new Document(MyDir + "Rendering.docx");
// Use a SaveOptions object to specify the encoding for a document that we will save.
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.SaveFormat = SaveFormat.Epub;
saveOptions.Encoding = Encoding.UTF8;
// By default, an output .epub document will have all its contents in one HTML part.
// A split criterion allows us to segment the document into several HTML parts.
// We will set the criteria to split the document into heading paragraphs.
// This is useful for readers who cannot read HTML files more significant than a specific size.
saveOptions.DocumentSplitCriteria = DocumentSplitCriteria.HeadingParagraph;
// Specify that we want to export document properties.
saveOptions.ExportDocumentProperties = true;
doc.Save(ArtifactsDir + "HtmlSaveOptions.Doc2EpubSaveOptions.epub", saveOptions);ScaleImageToShapeSize
Specifies whether images are scaled by Aspose.Words to the bounding shape size when exporting to HTML, MHTML
or EPUB.
Default value is true.
public bool ScaleImageToShapeSize { get; set; }Property Value
Examples
Shows how to disable the scaling of images to their parent shape dimensions when saving to .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a shape which contains an image, and then make that shape considerably smaller than the image.
Shape imageShape = builder.InsertImage(ImageDir + "Transparent background logo.png");
imageShape.Width = 50;
imageShape.Height = 50;
// Saving a document that contains shapes with images to HTML will create an image file in the local file system
// for each such shape. The output HTML document will use <image> tags to link to and display these images.
// When we save the document to HTML, we can pass a SaveOptions object to determine
// whether to scale all images that are inside shapes to the sizes of their shapes.
// Setting the "ScaleImageToShapeSize" flag to "true" will shrink every image
// to the size of the shape that contains it, so that no saved images will be larger than the document requires them to be.
// Setting the "ScaleImageToShapeSize" flag to "false" will preserve these images' original sizes,
// which will take up more space in exchange for preserving image quality.
HtmlSaveOptions options = new HtmlSaveOptions { ScaleImageToShapeSize = scaleImageToShapeSize };
doc.Save(ArtifactsDir + "HtmlSaveOptions.ScaleImageToShapeSize.html", options);Remarks
An image in a Microsoft Word document is a shape. The shape has a size and the image has its own size. The sizes are not directly linked. For example, the image can be 1024x786 pixels, but shape that displays this image can be 400x300 points.
In order to display an image in the browser, it must be scaled to the shape size. The Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize property controls where the scaling of the image takes place: in Aspose.Words during export to HTML or in the browser when displaying the document.
When Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize is true, the image is scaled by Aspose.Words
using high quality scaling during export to HTML. When Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize
is false, the image is output with its original size and the browser has to scale it.
In general, browsers do quick and poor quality scaling. As a result, you will normally get better
display quality in the browser and smaller file size when Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize is true,
but better printing quality and faster conversion when Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize is false.
In addition to shapes containing individual raster images, this option also affects group shapes consisting
of raster images. If Aspose.Words.Saving.HtmlSaveOptions.ScaleImageToShapeSize is false and a group shape contains raster images
whose intrinsic resolution is higher than the value specified in Aspose.Words.Saving.HtmlSaveOptions.ImageResolution, Aspose.Words will
increase rendering resolution for that group. This allows to better preserve quality of grouped high resolution
images when saving to HTML.
See Also
HtmlSaveOptions . ImageResolution
TableWidthOutputMode
Controls how table, row and cell widths are exported to HTML, MHTML or EPUB. Default value is Aspose.Words.Saving.HtmlElementSizeOutputMode.All.
public HtmlElementSizeOutputMode TableWidthOutputMode { get; set; }Property Value
Examples
Shows how to preserve negative indents in the output .html.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert a table with a negative indent, which will push it to the left past the left page boundary.
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1");
builder.InsertCell();
builder.Write("Row 1, Cell 2");
builder.EndTable();
table.LeftIndent = -36;
table.PreferredWidth = PreferredWidth.FromPoints(144);
builder.InsertBreak(BreakType.ParagraphBreak);
// Insert a table with a positive indent, which will push the table to the right.
table = builder.StartTable();
builder.InsertCell();
builder.Write("Row 1, Cell 1");
builder.InsertCell();
builder.Write("Row 1, Cell 2");
builder.EndTable();
table.LeftIndent = 36;
table.PreferredWidth = PreferredWidth.FromPoints(144);
// When we save a document to HTML, Aspose.Words will only preserve negative indents
// such as the one we have applied to the first table if we set the "AllowNegativeIndent" flag
// in a SaveOptions object that we will pass to "true".
HtmlSaveOptions options = new HtmlSaveOptions(SaveFormat.Html)
{
AllowNegativeIndent = allowNegativeIndent,
TableWidthOutputMode = HtmlElementSizeOutputMode.RelativeOnly
};
doc.Save(ArtifactsDir + "HtmlSaveOptions.NegativeIndent.html", options);
string outDocContents = File.ReadAllText(ArtifactsDir + "HtmlSaveOptions.NegativeIndent.html");
if (allowNegativeIndent)
{
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:-41.65pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:30.35pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
}
else
{
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
Assert.That(outDocContents.Contains(
"<table cellspacing=\"0\" cellpadding=\"0\" style=\"margin-left:30.35pt; border:0.75pt solid #000000; -aw-border:0.5pt single #000000; -aw-border-insideh:0.5pt single #000000; -aw-border-insidev:0.5pt single #000000; border-collapse:collapse\">"), Is.True);
}Remarks
In the HTML format, table, row and cell elements (<table>, <tr>, <th>, <td>) can have their widths specified either in relative (percentage) or in absolute units. In a document in Aspose.Words, tables, rows and cells can have their widths specified using either relative or absolute units too.
When you convert a document to HTML using Aspose.Words, you might want to control how table, row and cell widths are exported to affect how the resulting document is displayed in the visual agent (e.g. a browser or viewer).
Use this property as a filter to specify what table widths values are exported into the destination document. For example, if you are converting a document to EPUB and intend to view the document on a mobile reading device, then you probably want to avoid exporting absolute width values. To do this you need to specify the output mode Aspose.Words.Saving.HtmlElementSizeOutputMode.RelativeOnly or Aspose.Words.Saving.HtmlElementSizeOutputMode.None so the viewer on the mobile device can layout the table to fit the width of the screen as best as it can.