Markdown is great—it provides an easy way to write text that can be converted into HTML. But working with raw Markdown programmatically can be a pain. There are plenty of libraries for working with the HTML Markdown can generate, but not many good ones for working directly with Markdown.
Aspose.Words solves this problem by making it easy for .NET developers to load Markdown, work with it programmatically, and write it to a file.
In this article, we’ll use a .NET application to edit and save a Markdown file. Working with Markdown programmatically can be tough, but Aspose.Words makes it a breeze.
Setting Up Your Project
The easiest way to start using Aspose.Words is to use NuGet to download the required DLLs. I created a simple WinForms application that calls some code to create the Markdown document on a button click.
To add the NuGet package to your project, right-click on your solution and then click Manage NuGet Packages. Search for the term "Aspose.Words" and NuGet will display Aspose.Words as the first result. Now you can add the latest version of Aspose.Words to your project.
Once NuGet has completed, you’ll see the Aspose.Words reference added to your project:
We can now start creating some documents.
The Markdown File
The file we’ll be editing looks like this:
Create a new text file in Visual Studio Code (or any other text editor) and paste the following text into your file. If you use Visual Studio Code, you can save it as a Markdown file.
# Welcome to Aspose.Words
This is a demonstration of **Aspose.Words** that can be used to manipulate Markdown files.
> Markdown makes it easy to format plain text that can be converted to
> HTML.
This will be done in Visual Studio 2019 using C#.
This will be done in Visual Studio 2019 using C#.
With the file created, we’re ready to write some code.
Writing the Code
Before you can create a Markdown document, you need to add the following using statements to the code:
using Aspose.Words;
using Aspose.Words.Replacing;
Next, create a method called ProcessMarkdown
as follows:
private void ProcessMarkdown()
{
}
Inside this method, add a reference to the folder you saved your file to:
var docPath = "C:\\temp\\aspose\\";
var fileToLoad = "snippet.md";
If you want to work with the file, you need to load it into a new Document instance:
var doc = new Document(Path.Combine(docPath, fileToLoad));
Looking back at the rendered Markdown file, I want to change the word "demonstration" to "code demonstration" and format these words in italics. To do this, I’ll use the FindReplaceOptions
feature to apply formatting to the replaced text as follows:
var options = new FindReplaceOptions();
options.ApplyFont.Italic = true;
_ = doc.Range.Replace("demonstration", "code demonstration", options);
I then save this Markdown file to a new document:
_ = doc.Save(Path.Combine(docPath, "snippetModified.md"),
SaveFormat.Markdown);
Run your project and generate the new Markdown document. When you view the rendered Markdown, you’ll see the change applied.
This is great, but you can do so much more with Aspose.Words. You can, for example, print the document you just created. I’ll use a PDF printer to illustrate this, but you can try printing to a physical printer.
Note that you can save the created document as a PDF, too, but as I’m printing to a PDF printer, PDF is the file format it will create.
To print a document using Aspose.Words, simply add the following line of code:
doc.Print("Foxit Reader PDF Printer");
Of course, you’ll need to replace the text "Foxit Reader PDF Printer" with your own printer name.
Adding Some Text
With Aspose.Words, you can easily add text and formatting to your Markdown file.
To do this, you need to add Section, Body, and Paragraph classes and append them to your document. (See the Aspose.Words namespace in the documentation for details on these classes.) I also want to be able to specify some paragraph formats.
To start, I’ll add an enum:
public enum StyleNames { None, Heading1, Heading2, Quote }
The next bit of code is a CreateParagraph
method that will create the Section, Body, and Paragraph for me and apply any paragraph formatting I specify.
private Paragraph CreateParagraph(Document doc, StyleNames styleName = StyleNames.None)
{
var section = new Section(doc);
_ = doc.AppendChild(section);
var body = new Body(doc);
_ = section.AppendChild(body);
var paragraph = new Paragraph(doc);
_ = body.AppendChild(paragraph);
switch (styleName)
{
case StyleNames.Heading1:
_ = paragraph.ParagraphFormat.StyleName = "Heading 1";
break;
case StyleNames.Quote:
_ = paragraph.ParagraphFormat.StyleName = "Quote";
break;
case StyleNames.Heading2:
_ = paragraph.ParagraphFormat.StyleName = "Heading 2";
break;
default:
break;
}
return paragraph;
}
To add some text to my paragraph, I need to create a Run, which represents a sequence of characters all in the same font. I also want to specify whether the text should be formatted as bold or italics.
I add a method called CreateRun
as follows:
private Run CreateRun(Document doc, string text, bool isBold = false, bool isItalic = false)
{
var textRun = new Run(doc);
textRun.Text = text;
textRun.Font.Bold = isBold;
textRun.Font.Italic = isItalic;
return textRun;
}
This allows me to reuse the code to generate multiple paragraphs and apply text with formatting to those paragraphs. I’ll now create four new paragraphs in my Markdown file.
The first paragraph will just be a heading with the Heading 1 style applied:
var paragraph0 = CreateParagraph(doc, StyleNames.Heading1);
_ = paragraph0.AppendChild(CreateRun(doc, "This is a Heading 1"));
The second paragraph will add some bold text to the Markdown file:
var paragraph1 = CreateParagraph(doc);
_ = paragraph1.AppendChild(CreateRun(doc, "This is some bold text", true));
The third paragraph will add a heading with the Heading 2 style applied:
var paragraph2 = CreateParagraph(doc, StyleNames.Heading2);
_ = paragraph2.AppendChild(CreateRun(doc, "This is a Heading 2"));
The fourth paragraph will add some text formatted as italic to the file:
var paragraph3 = CreateParagraph(doc);
_ = paragraph3.AppendChild(CreateRun(doc, "This is some Italic text",
false, true));
The last paragraph will add a quote to the document:
var paragraph4 = CreateParagraph(doc, StyleNames.Quote);
_ = paragraph4.AppendChild(CreateRun(doc,
"This is a quote or something profound"));
When you view the rendered Markdown file, you’ll see the new paragraphs added along with their styles and formatting.
The Markdown generated by the code looks like this:
# Welcome to Aspose.Words
This is a *code demonstration* of **Aspose.Words** that can be used to manipulate Markdown files.
> Markdown makes it easy to format plain text that can be converted to
> HTML.
This will be done in Visual Studio 2019 using C#.
# This is a Heading 1
**This is some bold text**
## This is a Heading 2
*This is some Italic text*
> This is a quote or something profound
As you can see, generating Markdown files in code using Aspose.Words is incredibly simple. With just a few lines, you can create a variety of Markdown files. The code took just a matter of minutes to write.
Generating a Word Document
Aspose.Words also gives you the power to create a Word document from the same code used to create the Markdown file. Simply modify the Save method as follows:
_ = doc.Save(Path.Combine(docPath, "snippetModified.docx"),
SaveFormat.Docx);
This will generate a docx file. If you want to protect your Word document, it’s as easy as calling the Protect method on the Document class as follows:
doc.Protect(ProtectionType.ReadOnly, "password");
_ = doc.Save(Path.Combine(docPath, "snippetModified.docx"),
SaveFormat.Docx);
The Protect method allows you to specify a ProtectionType
and a password. If you open the generated Word document and try to edit it, you’ll see the Restrict Editing pane pop up:
Clicking on the Stop Protection button allows the user to enter a password to unprotect the document and allow editing.
Conclusion
In no time at all, we were able to open a Markdown file, modify its contents, print the document as a PDF, create a new Markdown file, and create a protected Word document from the same code.
If you want to learn more about Aspose.Words, head over to the site to see what Aspose.Words can do for you.