# Convert documents to DocLang A walkthrough of 5 code patterns for converting PDF, Word and Outlook documents to DocLang packages and markup with GroupDocs.Conversion for .NET, and how to choose between them. {{< alert style="info" >}} 💡 Full working example available on GitHub: [convert-documents-to-doclang-dotnet](https://github.com/groupdocs-conversion/convert-documents-to-doclang-dotnet) {{< /alert >}} ## Executive Summary DocLang conversion is a GroupDocs.Conversion capability for .NET that writes a document as DocLang, an XML description of what each element is (a heading, a paragraph, a table, a picture), where it sits on its page and how its pieces connect. Since 26.11 the library reads and writes two DocLang file types: DCLG, bare markup, and DCLX, a package that holds the markup, the document's pictures and, optionally, a picture of every page. All five patterns on this page go through one options class, `DocLangConvertOptions`. The sample behind them handles PDF, Word and Outlook files only, and I ran it on GroupDocs.Conversion 26.11.0 with a license applied. Every size quoted below comes from that run. ## Before You Start You need the .NET SDK 8.0 or later and the package: ```bash dotnet add package GroupDocs.Conversion --version 26.11.0 ``` The snippets rely on these namespaces: `GroupDocs.Conversion` for `Converter`, `GroupDocs.Conversion.Options.Convert` for `DocLangConvertOptions`, `GroupDocs.Conversion.FileTypes` for `DocLangFileType`, `System.IO.Compression` where a package is read back as a zip archive, and `System.Linq` for the folder loop. Their variables stand for the values the sample passes. `inputPath` is `resources/sample.pdf`, or `resources/sample.docx` in Pattern 2. `outputPath` is the file to write, such as `output/guide.dclx`. `outputFolder` is `output/guide-chunks` in Pattern 4 and `output/batch` in Pattern 5, and `inputFolder` is `resources`. Without a license the sample runs in evaluation mode: the markup starts with an evaluation notice line and the sizes differ from those below. Apply a license, or request a [temporary license](https://purchase.groupdocs.com/temporary-license) while testing. For the two file types in the documentation, see [Convert to and from DocLang](https://docs.groupdocs.com/conversion/net/convert/doclang/), and for the option list the [DocLangConvertOptions reference](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.options.convert/doclangconvertoptions/). ## Choosing an Output | Pattern | Output | Options set | Source in the sample | Size | |---------|--------|-------------|----------------------|------| | 1. Package | DCLX | none | PDF | 1,687,733 bytes | | 2. Bare markup | DCLG | `Format`, `IncludeLocations` | Word | 9,391 bytes | | 3. Page pictures | DCLX | `IncludePageImages` | PDF | 2,570,172 bytes | | 4. Page chunks | one DCLG per page | `Format`, `PageNumber`, `PagesCount` | PDF | 4,172 and 727 bytes | | 5. Folder | one DCLG per file | `Format` | PDF, Word, Outlook | 4,804, 18,993 and 1,140 bytes | Pick DCLX when pictures have to travel with the markup, and DCLG when the markup is enough. The Word document is the one source that appears in two rows of numbers: as DCLG with locations it is 18,993 bytes, and Pattern 2 cuts it to 9,391 bytes. ## Common Pitfalls 1. Setting `IncludePageImages` while `Format` is `Dclg`. The conversion throws `InvalidConvertOptionsException`; return to DCLX. 2. Expecting pictures in DCLG. In the PDF's DCLG output each picture is a `` element that holds only its page location, with no image data, so keep DCLX when the pictures matter. 3. Comparing the sizes from an unlicensed run with the ones here. The evaluation notice changes them. 4. Reading `PagesCount` as a last page. It is a number of pages, and the chunk loop sets it to 1 together with `PageNumber`. ## The Five Patterns ### Pattern 1: Package **What it writes.** DCLX is the default value of `DocLangConvertOptions.Format`, so an options object with nothing set already produces a package: a zip archive with the markup, the pictures and the position of each element. **Code.** ```csharp using (var converter = new Converter(inputPath)) { converter.Convert(outputPath, new DocLangConvertOptions()); } ``` ```csharp using (ZipArchive package = ZipFile.OpenRead(outputPath)) { foreach (ZipArchiveEntry part in package.Entries) { Console.WriteLine($" {part.FullName} ({part.Length} bytes)"); } } ``` **What the sample measured.** Opening `guide.dclx` lists 11 parts: `[Content_Types].xml`, `_rels/.rels`, `document.xml` (5,062 bytes) and eight pictures under `assets/`. The second block only inspects the result; the conversion needs the first. ### Pattern 2: Bare Markup **What it writes.** With `Format` set to `DocLangFileType.Dclg` the output is one XML file and no picture payload. Turning `IncludeLocations` off also drops the page coordinates, which suits work on text and structure alone. **Code.** ```csharp var options = new DocLangConvertOptions { Format = DocLangFileType.Dclg, IncludeLocations = false }; ``` ```csharp using (var converter = new Converter(inputPath)) { converter.Convert(outputPath, options); } ``` **What the sample measured.** `report.dclg` has a `doclang` root, version 0.10. Below the root sit `` and `` headings, runs of styled text and `` elements whose cells are ``. ### Pattern 3: Page Pictures **What it writes.** `IncludePageImages` adds a picture of every page to a DCLX package, stored as `pages/1.png`, `pages/2.png` and so on, so each page's look sits beside its markup. **Code.** ```csharp var options = new DocLangConvertOptions { IncludePageImages = true }; using (var converter = new Converter(inputPath)) { converter.Convert(outputPath, options); } ``` ```csharp using (ZipArchive package = ZipFile.OpenRead(outputPath)) { const StringComparison exact = StringComparison.Ordinal; int pictures = package.Entries .Count(part => part.FullName.StartsWith("pages/", exact)); Console.WriteLine($" page pictures: {pictures}"); } ``` **What the sample measured.** The count printed for the two-page guide is 2, and the table above shows what the pictures add to the package size. #### Which DocLang file type holds page pictures? Only DCLX holds them. Bare DCLG markup has no picture payload, so combining `IncludePageImages = true` with `Format = DocLangFileType.Dclg` throws `InvalidConvertOptionsException`. The 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." Set `IncludePageImages` on the default DCLX format instead and the package gains one picture per page. ### Pattern 4: Page Chunks **What it writes.** `GetDocumentInfo` reports the page count, and `PageNumber` with `PagesCount` select what each conversion writes. Here that is one page at a time, so a file can be stored or indexed with its page number as the reference. The `Pages` property is the alternative when the wanted pages are not consecutive. **Code.** ```csharp int pages; using (var converter = new Converter(inputPath)) { pages = converter.GetDocumentInfo().PagesCount; } ``` ```csharp for (int page = 1; page <= pages; page++) { var options = new DocLangConvertOptions { Format = DocLangFileType.Dclg, PageNumber = page, PagesCount = 1 }; string chunkPath = Path.Combine(outputFolder, $"page-{page}.dclg"); using (var converter = new Converter(inputPath)) { converter.Convert(chunkPath, options); } } ``` **What the sample measured.** The guide has 2 pages, so the loop leaves `page-1.dclg` and `page-2.dclg` in the folder. The first page carries most of the markup and the second only a little. ### Pattern 5: Folder of Mixed Documents **What it writes.** PDF, Word and Outlook files pass through the same options. Each output is named after its source plus `.dclg`, which keeps the origin of every file visible. **Code.** ```csharp var options = new DocLangConvertOptions { Format = DocLangFileType.Dclg }; int converted = 0; ``` ```csharp foreach (string inputPath in Directory.GetFiles(inputFolder).OrderBy(path => path)) { string sourceName = Path.GetFileName(inputPath); string outputName = sourceName + ".dclg"; using (var converter = new Converter(inputPath)) { converter.Convert(Path.Combine(outputFolder, outputName), options); } converted++; } ``` **What the sample measured.** The Outlook message came out as its Sent, To and Subject header lines plus the body. The PDF's file holds text, tables, a list that contains a link, `` and `` elements that hold only their page location, with no image data. ## Where Each Pattern Fits A team that stores reports can keep each one as a DCLX package: Pattern 3 puts the markup and every page picture into one file, so a reviewer reads the structure and still sees the page. A search index that points readers to a page needs one record per page, which is what Pattern 4 writes. A folder collecting PDF files, Word reports and Outlook messages becomes one markup format in a single pass with Pattern 5. Wrap every `Converter` in a `using` block, as all five patterns do, and run the sample from its project folder so the relative `resources/` and `output/` paths resolve. ## FAQ **Q: Which pattern should I start with?** A: Start with Pattern 1 to see what a package holds. Move to Pattern 2 when only the text structure matters, and to Pattern 3 when a picture of each page must stay with the markup. **Q: Can I mix the patterns?** A: Yes. They are independent `Converter` calls over the same input, so a program can write a package for the archive and per-page chunks for the index from one document. **Q: What does IncludeLocations change?** A: It controls whether the markup records where each element sits on its page. It is on by default, and switching it off is what shrinks the Word report in Pattern 2. **Q: What is the DocLang format?** A: It is published as [the DocLang format](https://github.com/doclang-project/doclang). GroupDocs.Conversion writes it as DCLG or DCLX, and [Load DocLang document with options](https://docs.groupdocs.com/conversion/net/load-doclang-document-with-options/) covers reading it back. ## Conclusion and Recommendations The defaults are `DocLangFileType.Dclx`, `IncludeLocations` on and `IncludePageImages` off. Start from them, and change one option at a time: the type for markup alone, `IncludePageImages` for page pictures, `PageNumber` and `PagesCount` for chunks. The type reference is on the [DocLangFileType](https://reference.groupdocs.com/conversion/net/groupdocs.conversion.filetypes/doclangfiletype/) page. ## See Also - [In-depth blog article about this project](https://blog.groupdocs.com/conversion/convert-documents-to-doclang-net/) - [Convert to and from DocLang](https://docs.groupdocs.com/conversion/net/convert/doclang/) - [Load DocLang document with options](https://docs.groupdocs.com/conversion/net/load-doclang-document-with-options/) - [GitHub repository](https://github.com/groupdocs-conversion/convert-documents-to-doclang-dotnet) - [GroupDocs.Conversion for .NET documentation](https://docs.groupdocs.com/conversion/net/)