đź’ˇ Full working example available on GitHub: convert-documents-to-doclang-dotnet

Converting documents to DocLang is a GroupDocs.Conversion workflow for .NET that writes a PDF file, a Word document or an Outlook message as an XML description of what each element is, where it sits on its page and how its pieces connect. You choose between a DCLX package, which carries the pictures, and bare DCLG markup, and you can split any file into page chunks. The whole thing is one options class and a few lines of C#.

The Challenge: Three Formats, One Question

Picture a shared folder with a PDF guide, a Word report and an Outlook message in it. Someone asks which parts of those files are headings, which are tables and where each of them sits on its page. The PDF knows about pages and pictures, the Word report knows about styles and tables, and the message knows about header lines and a body. Answering the question means reading three formats in three different ways.

That gets worse when the folder keeps changing. A team that stores, indexes or reviews these files wants one description of their structure, written the same way every time, with the pictures kept where they are needed and left out where they are not.

When I ran the sample for this post, the conversion call turned out to be the short part; the decisions were about which output to ask for. A package or bare markup? Page pictures or not? One file or one file per page?

What the sample shows: the same two-page PDF came out as a 1,687,733-byte package, a 2,570,172-byte package with page pictures, and two chunks of 4,172 and 727 bytes. The choice of output matters more than the call.

The Solution: DocLang Through One Options Class

DocLang is an XML description of a document. GroupDocs.Conversion for .NET reads and writes two DocLang file types since 26.11. DCLG is bare markup: one XML file whose root is doclang, with no picture payload. DCLX is a package that holds the markup, the document’s pictures and, optionally, a picture of every page. The format itself is published as the DocLang format.

Everything runs through DocLangConvertOptions. Format selects the type, IncludeLocations and IncludePageImages switch page positions and page pictures, and PageNumber with PagesCount select consecutive pages. For pages that are not consecutive, Pages takes a list.

To follow along you need:

  • .NET SDK 8.0 or later
  • GroupDocs.Conversion for .NET 26.11.0 (get a temporary license to remove evaluation limits)
  • A PDF file, a Word document and an Outlook message to convert
dotnet add package GroupDocs.Conversion --version 26.11.0

Implementing the Conversion Step by Step

The sample folder holds sample.pdf (a two-page illustrated guide), sample.docx (a Word report) and sample.msg (an Outlook message). The five steps below write five kinds of output from them.

The code uses the namespaces GroupDocs.Conversion, GroupDocs.Conversion.Options.Convert and GroupDocs.Conversion.FileTypes, plus System.IO.Compression where a package is read back and System.Linq for the folder loop. In the snippets, inputPath is the source file (resources/sample.pdf, or resources/sample.docx in Step 2) and outputPath the file to write, such as output/guide.dclx. outputFolder is output/guide-chunks in Step 4 and output/batch in Step 5, and inputFolder is resources.

Step 1 - Write a Package

DCLX is the default, so an options object with nothing set is enough.

// DCLX is the default value of Format
using (var converter = new Converter(inputPath))
{
    converter.Convert(outputPath, new DocLangConvertOptions());
}

Opened as a zip archive, the guide’s package holds 11 parts: [Content_Types].xml (696 bytes), _rels/.rels (263 bytes), document.xml (5,062 bytes) and eight pictures under assets/. The package file is 1,687,733 bytes.

Step 2 - Write Bare Markup Without Positions

Switch the type to DCLG and turn locations off when the text and its structure are all you need.

var options = new DocLangConvertOptions
{
    Format = DocLangFileType.Dclg,
    IncludeLocations = false   // leave out where each element sits on its page
};

The size drops from 18,993 bytes with locations to 9,391 bytes without them when the Word report goes through this. Its root element is doclang, marked version="0.10", and the file holds <heading level="1"> and <heading level="2"> headings, text styled as bold, italic, underline, strikethrough, superscript and subscript, and <table> elements with <fcel /> cells.

Step 3 - How do I keep a picture of every page next to the markup?

