How to insert Markdown into word

How to insert Markdown into word

@GusGus

To insert Markdown content into a Word document using Aspose.Words, you can utilize the DocumentBuilder class to create and manipulate the document. Below is a simple example demonstrating how to append Markdown content at the end of a Word document.

Example Code

Here’s a C# example that shows how to create a Word document and insert Markdown content at the end:

using System;
using Aspose.Words;

class Program
{
    static void Main()
    {
        // Create a new document
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        // Add some initial content
        builder.Writeln("This is the beginning of the document.");

        // Now, let's add Markdown content
        string markdownContent = "# Heading 1\nThis is a paragraph with **bold** text and *italic* text.\n- Item 1\n- Item 2\n";
        
        // Insert the Markdown content
        builder.Writeln(markdownContent);

        // Save the document
        doc.Save("Output.docx");
    }
}

Explanation

  1. Creating a Document: A new Document object is created, and a DocumentBuilder is initialized to facilitate content insertion.
  2. Adding Initial Content: You can add any initial content to the document using the Writeln method.
  3. Inserting Markdown: The Markdown content is defined as a string and then inserted into the document using the Writeln method of the DocumentBuilder.
  4. Saving the Document: Finally, the document is saved in the desired format (e.g., DOCX).

Important Notes

  • The Markdown content will be treated as plain text in the Word document. If you need to convert Markdown to formatted text, you may need to parse the Markdown and apply the corresponding formatting using the DocumentBuilder methods.
  • Ensure you have the Aspose.Words library referenced in your project to use the above code.

This approach allows you to easily append Markdown content to a Word document programmatically. If you have any further questions or need additional examples, feel free to ask!

@GusGus You can load markdown string into a temporary document and then insert it into the destination document. For example see the following simple code:

String md = "**bold text** *italic text*";
// Load markdown string into a temporary document.
LoadOptions loadOptions = new LoadOptions();
loadOptions.setLoadFormat(LoadFormat.MARKDOWN);
Document tmpDoc = new Document(new ByteArrayInputStream(md.getBytes()), loadOptions);

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert temporary document into the destination document.
builder.insertDocumentInline(tmpDoc, ImportFormatMode.USE_DESTINATION_STYLES, new ImportFormatOptions());
doc.save("C:\\temp\\out.docx");

Thanks,but I still have a problem, how to auot adjust pictures in word to make sure their width is not beyond the word page size.

@GusGus You can try using code like this to make sure width of shape is not wider than page width:

for (Shape s : (Iterable<Shape>)doc.getChildNodes(NodeType.SHAPE, true))
{
    if (!s.isTopLevel())
        continue;

    // Get parent section.
    Section sect = (Section)s.getAncestor(NodeType.SECTION);
    PageSetup ps = sect.getPageSetup();

    if (s.getWidth() > ps.getPageWidth())
    {
        // Resize shape if it width is wider than page.
        s.setAspectRatioLocked(true);
        s.setWidth(ps.getPageWidth());
    }
}