public class MarkdownSaveOptions extends TxtSaveOptionsBase
SaveFormat.MARKDOWN format.
To learn more, visit the Specify Save Options documentation article.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
| Constructor and Description |
|---|
MarkdownSaveOptions()
Initializes a new instance of this class that can be used to save a document in the
SaveFormat.MARKDOWN format. |
| Modifier and Type | Method and Description |
|---|---|
int |
getEmptyParagraphExportMode()
Specifies how to export empty paragraphs to Markdown.
|
int |
getExportAsHtml()
Allows to specify the elements to be exported to Markdown as raw HTML.
|
boolean |
getExportImagesAsBase64()
Specifies whether images are saved in Base64 format to the output file.
|
boolean |
getExportUnderlineFormatting()
Gets a boolean value indicating either to export underline text formatting as sequence of two plus characters "++".
|
int |
getImageResolution()
Specifies the output resolution for images when exporting to Markdown.
|
IImageSavingCallback |
getImageSavingCallback()
Allows to control how images are saved when a document is saved to
SaveFormat.MARKDOWN format. |
java.lang.String |
getImagesFolder()
Specifies the physical folder where images are saved when exporting a document to the
SaveFormat.MARKDOWN format. |
java.lang.String |
getImagesFolderAlias()
Specifies the name of the folder used to construct image URIs written into a document.
|
int |
getLinkExportMode()
Specifies how links will be written to the output file.
|
int |
getListExportMode()
Specifies how list items will be written to the output file.
|
int |
getOfficeMathExportMode()
Specifies how OfficeMath will be written to the output file.
|
IResourceSavingCallback |
getResourceSavingCallback()
Allows to control how resources are saved when a document is exported to
SaveFormat.MARKDOWN format. |
int |
getSaveFormat()
Specifies the format in which the document will be saved if this save options object is used.
|
int |
getTableContentAlignment()
Gets a value that specifies how to align contents in tables when exporting into the
SaveFormat.MARKDOWN format. |
void |
setEmptyParagraphExportMode(int value)
Specifies how to export empty paragraphs to Markdown.
|
void |
setExportAsHtml(int value)
Allows to specify the elements to be exported to Markdown as raw HTML.
|
void |
setExportImagesAsBase64(boolean value)
Specifies whether images are saved in Base64 format to the output file.
|
void |
setExportUnderlineFormatting(boolean value)
Sets a boolean value indicating either to export underline text formatting as sequence of two plus characters "++".
|
void |
setImageResolution(int value)
Specifies the output resolution for images when exporting to Markdown.
|
void |
setImageSavingCallback(IImageSavingCallback value)
Allows to control how images are saved when a document is saved to
SaveFormat.MARKDOWN format. |
void |
setImagesFolder(java.lang.String value)
Specifies the physical folder where images are saved when exporting a document to the
SaveFormat.MARKDOWN format. |
void |
setImagesFolderAlias(java.lang.String value)
Specifies the name of the folder used to construct image URIs written into a document.
|
void |
setLinkExportMode(int value)
Specifies how links will be written to the output file.
|
void |
setListExportMode(int value)
Specifies how list items will be written to the output file.
|
void |
setOfficeMathExportMode(int value)
Specifies how OfficeMath will be written to the output file.
|
void |
setResourceSavingCallback(IResourceSavingCallback value)
Allows to control how resources are saved when a document is exported to
SaveFormat.MARKDOWN format. |
void |
setSaveFormat(int value)
Specifies the format in which the document will be saved if this save options object is used.
|
void |
setTableContentAlignment(int value)
Sets a value that specifies how to align contents in tables when exporting into the
SaveFormat.MARKDOWN format. |
getEncoding, getExportHeadersFootersMode, getForcePageBreaks, getParagraphBreak, setEncoding, setExportHeadersFootersMode, setForcePageBreaks, setParagraphBreakcreateSaveOptions, createSaveOptions, getAllowEmbeddingPostScriptFonts, getCustomTimeZoneInfo, getDefaultTemplate, getDml3DEffectsRenderingMode, getDmlEffectsRenderingMode, getDmlRenderingMode, getExportGeneratorName, getImlRenderingMode, getMemoryOptimization, getPrettyFormat, getProgressCallback, getTempFolder, getUpdateAmbiguousTextFont, getUpdateCreatedTimeProperty, getUpdateFields, getUpdateLastPrintedProperty, getUpdateLastSavedTimeProperty, getUseAntiAliasing, getUseHighQualityRendering, setAllowEmbeddingPostScriptFonts, setCustomTimeZoneInfo, setDefaultTemplate, setDml3DEffectsRenderingMode, setDmlEffectsRenderingMode, setDmlRenderingMode, setExportGeneratorName, setImlRenderingMode, setMemoryOptimization, setPrettyFormat, setProgressCallback, setTempFolder, setUpdateAmbiguousTextFont, setUpdateCreatedTimeProperty, setUpdateFields, setUpdateLastPrintedProperty, setUpdateLastSavedTimeProperty, setUseAntiAliasing, setUseHighQualityRenderingpublic MarkdownSaveOptions()
SaveFormat.MARKDOWN format.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
public int getSaveFormat()
SaveFormat.MARKDOWN.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
getSaveFormat in class SaveOptionsint value. The returned value is one of SaveFormat constants.public void setSaveFormat(int value)
SaveFormat.MARKDOWN.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
setSaveFormat in class SaveOptionsvalue - The corresponding int value. The value must be one of SaveFormat constants.public int getTableContentAlignment()
SaveFormat.MARKDOWN format. The default value is TableContentAlignment.AUTO.
Examples:
Shows how to align contents in tables.
DocumentBuilder builder = new DocumentBuilder();
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Cell1");
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Cell2");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); { saveOptions.setTableContentAlignment(tableContentAlignment); }
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.MarkdownDocumentTableContentAlignment.md", saveOptions);
Document doc = new Document(getArtifactsDir() + "MarkdownSaveOptions.MarkdownDocumentTableContentAlignment.md");
Table table = doc.getFirstSection().getBody().getTables().get(0);
switch (tableContentAlignment)
{
case TableContentAlignment.AUTO:
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.LEFT:
Assert.assertEquals(ParagraphAlignment.LEFT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.LEFT,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.CENTER:
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.RIGHT:
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
}
SaveFormat.MARKDOWN format. The returned value is one of TableContentAlignment constants.public void setTableContentAlignment(int value)
SaveFormat.MARKDOWN format. The default value is TableContentAlignment.AUTO.
Examples:
Shows how to align contents in tables.
DocumentBuilder builder = new DocumentBuilder();
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Cell1");
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Cell2");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); { saveOptions.setTableContentAlignment(tableContentAlignment); }
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.MarkdownDocumentTableContentAlignment.md", saveOptions);
Document doc = new Document(getArtifactsDir() + "MarkdownSaveOptions.MarkdownDocumentTableContentAlignment.md");
Table table = doc.getFirstSection().getBody().getTables().get(0);
switch (tableContentAlignment)
{
case TableContentAlignment.AUTO:
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.LEFT:
Assert.assertEquals(ParagraphAlignment.LEFT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.LEFT,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.CENTER:
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.CENTER,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
case TableContentAlignment.RIGHT:
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(0).getFirstParagraph().getParagraphFormat().getAlignment());
Assert.assertEquals(ParagraphAlignment.RIGHT,
table.getFirstRow().getCells().get(1).getFirstParagraph().getParagraphFormat().getAlignment());
break;
}
value - A value that specifies how to align contents in tables when exporting into the SaveFormat.MARKDOWN format. The value must be one of TableContentAlignment constants.public java.lang.String getImagesFolder()
SaveFormat.MARKDOWN format. Default is an empty string.
Remarks:
When you save a Document in SaveFormat.MARKDOWN format, Aspose.Words needs to save all images embedded in the document as standalone files. getImagesFolder() / setImagesFolder(java.lang.String) allows you to specify where the images will be saved.
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 getImagesFolder() / setImagesFolder(java.lang.String) 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 getImagesFolder() / setImagesFolder(java.lang.String) property.
If the folder specified by getImagesFolder() / setImagesFolder(java.lang.String) doesn't exist, it will be created automatically.
Examples:
Shows how to specifies the name of the folder used to construct image URIs.
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Some image below:");
builder.insertImage(getImageDir() + "Logo.jpg");
String imagesFolder = Paths.get(getArtifactsDir(), "ImagesDir").toString();
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// Use the "ImagesFolder" property to assign a folder in the local file system into which
// Aspose.Words will save all the document's linked images.
saveOptions.setImagesFolder(imagesFolder);
// Use the "ImagesFolderAlias" property to use this folder
// when constructing image URIs instead of the images folder's name.
saveOptions.setImagesFolderAlias("http://example.com/images");
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.ImagesFolder.md", saveOptions);
String value.public void setImagesFolder(java.lang.String value)
SaveFormat.MARKDOWN format. Default is an empty string.
Remarks:
When you save a Document in SaveFormat.MARKDOWN format, Aspose.Words needs to save all images embedded in the document as standalone files. getImagesFolder() / setImagesFolder(java.lang.String) allows you to specify where the images will be saved.
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 getImagesFolder() / setImagesFolder(java.lang.String) 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 getImagesFolder() / setImagesFolder(java.lang.String) property.
If the folder specified by getImagesFolder() / setImagesFolder(java.lang.String) doesn't exist, it will be created automatically.
Examples:
Shows how to specifies the name of the folder used to construct image URIs.
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Some image below:");
builder.insertImage(getImageDir() + "Logo.jpg");
String imagesFolder = Paths.get(getArtifactsDir(), "ImagesDir").toString();
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// Use the "ImagesFolder" property to assign a folder in the local file system into which
// Aspose.Words will save all the document's linked images.
saveOptions.setImagesFolder(imagesFolder);
// Use the "ImagesFolderAlias" property to use this folder
// when constructing image URIs instead of the images folder's name.
saveOptions.setImagesFolderAlias("http://example.com/images");
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.ImagesFolder.md", saveOptions);
value - The corresponding String value.public java.lang.String getImagesFolderAlias()
Remarks:
When you save a Document in SaveFormat.MARKDOWN format, Aspose.Words needs to save all images embedded in the document as standalone files. getImagesFolder() / setImagesFolder(java.lang.String) allows you to specify where the images will be saved and getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is not an empty string, then the image URI written to Markdown will be ImagesFolderAlias +
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is an empty string, then the image URI written to Markdown will be ImagesFolder +
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is set to '.' (dot), then the image file name will be written to Markdown without path regardless of other options.
Examples:
Shows how to specifies the name of the folder used to construct image URIs.
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Some image below:");
builder.insertImage(getImageDir() + "Logo.jpg");
String imagesFolder = Paths.get(getArtifactsDir(), "ImagesDir").toString();
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// Use the "ImagesFolder" property to assign a folder in the local file system into which
// Aspose.Words will save all the document's linked images.
saveOptions.setImagesFolder(imagesFolder);
// Use the "ImagesFolderAlias" property to use this folder
// when constructing image URIs instead of the images folder's name.
saveOptions.setImagesFolderAlias("http://example.com/images");
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.ImagesFolder.md", saveOptions);
String value.getImagesFolder(),
setImagesFolder(java.lang.String),
getImageSavingCallback(),
setImageSavingCallback(com.aspose.words.IImageSavingCallback)public void setImagesFolderAlias(java.lang.String value)
Remarks:
When you save a Document in SaveFormat.MARKDOWN format, Aspose.Words needs to save all images embedded in the document as standalone files. getImagesFolder() / setImagesFolder(java.lang.String) allows you to specify where the images will be saved and getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) allows to specify how the image URIs will be constructed.
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is not an empty string, then the image URI written to Markdown will be ImagesFolderAlias +
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is an empty string, then the image URI written to Markdown will be ImagesFolder +
If getImagesFolderAlias() / setImagesFolderAlias(java.lang.String) is set to '.' (dot), then the image file name will be written to Markdown without path regardless of other options.
Examples:
Shows how to specifies the name of the folder used to construct image URIs.
DocumentBuilder builder = new DocumentBuilder();
builder.writeln("Some image below:");
builder.insertImage(getImageDir() + "Logo.jpg");
String imagesFolder = Paths.get(getArtifactsDir(), "ImagesDir").toString();
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// Use the "ImagesFolder" property to assign a folder in the local file system into which
// Aspose.Words will save all the document's linked images.
saveOptions.setImagesFolder(imagesFolder);
// Use the "ImagesFolderAlias" property to use this folder
// when constructing image URIs instead of the images folder's name.
saveOptions.setImagesFolderAlias("http://example.com/images");
builder.getDocument().save(getArtifactsDir() + "MarkdownSaveOptions.ImagesFolder.md", saveOptions);
value - The corresponding String value.getImagesFolder(),
setImagesFolder(java.lang.String),
getImageSavingCallback(),
setImageSavingCallback(com.aspose.words.IImageSavingCallback)public IImageSavingCallback getImageSavingCallback()
SaveFormat.MARKDOWN format.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
IImageSavingCallback value.public void setImageSavingCallback(IImageSavingCallback value)
SaveFormat.MARKDOWN format.
Examples:
Shows how to rename the image name during saving into Markdown document.
public void renameImages() throws Exception {
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
// If we convert a document that contains images into Markdown, we will end up with one Markdown 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.
saveOptions.setImageSavingCallback(new SavedImageRename("MarkdownSaveOptions.HandleDocument.md"));
saveOptions.setSaveFormat(SaveFormat.MARKDOWN);
// The ImageSaving() method of our callback will be run at this time.
doc.save(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md", saveOptions);
Supplier<Stream<String>> filteredShapes = () -> DocumentHelper.directoryGetFiles(
getArtifactsDir(), "*").stream().
filter(s -> s.startsWith(getArtifactsDir() + "MarkdownSaveOptions.HandleDocument.md shape"));
Assert.assertEquals(1, filteredShapes.get().filter(f -> f.endsWith(".jpeg")).count());
Assert.assertEquals(8, filteredShapes.get().filter(f -> f.endsWith(".png")).count());
}
/// <summary>
/// Renames saved images that are produced when an Markdown document is saved.
/// </summary>
public static class SavedImageRename implements IImageSavingCallback
{
public SavedImageRename(String outFileName)
{
mOutFileName = outFileName;
}
public void imageSaving(ImageSavingArgs args) throws Exception
{
String imageFileName = MessageFormat.format("{0} shape {1}, of type {2}.{3}",
mOutFileName, ++mCount, args.getCurrentShape().getShapeType(),
FilenameUtils.getExtension(args.getImageFileName()));
args.setImageFileName(imageFileName);
args.setImageStream(new FileOutputStream(getArtifactsDir() + imageFileName));
Assert.assertTrue(args.isImageAvailable());
Assert.assertFalse(args.getKeepImageStreamOpen());
}
private int mCount;
private String mOutFileName;
}
value - The corresponding IImageSavingCallback value.public IResourceSavingCallback getResourceSavingCallback()
SaveFormat.MARKDOWN format.
Remarks:
Note, there is only one type of resources in Markdown. These are images. When you specify both getImageSavingCallback() / setImageSavingCallback(com.aspose.words.IImageSavingCallback) and getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback), then first is called getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback). However, note it is not necessary to have both implementations, as ImageSavingArgs is actually a subset of ResourceSavingArgs.
Examples:
Shows how to use a callback to change the resource URI.
public void resourceSavingCallback() throws Exception
{
String outputPath = getArtifactsDir() + "MarkdownSaveOptions.ResourceSavingCallback.md";
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setResourceSavingCallback(new ChangeUriPath());
doc.save(outputPath, saveOptions);
DocumentHelper.findTextInFile(outputPath, "/uri/for/");
}
/// <summary>
/// Class implementing <see cref="IResourceSavingCallback"/>.
/// </summary>
private static class ChangeUriPath implements IResourceSavingCallback
{
public void resourceSaving(ResourceSavingArgs args)
{
args.setResourceFileUri(MessageFormat.format("/uri/for/{0}", args.getResourceFileName()));
}
}
IResourceSavingCallback value.public void setResourceSavingCallback(IResourceSavingCallback value)
SaveFormat.MARKDOWN format.
Remarks:
Note, there is only one type of resources in Markdown. These are images. When you specify both getImageSavingCallback() / setImageSavingCallback(com.aspose.words.IImageSavingCallback) and getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback), then first is called getResourceSavingCallback() / setResourceSavingCallback(com.aspose.words.IResourceSavingCallback). However, note it is not necessary to have both implementations, as ImageSavingArgs is actually a subset of ResourceSavingArgs.
Examples:
Shows how to use a callback to change the resource URI.
public void resourceSavingCallback() throws Exception
{
String outputPath = getArtifactsDir() + "MarkdownSaveOptions.ResourceSavingCallback.md";
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setResourceSavingCallback(new ChangeUriPath());
doc.save(outputPath, saveOptions);
DocumentHelper.findTextInFile(outputPath, "/uri/for/");
}
/// <summary>
/// Class implementing <see cref="IResourceSavingCallback"/>.
/// </summary>
private static class ChangeUriPath implements IResourceSavingCallback
{
public void resourceSaving(ResourceSavingArgs args)
{
args.setResourceFileUri(MessageFormat.format("/uri/for/{0}", args.getResourceFileName()));
}
}
value - The corresponding IResourceSavingCallback value.public boolean getExportImagesAsBase64()
false.
Remarks:
When this property is set to true images data are exported directly into the img elements and separate files are not created.
Examples:
Shows how to save a .md document with images embedded inside it.
Document doc = new Document(getMyDir() + "Images.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); { saveOptions.setExportImagesAsBase64(exportImagesAsBase64); }
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportImagesAsBase64.md", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "MarkdownSaveOptions.ExportImagesAsBase64.md"), Charset.forName("UTF-8"));
Assert.assertTrue(exportImagesAsBase64
? outDocContents.contains("data:image/jpeg;base64")
: outDocContents.contains("MarkdownSaveOptions.ExportImagesAsBase64.001.jpeg"));
boolean value.public void setExportImagesAsBase64(boolean value)
false.
Remarks:
When this property is set to true images data are exported directly into the img elements and separate files are not created.
Examples:
Shows how to save a .md document with images embedded inside it.
Document doc = new Document(getMyDir() + "Images.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions(); { saveOptions.setExportImagesAsBase64(exportImagesAsBase64); }
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportImagesAsBase64.md", saveOptions);
String outDocContents = FileUtils.readFileToString(new File(getArtifactsDir() + "MarkdownSaveOptions.ExportImagesAsBase64.md"), Charset.forName("UTF-8"));
Assert.assertTrue(exportImagesAsBase64
? outDocContents.contains("data:image/jpeg;base64")
: outDocContents.contains("MarkdownSaveOptions.ExportImagesAsBase64.001.jpeg"));
value - The corresponding boolean value.public int getListExportMode()
MarkdownListExportMode.MARKDOWN_SYNTAX.
Remarks:
When this property is set to MarkdownListExportMode.PLAIN_TEXT all list labels are updated using Document.updateListLabels() and exported with their actual values. Such lists can be non-compatible with Markdown format and will be recognized as plain text upon importing in this case.
When this property is set to MarkdownListExportMode.MARKDOWN_SYNTAX, writer tries to export list items in manner that allows to numerate list items in automatic mode by Markdown.
Examples:
Shows how to list items will be written to the markdown document.
Document doc = new Document(getMyDir() + "List item.docx");
// Use MarkdownListExportMode.PlainText or MarkdownListExportMode.MarkdownSyntax to export list.
MarkdownSaveOptions options = new MarkdownSaveOptions(); { options.setListExportMode(markdownListExportMode); }
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ListExportMode.md", options);
int value. The returned value is one of MarkdownListExportMode constants.public void setListExportMode(int value)
MarkdownListExportMode.MARKDOWN_SYNTAX.
Remarks:
When this property is set to MarkdownListExportMode.PLAIN_TEXT all list labels are updated using Document.updateListLabels() and exported with their actual values. Such lists can be non-compatible with Markdown format and will be recognized as plain text upon importing in this case.
When this property is set to MarkdownListExportMode.MARKDOWN_SYNTAX, writer tries to export list items in manner that allows to numerate list items in automatic mode by Markdown.
Examples:
Shows how to list items will be written to the markdown document.
Document doc = new Document(getMyDir() + "List item.docx");
// Use MarkdownListExportMode.PlainText or MarkdownListExportMode.MarkdownSyntax to export list.
MarkdownSaveOptions options = new MarkdownSaveOptions(); { options.setListExportMode(markdownListExportMode); }
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ListExportMode.md", options);
value - The corresponding int value. The value must be one of MarkdownListExportMode constants.public boolean getExportUnderlineFormatting()
false.
Examples:
Shows how to export underline formatting as ++.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.setUnderline(Underline.SINGLE);
builder.write("Lorem ipsum. Dolor sit amet.");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setExportUnderlineFormatting(true);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportUnderlineFormatting.md", saveOptions);
public void setExportUnderlineFormatting(boolean value)
false.
Examples:
Shows how to export underline formatting as ++.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.setUnderline(Underline.SINGLE);
builder.write("Lorem ipsum. Dolor sit amet.");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setExportUnderlineFormatting(true);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportUnderlineFormatting.md", saveOptions);
value - A boolean value indicating either to export underline text formatting as sequence of two plus characters "++".public int getLinkExportMode()
MarkdownLinkExportMode.AUTO.
Examples:
Shows how to links will be written to the .md file.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertShape(ShapeType.BALLOON, 100.0, 100.0);
// Image will be written as reference:
// ![ref1]
//
// [ref1]: aw_ref.001.png
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setLinkExportMode(MarkdownLinkExportMode.REFERENCE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.LinkExportMode.Reference.md", saveOptions);
// Image will be written as inline:
// 
saveOptions.setLinkExportMode(MarkdownLinkExportMode.INLINE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.LinkExportMode.Inline.md", saveOptions);
int value. The returned value is one of MarkdownLinkExportMode constants.public void setLinkExportMode(int value)
MarkdownLinkExportMode.AUTO.
Examples:
Shows how to links will be written to the .md file.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.insertShape(ShapeType.BALLOON, 100.0, 100.0);
// Image will be written as reference:
// ![ref1]
//
// [ref1]: aw_ref.001.png
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setLinkExportMode(MarkdownLinkExportMode.REFERENCE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.LinkExportMode.Reference.md", saveOptions);
// Image will be written as inline:
// 
saveOptions.setLinkExportMode(MarkdownLinkExportMode.INLINE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.LinkExportMode.Inline.md", saveOptions);
value - The corresponding int value. The value must be one of MarkdownLinkExportMode constants.public int getOfficeMathExportMode()
MarkdownOfficeMathExportMode.TEXT.
Examples:
Shows how OfficeMath will be written to the document.
Document doc = new Document(getMyDir() + "Office math.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setOfficeMathExportMode(MarkdownOfficeMathExportMode.IMAGE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.OfficeMathExportMode.md", saveOptions);
Shows how to export OfficeMath object as Latex.
Document doc = new Document(getMyDir() + "Office math.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setOfficeMathExportMode(MarkdownOfficeMathExportMode.LATEX);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportOfficeMathAsLatex.md", saveOptions);
int value. The returned value is one of MarkdownOfficeMathExportMode constants.public void setOfficeMathExportMode(int value)
MarkdownOfficeMathExportMode.TEXT.
Examples:
Shows how OfficeMath will be written to the document.
Document doc = new Document(getMyDir() + "Office math.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setOfficeMathExportMode(MarkdownOfficeMathExportMode.IMAGE);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.OfficeMathExportMode.md", saveOptions);
Shows how to export OfficeMath object as Latex.
Document doc = new Document(getMyDir() + "Office math.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setOfficeMathExportMode(MarkdownOfficeMathExportMode.LATEX);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportOfficeMathAsLatex.md", saveOptions);
value - The corresponding int value. The value must be one of MarkdownOfficeMathExportMode constants.public int getExportAsHtml()
MarkdownExportAsHtml.NONE.
Examples:
Shows how to export a table to Markdown as raw HTML.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Sample table:");
// Create table.
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Cell1");
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Cell2");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setExportAsHtml(MarkdownExportAsHtml.TABLES);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportTableAsHtml.md", saveOptions);
int value. The returned value is a bitwise combination of MarkdownExportAsHtml constants.public void setExportAsHtml(int value)
MarkdownExportAsHtml.NONE.
Examples:
Shows how to export a table to Markdown as raw HTML.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("Sample table:");
// Create table.
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.RIGHT);
builder.write("Cell1");
builder.insertCell();
builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
builder.write("Cell2");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setExportAsHtml(MarkdownExportAsHtml.TABLES);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ExportTableAsHtml.md", saveOptions);
value - The corresponding int value. The value must be a bitwise combination of MarkdownExportAsHtml constants.public int getImageResolution()
96 dpi.
Examples:
Shows how to set the output resolution for images.
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setImageResolution(300);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ImageResolution.md", saveOptions);
int value.public void setImageResolution(int value)
96 dpi.
Examples:
Shows how to set the output resolution for images.
Document doc = new Document(getMyDir() + "Rendering.docx");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setImageResolution(300);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.ImageResolution.md", saveOptions);
value - The corresponding int value.public int getEmptyParagraphExportMode()
MarkdownEmptyParagraphExportMode.EMPTY_LINE.
Examples:
Shows how to export empty paragraphs.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("First");
builder.writeln("\r\n\r\n\r\n");
builder.writeln("Last");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setEmptyParagraphExportMode(exportMode);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.EmptyParagraphExportMode.md", saveOptions);
String result = FileUtils.readFileToString( new File(getArtifactsDir() + "MarkdownSaveOptions.EmptyParagraphExportMode.md"), StandardCharsets.UTF_8);
switch (exportMode)
{
case MarkdownEmptyParagraphExportMode.NONE:
Assert.assertEquals("First\r\n\r\nLast\r\n", result);
break;
case MarkdownEmptyParagraphExportMode.EMPTY_LINE:
Assert.assertEquals("First\r\n\r\n\r\n\r\n\r\nLast\r\n\r\n", result);
break;
case MarkdownEmptyParagraphExportMode.MARKDOWN_HARD_LINE_BREAK:
Assert.assertEquals("First\r\n\\\r\n\\\r\n\\\r\n\\\r\n\\\r\nLast\r\n<br>\r\n", result);
break;
}
int value. The returned value is one of MarkdownEmptyParagraphExportMode constants.public void setEmptyParagraphExportMode(int value)
MarkdownEmptyParagraphExportMode.EMPTY_LINE.
Examples:
Shows how to export empty paragraphs.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.writeln("First");
builder.writeln("\r\n\r\n\r\n");
builder.writeln("Last");
MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
saveOptions.setEmptyParagraphExportMode(exportMode);
doc.save(getArtifactsDir() + "MarkdownSaveOptions.EmptyParagraphExportMode.md", saveOptions);
String result = FileUtils.readFileToString( new File(getArtifactsDir() + "MarkdownSaveOptions.EmptyParagraphExportMode.md"), StandardCharsets.UTF_8);
switch (exportMode)
{
case MarkdownEmptyParagraphExportMode.NONE:
Assert.assertEquals("First\r\n\r\nLast\r\n", result);
break;
case MarkdownEmptyParagraphExportMode.EMPTY_LINE:
Assert.assertEquals("First\r\n\r\n\r\n\r\n\r\nLast\r\n\r\n", result);
break;
case MarkdownEmptyParagraphExportMode.MARKDOWN_HARD_LINE_BREAK:
Assert.assertEquals("First\r\n\\\r\n\\\r\n\\\r\n\\\r\n\\\r\nLast\r\n<br>\r\n", result);
break;
}
value - The corresponding int value. The value must be one of MarkdownEmptyParagraphExportMode constants.