Set IncludePageImages to true and keep the default DCLX format. The package then holds one picture per page under pages/. For the sample guide that added 882,439 bytes, taking the package from 1,687,733 to 2,570,172 bytes. DCLG cannot hold them: that combination throws InvalidConvertOptionsException, so keep the type on DCLX whenever page pictures are needed.

var options = new DocLangConvertOptions { IncludePageImages = true };

using (var converter = new Converter(inputPath))
{
    converter.Convert(outputPath, options);
}

The exception message reads: “DocLangConvertOptions.IncludePageImages cannot be combined with the dclg format: bare DocLang markup cannot hold page pictures. Use the dclx format for page pictures.”

Step 4 - Split a File Into Page Chunks

GetDocumentInfo gives the page count, and each conversion then takes one page.

int pages;
using (var converter = new Converter(inputPath))
{
    pages = converter.GetDocumentInfo().PagesCount;
}
for (int page = 1; page <= pages; page++)
{
    var options = new DocLangConvertOptions
    {
        Format = DocLangFileType.Dclg,
        PageNumber = page,
        PagesCount = 1
    };
    using (var converter = new Converter(inputPath))
    {
        converter.Convert(Path.Combine(outputFolder, $"page-{page}.dclg"), options);
    }
}

With 2 pages in the guide, the loop leaves two files behind, 4,172 and 727 bytes. Because the page number is in the file name, a chunk can be stored or indexed under that number.

Step 5 - Run a Whole Folder

The same options object handles every format, and an output named source file + .dclg keeps its origin visible.

var options = new DocLangConvertOptions { Format = DocLangFileType.Dclg };

foreach (string inputPath in Directory.GetFiles(inputFolder).OrderBy(path => path))
{
    string outputName = Path.GetFileName(inputPath) + ".dclg";
    using (var converter = new Converter(inputPath))
    {
        converter.Convert(Path.Combine(outputFolder, outputName), options);
    }
}

Three files come out of the sample’s folder: 18,993 bytes for the Word report, 1,140 bytes for the Outlook message and 4,804 bytes for the PDF. The message is small because it is little more than its header lines and body. In the PDF’s file each picture survives only as a <picture> element with its page location and no image data, since DCLG has no picture payload, next to the text, the tables and a <page_break />.

An Example Workflow: Storing and Reviewing Mixed Documents

Suppose a team keeps incoming PDF files, Word reports and Outlook messages in one archive and reviews them later. The folder run turns the three formats into one markup type in a single pass, and each result carries its source name. For the files that need their pages seen as well as read, the package with page pictures keeps both in one file.

The step-by-step code above maps onto that workflow directly. Step 5 gives a markup copy of every file, Step 4 gives a page-level record to point a reader to, and the package with page pictures is the copy a reviewer opens when the markup is not enough.

What the Choice of Output Changes

Output What it holds Size in the sample
DCLX package markup, pictures and positions 1,687,733 bytes (PDF)
DCLX with page pictures the package plus pages/1.png and pages/2.png 2,570,172 bytes (PDF)
DCLG with locations markup with page positions, no pictures 18,993 bytes (Word)
DCLG without locations markup only 9,391 bytes (Word)
DCLG chunks one file per page 4,172 and 727 bytes (PDF)

Pick the smallest output that still carries what the next step needs. If that step is a text pass, the Word report without locations is under 10,000 bytes. If it is a review, the package with page pictures costs about 880,000 bytes more than the plain package for this two-page guide, and in return every page can be looked at as well as read.

Other Places the Same Options Fit

  • Review copies: a package with page pictures lets a reviewer compare the markup with what the page looked like.
  • Per-page references: a chunk file named after its page number is a record you can store on its own.
  • Folder migrations: the folder loop converts a mixed directory without separate code for each format.

Without a license the sample runs in evaluation mode: the markup starts with an evaluation notice line and the sizes differ from the ones in this post, which come from a licensed run on GroupDocs.Conversion 26.11.0.

Getting Started

Additional Resources