The following code loads and converts markdown to docx, but the output still contains markdown tags i.e. #
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder();
Document doc = new Document();
string contents = File.ReadAllText("input.md");
builder.Write(contents);
MemoryStream wordsStream = new MemoryStream();
// Save the document to a MemoryStream in PDF format
builder.Document.Save(wordsStream, Aspose.Words.SaveFormat.Docx);
builder.Document.Save("outputDocumentBuilder.docx", SaveFormat.Docx);
However, the output still contains the markdown tags.
input.zip (7.0 KB)
@kashif.iqbal
It appears that the issue with your Markdown not converting properly to DOCX is related to how you are loading and saving the document. Instead of using DocumentBuilder to write the contents, you should directly load the Markdown file into a Document object. Here’s a corrected version of your code:
// Load the Markdown document
Document doc = new Document("input.md");
// Save as DOCX
doc.Save("outputDocument.docx", SaveFormat.Docx);
This approach ensures that the Markdown tags are processed correctly and should not appear in the output DOCX file. Ensure that your Markdown content is properly formatted. If you continue to experience issues, consider updating to the latest version of Aspose.Words.
For advanced Markdown features, please check the Aspose documentation for supported features, as some may not be fully processed.
Why not to use DocumentBuilder? It is must for me to use DocumetnBuilder.
@kashif.iqbal DocumentBuilder.Write method does not interpret Markdown syntax. This is by design. We already have WORDSNET-25733 feature request to add DocumentBuilder.InsetMarkdown method.
If you need to use DocumentBuilder for inserting Markdown, you can load it into a separate document and then use DocumentBuilder.InsertDocument or DocumentBuilder.InsertDocumentInline method. For example see the following code:
string md = "**bold text** *italic text*";
// Load markdown string into a temporary document.
LoadOptions loadOptions = new LoadOptions() { LoadFormat = LoadFormat.Markdown };
Document tmpDoc = new Document(new MemoryStream(Encoding.UTF8.GetBytes(md)), loadOptions);
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert temporary document into the destination document.
builder.InsertDocumentInline(tmpDoc, ImportFormatMode.UseDestinationStyles, new ImportFormatOptions());
doc.Save("C:\\temp\\out.docx");