Markdown content can be rendered in the pages of a PDF document created with the PdfDocument class, between the text, images and other elements added from code. The content starts at a position in the current page and flows to as many new pages as it needs, laid out inside the margins of the PDF document. The content keeps its structure, headings, links, code blocks and images.
The content is given to a PdfMarkdownElement object and rendered with the MarkdownPdfDocumentExtensionsAddMarkdown(PdfDocument, PdfMarkdownElement) extension method of the PDF document. The method returns a PdfContentRenderInfo object describing the pages and the areas where the content was rendered. The MarkdownPdfDocumentExtensionsAddMarkdownAsync(PdfDocument, PdfMarkdownElement, CancellationToken) method is the asynchronous variant.
The element is created with PdfMarkdownElementPdfMarkdownElement(String, String) from a Markdown string and the base URL used to resolve its relative links and images, or with PdfMarkdownElementPdfMarkdownElement(String) from the path of a Markdown file, whose relative links and images are resolved from the folder of the file.
// A Markdown string with a base URL for its relative links and images
PdfMarkdownElement markdownElement = new PdfMarkdownElement(markdownString, baseUrl);
// A Markdown file
PdfMarkdownElement markdownFileElement = new PdfMarkdownElement(markdownFilePath);The X and Y properties give the position where the content starts, in points and relative to the top left corner of the content area of the current page, inside the margins. The content keeps the left offset on the next pages, where it starts at the top margin. Width is the width of the content in points, or 0 to extend it to the right margin of the page. Zoom scales the rendered content, in percents from 10 to 200, with 100 as the default.
The StyleSheet property is the CSS style sheet applied to the rendered content, or empty for the default styles, as for the Markdown to PDF Converter. The raw markup embedded in the Markdown content is ignored when DisableRawHtml is true. The headings of the content join the outline of the PDF document when GenerateDocumentOutline is true. The FontFiles and FallbackFontFiles lists give more fonts to the content, in addition to the fonts installed on the machine.
markdownElement.X = 0;
markdownElement.Y = crtYPos;
// 0 extends the content to the right margin of the page
markdownElement.Width = 0;
markdownElement.Zoom = 100;
// The CSS applied to the rendered Markdown content
markdownElement.StyleSheet = styleSheet;
// The headings of the Markdown content join the outline of the PDF document
markdownElement.GenerateDocumentOutline = true;
PdfContentRenderInfo markdownRenderInfo = pdfDocument.AddMarkdown(markdownElement);The returned PdfContentRenderInfo object exposes the FirstPageNumber, LastPageNumber and PagesCount of the content and the area it covers on each page in RenderedRectangles. The elements added after the content go to its last page, below the LastPageRectangle.
// Use EVO PDF Namespace
using EvoPdf.Next;
public byte[] AddMarkdownToPdfDocument(string markdownString, string baseUrl, string styleSheet, string fontFilePath)
{
// Set the license key received after purchase to use the library in licensed mode; leave it commented for demo mode
// Licensing.LicenseKey = "your-license-key";
// The Markdown content: a string with a base URL for its relative links and images.
// The element can also be created from the path of a Markdown file
PdfMarkdownElement markdownElement = new PdfMarkdownElement(markdownString, baseUrl);
PdfDocumentCreateSettings pdfCreateSettings = new PdfDocumentCreateSettings()
{
PageSize = PdfPageSize.A4,
PageOrientation = PdfPageOrientation.Portrait,
Margins = new PdfMargins(36, 36, 36, 36),
Language = "en-US"
};
using PdfDocument pdfDocument = new PdfDocument(pdfCreateSettings);
pdfDocument.PdfDocumentInfo.Title = "Markdown Content in a PDF Document";
PdfBaseFont baseFont = PdfFontManager.CreateBaseFont(fontFilePath);
PdfFont titleFont = PdfFontManager.CreateFont(baseFont, 16f, PdfFontStyle.Bold, PdfColor.DarkBlue);
PdfFont textFont = PdfFontManager.CreateFont(baseFont, 10f, PdfFontStyle.Normal, PdfColor.Black);
// Elements created with the PDF creator before the document content
PdfTextElement titleElement = new PdfTextElement("Markdown Content in a PDF Document", titleFont)
{
X = 0,
Y = 0,
Width = pdfDocument.ContentWidth,
Alignment = PdfTextAlignment.Center
};
titleElement.Accessibility.StructureType = PdfStructureType.Heading1;
float crtYPos = pdfDocument.AddText(titleElement).LastPageRectangle.Bounds.Bottom + 10;
PdfTextElement introElement = new PdfTextElement(
"The text above and this paragraph are text elements of the PDF document. The Markdown content " +
"starts below them and continues on new pages when it does not fit in this one. " +
"The elements added after the Markdown content go to its last page, below it.", textFont)
{
X = 0,
Y = crtYPos,
Width = pdfDocument.ContentWidth
};
crtYPos = pdfDocument.AddText(introElement).LastPageRectangle.Bounds.Bottom + 15;
// The Markdown content is rendered in the PDF document from the given position of the
// current page and inside the page margins. The X position is kept on the next pages
markdownElement.X = 0;
markdownElement.Y = crtYPos;
// 0 extends the content to the right margin of the page
markdownElement.Width = 0;
markdownElement.Zoom = 100;
// The CSS applied to the rendered Markdown content
markdownElement.StyleSheet = styleSheet;
// The raw markup embedded in the Markdown content is rendered, or ignored when disabled
markdownElement.DisableRawHtml = false;
// The headings of the Markdown content join the outline of the PDF document
markdownElement.GenerateDocumentOutline = true;
PdfContentRenderInfo markdownRenderInfo = pdfDocument.AddMarkdown(markdownElement);
// The rendered area reported for the last page places the elements after the content
crtYPos = markdownRenderInfo.LastPageRectangle.Bounds.Bottom + 15;
PdfRectangleElement separator = new PdfRectangleElement(0, crtYPos, pdfDocument.ContentWidth, 1)
{
FillColor = PdfColor.DarkBlue,
BorderColor = null
};
pdfDocument.AddRectangle(separator);
PdfTextElement closingElement = new PdfTextElement(
string.Format("The Markdown content was rendered on {0} page(s), from page {1} to page {2}. This text was added after it.",
markdownRenderInfo.PagesCount, markdownRenderInfo.FirstPageNumber, markdownRenderInfo.LastPageNumber), textFont)
{
X = 0,
Y = crtYPos + 8,
Width = pdfDocument.ContentWidth
};
pdfDocument.AddText(closingElement);
// The buffer to receive the generated PDF document
byte[] outPdfBuffer = pdfDocument.Save();
return outPdfBuffer;
}