Skip to content

Commit af08cc9

Browse files
committed
update context objects article
1 parent 3c147ef commit af08cc9

1 file changed

Lines changed: 75 additions & 20 deletions

File tree

net/developer-guide/basic-usage/context-objects-complete-guide.md

Lines changed: 75 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ GroupDocs.Conversion provides six context types, each serving a specific purpose
5252
| `SourceFileName` | `string` | Name of the source file (read-only) |
5353
| `SourceFormat` | `FileType` | Format of the source document (read-only) |
5454
| `SourceStream` | `Stream` | Source document stream (read-only) |
55-
| `HierarchyLevel` | `int` | Hierarchy level for container documents (read-only) * |
56-
| `ItemIndex` | `int` | Index of current item in container documents (read-only) * |
55+
| `HierarchyLevel` | `int` | Nesting depth of current document in container hierarchy (read-only) * |
56+
| `ItemIndex` | `int` | Absolute sequential counter across all items (read-only) * |
5757

58-
\* `HierarchyLevel` and `ItemIndex` are only relevant when converting container documents (e.g., PST, OST, PDF with embedded files). For regular documents, these values are always 0 and 1.
58+
\* `HierarchyLevel` shows nesting depth (0 = root, 1 = embedded in root, etc.). `ItemIndex` is an absolute counter (1, 2, 3...) across all items regardless of hierarchy. For regular documents, these values are always 0 and 1.
5959

6060
### Example
6161

@@ -135,10 +135,10 @@ using (var converter = new Converter("quarterly-report.pdf"))
135135
| `SourceFileName` | `string` | Name of the source file (read-only) |
136136
| `SourceFormat` | `FileType` | Format of the source document (read-only) |
137137
| `TargetFormat` | `string` | Target format for conversion (read-only) |
138-
| `HierarchyLevel` | `int` | Hierarchy level for container documents (read-only) * |
139-
| `ItemIndex` | `int` | Index of current item in container documents (read-only) * |
138+
| `HierarchyLevel` | `int` | Nesting depth of current document in container hierarchy (read-only) * |
139+
| `ItemIndex` | `int` | Absolute sequential counter across all items (read-only) * |
140140

141-
\* Only relevant for container documents (e.g., PST, OST, PDF with embedded files).
141+
\* `HierarchyLevel` shows nesting depth (0 = root, 1 = embedded, etc.). `ItemIndex` is an absolute counter (1, 2, 3...) across all items. Only relevant for container documents (e.g., PST, OST, PDF with embedded files).
142142

143143
### Example
144144

@@ -214,10 +214,10 @@ using (var converter = new Converter("contract-2024.pdf"))
214214
| `ConvertedStream` | `Stream` | Stream containing the converted document (read-only) |
215215
| `SourceFileName` | `string` | Name of the source file (read-only) |
216216
| `SourceFormat` | `FileType` | Format of the source document (read-only) |
217-
| `HierarchyLevel` | `int` | Hierarchy level for container documents (read-only) * |
218-
| `ItemIndex` | `int` | Index of current item in container documents (read-only) * |
217+
| `HierarchyLevel` | `int` | Nesting depth of current document in container hierarchy (read-only) * |
218+
| `ItemIndex` | `int` | Absolute sequential counter across all items (read-only) * |
219219

220-
\* Only relevant for container documents (e.g., PST, OST, PDF with embedded files).
220+
\* `HierarchyLevel` shows nesting depth (0 = root, 1 = embedded, etc.). `ItemIndex` is an absolute counter (1, 2, 3...) across all items. Only relevant for container documents (e.g., PST, OST, PDF with embedded files).
221221

222222
### Example
223223

@@ -291,6 +291,26 @@ using (var converter = new Converter("monthly-report.pdf"))
291291

292292
The `HierarchyLevel` and `ItemIndex` properties become relevant when converting **container documents** that can hold multiple items in a hierarchical structure.
293293

