Example T632549
Visible to All Users

Word Processing Document API - Use DocumentIterator to Export a Document in a Custom Format

This example demonstrates how to use a DocumentIterator object and a DocumentVisitor implementation to export a document in a custom format. The DocumentIterator navigates through document elements. The document visitor is used to export document elements to a lightweight markup language (Markdown).

Implementation

The document visitor is implemented according to the Visitor pattern. In this example, the custom document visitor is inherited from the DocumentVisitorBase class. The DocumentVisitorBase class implements the IDocumentVisitor interface. This means you can override only the methods of the DocumentVisitorBase class to process documents.

The document iterator moves over each document structure item and passes a custom document visitor object to the current item. The custom document visitor builds a string with lightweight markup for the processed document item.

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Program.cs(vb)
C#
using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using System; using System.Diagnostics; using System.IO; namespace DocumentIteratorExample { class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer()) { wordProcessor.LoadDocument("Markdown.docx", DocumentFormat.OpenXml); Document document = wordProcessor.Document; MarkdownVisitor visitor = new MarkdownVisitor(); Iterate(visitor, document); string newFilePath = "output.md"; File.WriteAllText(newFilePath, visitor.Text); } var p = new Process(); p.StartInfo = new ProcessStartInfo(@"output.md") { UseShellExecute = true }; p.Start(); } private static void Iterate(IDocumentVisitor visitor, Document document) { DocumentIterator iterator = new DocumentIterator(document, true); while (iterator.MoveNext()) iterator.Current.Accept(visitor); } } }
MarkdownVisitor.cs(vb)
C#
using DevExpress.XtraRichEdit.API.Native; using System; using System.Linq; using System.Text; namespace DocumentIteratorExample { public abstract class BufferedDocumentVisitor : DocumentVisitorBase { readonly StringBuilder buffer; protected BufferedDocumentVisitor() { this.buffer = new StringBuilder(); } protected StringBuilder Buffer { get { return buffer; } } } public class MarkdownVisitor : BufferedDocumentVisitor { const char lastLowSpecial = '\x1f'; const char firstHighSpecial = '\xffff'; public string Text { get { return Buffer.ToString(); } } public override void Visit(DocumentText text) { string prefix = GetPrefix(text.TextProperties); Buffer.Append(prefix); int count = text.Length; for (int i = 0; i < count; i++) { char ch = text.Text[i]; if (ch > lastLowSpecial && ch < firstHighSpecial) Buffer.Append(ch); else if (ch == '\x9' || ch == '\xA' || ch == '\xD') Buffer.Append(ch); } Buffer.Append(prefix); } public override void Visit(DocumentParagraphStart paragraphStart) { if (paragraphStart.ParagraphProperties.ParagraphStyle.Name == "heading 1") Buffer.Append("#"); } public override void Visit(DocumentInlinePicture inlinePicture) { InsertImageUri(inlinePicture.Uri); } public override void Visit(DocumentPicture picture) { InsertImageUri(picture.Uri); } public override void Visit(DocumentParagraphEnd paragraphEnd) { AppendLineOnNonEmptyContent(); } public override void Visit(DocumentSectionEnd sectionEnd) { AppendLineOnNonEmptyContent(); } public override void Visit(DocumentHyperlinkStart hyperlinkStart) { if (!String.IsNullOrEmpty(hyperlinkStart.NavigateUri)) Buffer.Append("["); } public override void Visit(DocumentHyperlinkEnd hyperlinkEnd) { if (!String.IsNullOrEmpty(hyperlinkEnd.NavigateUri)) Buffer.Append(String.Format("]({0})", hyperlinkEnd.NavigateUri)); } void InsertImageUri(string uri) { if (!string.IsNullOrEmpty(uri)) Buffer.Append(String.Format("![]({0})", uri)); else Buffer.Append("[[img src=attached-image.jpg alt=foobar]]"); } string GetPrefix(ReadOnlyTextProperties properties) { string prefix = string.Empty; if (properties.FontBold) prefix = "**"; if (properties.FontItalic) prefix += "*"; if (properties.StrikeoutType == StrikeoutType.Single) prefix += "~~"; return prefix; } void AppendLineOnNonEmptyContent() { if (Buffer.Length > 0) Buffer.AppendLine(); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.