Fenced Code

In this example, we will walk you through how to use the fenced code feature with Aspose.Words for .NET. fenced code is used to represent blocks of code with specific formatting.

Step 1: Using a document generator

First, we’ll use a document generator to add content to our document.

DocumentBuilder builder = new DocumentBuilder();

Step 2: Adding a style for fenced code

We will add a custom style for the fenced code using the Styles.Add method of the Document object. In this example, we are creating a style called “FencedCode” for the fenced code.

Style fencedCode = builder.Document.Styles.Add(StyleType.Paragraph, "FencedCode");
builder.ParagraphFormat.Style = fencedCode;

Step 3: Adding fenced code without info

Now we can add a fenced code block with no information string using the “FencedCode” custom style.

builder.Writeln("This is an fenced code");

Step 4: Add fenced code with info string

We can also add a fenced code block with a string of information using another custom style. In this example, we are creating a style called “FencedCode.C#” to represent a block of C# code.

Style fencedCodeWithInfo = builder.Document.Styles.Add(StyleType.Paragraph, "FencedCode.C#");
builder.ParagraphFormat.Style = fencedCodeWithInfo;
builder.Writeln("This is a fenced code with info string");

Example source code for Fenced Code using Aspose.Words for .NET

// Use a document builder to add content to the document.
DocumentBuilder builder = new DocumentBuilder();

Style fencedCode = builder.Document.Styles.Add(StyleType.Paragraph, "FencedCode");
builder.ParagraphFormat.Style = fencedCode;
builder.Writeln("This is an fenced code");

Style fencedCodeWithInfo = builder.Document.Styles.Add(StyleType.Paragraph, "FencedCode.C#");
builder.ParagraphFormat.Style = fencedCodeWithInfo;
builder.Writeln("This is a fenced code with info string");

FAQ’s

Q: What is delimited code in Markdown?

A: Delimited code in Markdown is a formatting method used to display code in a Markdown document. It consists of framing the code with specific delimiters.

Q: What are the benefits of delimited code in Markdown?

A: Delimited code in Markdown improves code readability and makes it easier for readers to understand. It also allows preserving syntax highlighting in some Markdown editors.

Q: What is the difference between delimited and indented code in Markdown?

A: Delimited code uses specific delimiters to enclose the code, while indented code involves indenting each line of code with spaces or tabs.

Q: Is delimited code in Markdown supported by all Markdown editors?

A: Support for delimited code in Markdown may vary between Markdown editors. Check your publisher’s specific documentation to be sure.