294+
### Understanding HierarchyLevel and ItemIndex
295+
296+
- **HierarchyLevel**: Shows the **nesting depth** of the current document
297+
- `0` = Root/main document
298+
- `1` = Directly embedded in main document
299+
- `2` = Embedded within an embedded document, etc.
300+
301+
- **ItemIndex**: **Absolute sequential counter** across ALL items
302+
- Starts at 1 and increments for each item
303+
- NOT per-hierarchy-level (it's global)
304+
305+
**Example hierarchy:**
306+
```
307+
Main PDF (Level 0, Index 1)
308+
├─ Attachment 1 (Level 1, Index 2)
309+
├─ Attachment 2 (Level 1, Index 3)
310+
└─ Attachment 3 (Level 1, Index 4)
311+
└─ Sub-attachment (Level 2, Index 5)
312+
```
313+
294314
### Container Document Types
295315

296316
- **Email containers**: PST, OST files (Outlook data files)
@@ -330,29 +350,64 @@ using (var converter = new Converter("mailbox-archive.pst"))
330350
### Example: PDF with Embedded Files
331351

332352
```csharp
353+
// LoadContext is called for EACH document in the hierarchy
333354
using (var converter = new Converter(
334355
"contract-with-attachments.pdf",
335356
(LoadContext loadContext) =>
336357
{
337-
// Track hierarchy as we load
338-
if (loadContext.HierarchyLevel > 0)
358+
// HierarchyLevel shows WHERE in the hierarchy this document is
359+
// Level 0 = main document
360+
// Level 1 = directly embedded in main document
361+
// Level 2 = embedded within an embedded document, etc.
362+
363+
Console.WriteLine($"Loading: {loadContext.SourceFileName}");
364+
Console.WriteLine($" Hierarchy Level: {loadContext.HierarchyLevel}");
365+
Console.WriteLine($" Item Index: {loadContext.ItemIndex}");
366+
367+
if (loadContext.HierarchyLevel == 0)
339368
{
340-
Console.WriteLine($"Loading embedded file: {loadContext.SourceFileName}");
341-
Console.WriteLine($" At level: {loadContext.HierarchyLevel}");
342-
Console.WriteLine($" Item index: {loadContext.ItemIndex}");
369+
Console.WriteLine(" -> This is the main document");
370+
return new PdfLoadOptions { RemoveEmbeddedFiles = false };
371+
}
372+
else if (loadContext.HierarchyLevel == 1)
373+
{
374+
Console.WriteLine(" -> This is an embedded file in the main document");
375+
return new PdfLoadOptions();
376+
}
377+
else
378+
{
379+
Console.WriteLine($" -> This is nested {loadContext.HierarchyLevel} levels deep");
380+
return new PdfLoadOptions();
343381
}
344-
345-
return new PdfLoadOptions { RemoveEmbeddedFiles = false };
346382
}))
347383
{
348-
converter.Convert("contract-complete.pdf", new PdfConvertOptions());
384+
int itemCount = 0;
385+
386+
converter.Convert(
387+
(SaveContext saveContext) =>
388+
{
389+
itemCount++;
390+
391+
// SaveContext also shows hierarchy level during conversion
392+
var outputName = $"contract-L{saveContext.HierarchyLevel}-I{saveContext.ItemIndex}.pdf";
393+
Console.WriteLine($"Saving: {outputName}");
394+
395+
return File.Create(outputName);
396+
},
397+
new PdfConvertOptions());
398+
399+
Console.WriteLine($"Total items converted: {itemCount}");
349400
}
350401
```
351402

352403
**Key Points:**
353-
- For regular documents (DOCX, XLSX, simple PDF), `HierarchyLevel` is always 0 and `ItemIndex` is always 1
354-
- For container documents, `HierarchyLevel` indicates nesting depth (0 = root, 1 = first level, etc.)
355-
- `ItemIndex` provides the sequential position within the current hierarchy level
404+
- **LoadContext is called once for each document** in the container hierarchy (main document + each embedded document)
405+
- **HierarchyLevel shows the nesting depth** of the document currently being loaded/converted:
406+
- `HierarchyLevel = 0` → Main/root document
407+
- `HierarchyLevel = 1` → Directly embedded in main document
408+
- `HierarchyLevel = 2` → Embedded within an embedded document, etc.
409+
- **ItemIndex is an absolute sequential counter** - counts all items across the entire conversion (1, 2, 3, 4...), regardless of hierarchy level
410+
- For regular documents (DOCX, XLSX, simple PDF without embedded files), `HierarchyLevel` is always 0 and `ItemIndex` is always 1
356411

357412
---
358413

0 commit comments

Comments
 (0)