Skip to content

Commit 7736c4e

Browse files
Publish programming-with-documents category examples (#239)
1 parent 6384169 commit 7736c4e

75 files changed

Lines changed: 1602 additions & 1629 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

programming-with-documents/AGENTS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ Use the simplest workflow that satisfies the task.
604604
## Build and run contract
605605

606606
- Target framework: `net8.0`
607-
- Package: `Aspose.Words` `26.7.0`
607+
- Package: `Aspose.Words` `26.8.0`
608608

609609
## Command reference
610610

@@ -618,7 +618,7 @@ cd ExampleProject
618618
### Add required packages
619619

620620
```bash
621-
dotnet add package Aspose.Words --version 26.7.0
621+
dotnet add package Aspose.Words --version 26.8.0
622622
```
623623

624624
### Copy a category example into the temp project

programming-with-documents/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# Programming With Documents Examples for Aspose.Words for .NET
22

3-
This folder contains the live, publish-ready C# examples for the Programming With Documents category. Each file is a standalone console example selected from the verified 26.7.0 run.
3+
This folder contains the live, publish-ready C# examples for the Programming With Documents category. Each file is a standalone console example selected from the verified 26.8.0 run.
44

55
## Snapshot
66

77
- Category: Programming With Documents
88
- Slug: programming-with-documents
99
- Total examples: 110
1010
- Publish-ready successful examples: 110 / 110
11-
- Source run: 20260802_190455_803b68
11+
- Source run: 20260830_195242_46e1a9
1212
- Document Workflow examples: 110
1313

1414
## Category rules that shaped these examples
@@ -22,7 +22,7 @@ This folder contains the live, publish-ready C# examples for the Programming Wit
2222
## Prerequisites
2323

2424
- .NET SDK 8.0 or later
25-
- Aspose.Words 26.7.0
25+
- Aspose.Words 26.8.0
2626

2727
## Running Examples
2828

@@ -31,7 +31,7 @@ Each file in this folder is a single, standalone `.cs` console example. To run o
3131
```bash
3232
dotnet new console -n ExampleProject --framework net8.0
3333
cd ExampleProject
34-
dotnet add package Aspose.Words --version 26.7.0
34+
dotnet add package Aspose.Words --version 26.8.0
3535

3636
# Copy one example from this folder into the project as Program.cs
3737
# PowerShell:
@@ -52,7 +52,7 @@ Example:
5252
# From the repository root
5353
dotnet new console -n ExampleProject --framework net8.0
5454
cd ExampleProject
55-
dotnet add package Aspose.Words --version 26.7.0
55+
dotnet add package Aspose.Words --version 26.8.0
5656

5757
# PowerShell example
5858
Copy-Item ..\programming-with-documents\create-a-first-page-header-with-a-centered-logo-image-using-documentbuilder.cs .\Program.cs
@@ -204,6 +204,6 @@ dotnet run --configuration Release --no-build
204204
205205
## Notes for maintainers
206206

207-
- This category is 100% publish-ready for the 26.7.0 run.
207+
- This category is 100% publish-ready for the 26.8.0 run.
208208
- Preserve file-to-task traceability when updating this folder.
209209
- Keep examples standalone and bootstrap local inputs inside the example whenever external sources are mentioned.
Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.IO;
32
using Aspose.Words;
43
using Aspose.Words.Tables;
54

@@ -11,41 +10,36 @@ public static void Main()
1110
Document doc = new Document();
1211
DocumentBuilder builder = new DocumentBuilder(doc);
1312

14-
// Insert a bookmark that will surround the table so we can reference it later.
15-
builder.StartBookmark("MyTable");
16-
1713
// Build a simple 2x2 table.
18-
Table table = builder.StartTable();
14+
builder.StartTable();
1915
builder.InsertCell();
20-
builder.Write("Cell 1,1");
16+
builder.Write("Cell 1");
2117
builder.InsertCell();
22-
builder.Write("Cell 1,2");
18+
builder.Write("Cell 2");
2319
builder.EndRow();
2420

2521
builder.InsertCell();
26-
builder.Write("Cell 2,1");
22+
builder.Write("Cell 3");
2723
builder.InsertCell();
28-
builder.Write("Cell 2,2");
24+
builder.Write("Cell 4");
2925
builder.EndRow();
3026
builder.EndTable();
3127

32-
// End the bookmark after the table.
33-
builder.EndBookmark("MyTable");
34-
35-
// Insert a caption paragraph directly below the table.
36-
// Use the built‑in "Caption" style.
37-
builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
28+
// Insert a caption paragraph styled as "Caption" and bookmark it for referencing.
29+
builder.StartBookmark("TableCaption");
30+
builder.ParagraphFormat.StyleName = "Caption";
3831
builder.Writeln("Table 1: Sample table.");
32+
builder.EndBookmark("TableCaption");
33+
34+
// Move cursor to the end of the document to add a reference to the caption.
35+
builder.MoveToDocumentEnd();
3936

40-
// Add some regular text and a cross‑reference to the table.
41-
builder.ParagraphFormat.ClearFormatting(); // reset to normal style
42-
builder.Writeln();
43-
builder.Writeln("Reference to the table above:");
44-
// Insert a REF field that points to the bookmark "MyTable" and makes it a hyperlink.
45-
builder.InsertField(@" REF MyTable \h ");
37+
// Insert a reference field that points to the bookmarked caption.
38+
builder.Write("See Table ");
39+
builder.InsertField(" REF TableCaption \\h ");
40+
builder.Writeln(" for details.");
4641

47-
// Save the document to the current directory.
48-
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "TableCaption.docx");
49-
doc.Save(outputPath);
42+
// Save the document to the local file system.
43+
doc.Save("TableCaptionReference.docx");
5044
}
5145
}

programming-with-documents/add-a-paragraph-with-a-background-shading-of-light-yellow-to-highlight-important-informati.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ public static void Main()
99
// Create a new blank document.
1010
Document doc = new Document();
1111

12-
// Attach a DocumentBuilder to the document for easy content insertion.
12+
// Initialize a DocumentBuilder which will be used to add content.
1313
DocumentBuilder builder = new DocumentBuilder(doc);
1414

15-
// Set the paragraph shading to a light yellow color.
16-
// This will highlight the paragraph background.
15+
// Apply light yellow background shading to the current paragraph.
1716
builder.ParagraphFormat.Shading.BackgroundPatternColor = Color.LightYellow;
1817

1918
// Write the highlighted paragraph.
20-
builder.Writeln("This is an important paragraph highlighted with a light yellow background.");
19+
builder.Writeln("Important information highlighted with light yellow shading.");
2120

2221
// Save the document to the local file system.
23-
doc.Save("HighlightedParagraph.docx");
22+
string outputPath = "HighlightedParagraph.docx";
23+
doc.Save(outputPath);
2424
}
2525
}

programming-with-documents/add-a-paragraph-with-a-custom-field-that-displays-the-document-s-author-name-dynamically.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,21 @@ public static void Main()
1010
// Create a new blank document.
1111
Document doc = new Document();
1212

13-
// Set the built‑in Author property the AUTHOR field will read this value.
13+
// Set the built‑in Author property (optional, otherwise the default author will be used).
1414
doc.BuiltInDocumentProperties.Author = "John Doe";
1515

16-
// Initialize a DocumentBuilder to add content.
16+
// Initialize a DocumentBuilder for the document.
1717
DocumentBuilder builder = new DocumentBuilder(doc);
1818

1919
// Add a paragraph that introduces the author field.
2020
builder.Writeln("Document author:");
2121

22-
// Insert an AUTHOR field and update it so the result reflects the Author property.
22+
// Insert an AUTHOR field that displays the document's author dynamically.
2323
FieldAuthor authorField = (FieldAuthor)builder.InsertField(FieldType.FieldAuthor, true);
24+
// Update the field to reflect the current Author property.
2425
authorField.Update();
2526

26-
// Save the document to the current working directory.
27+
// Save the document to the current directory.
2728
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "AuthorField.docx");
2829
doc.Save(outputPath);
2930
}

programming-with-documents/add-a-paragraph-with-a-drop-cap-character-and-specify-the-number-of-lines-it-spans.cs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,36 @@
22
using System.IO;
33
using Aspose.Words;
44

5-
public class Program
5+
namespace DropCapExample
66
{
7-
public static void Main()
7+
public class Program
88
{
9-
// Create a new blank document.
10-
Document doc = new Document();
11-
DocumentBuilder builder = new DocumentBuilder(doc);
12-
13-
// Set the drop cap height to span 4 lines.
14-
builder.ParagraphFormat.LinesToDrop = 4;
15-
builder.Writeln("H"); // This paragraph becomes the drop cap.
16-
17-
// Reset the drop cap setting for subsequent paragraphs.
18-
builder.ParagraphFormat.LinesToDrop = 0;
19-
builder.Writeln("ello world!"); // Normal paragraph that wraps around the drop cap.
20-
21-
// Save the document to the output folder.
22-
string outputDir = "Output";
23-
Directory.CreateDirectory(outputDir);
24-
string outputPath = Path.Combine(outputDir, "DropCapExample.docx");
25-
doc.Save(outputPath);
9+
public static void Main()
10+
{
11+
// Create a new blank document.
12+
Document doc = new Document();
13+
14+
// Attach a DocumentBuilder to the document.
15+
DocumentBuilder builder = new DocumentBuilder(doc);
16+
17+
// Set the number of lines the drop cap should span.
18+
// This makes the next paragraph start with a large capital letter.
19+
builder.ParagraphFormat.LinesToDrop = 4;
20+
21+
// Write the drop cap character.
22+
builder.Writeln("H");
23+
24+
// Reset LinesToDrop to 0 so subsequent text behaves normally.
25+
builder.ParagraphFormat.LinesToDrop = 0;
26+
27+
// Write the remaining text that will wrap around the drop cap.
28+
builder.Writeln("ello world! This paragraph demonstrates a drop cap character spanning four lines.");
29+
30+
// Define the output path (in the same folder as the executable).
31+
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "DropCapExample.docx");
32+
33+
// Save the document.
34+
doc.Save(outputPath);
35+
}
2636
}
2737
}

programming-with-documents/add-a-paragraph-with-a-hanging-indent-of-0-25-inches-for-citation-formatting.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,21 @@ public static void Main()
99
// Create a new blank document.
1010
Document doc = new Document();
1111

12-
// Initialize a DocumentBuilder for the document.
12+
// Attach a DocumentBuilder to the document.
1313
DocumentBuilder builder = new DocumentBuilder(doc);
1414

15-
// Set a hanging indent of 0.25 inches (18 points). Negative value creates a hanging indent.
16-
builder.ParagraphFormat.FirstLineIndent = -18; // 0.25 inch = 0.25 * 72 points
15+
// Set a hanging indent of 0.25 inches (18 points) for the next paragraph.
16+
// Positive values create a first‑line indent, negative values create a hanging indent.
17+
builder.ParagraphFormat.FirstLineIndent = -18; // 0.25 inch = 18 points
1718

18-
// Add a sample citation paragraph.
19-
builder.Writeln("Doe, J. (2020). Example citation text with a hanging indent for formatting purposes.");
19+
// Write the citation text. The paragraph will inherit the hanging indent set above.
20+
builder.Writeln("“The quick brown fox jumps over the lazy dog.” – Example Citation");
2021

21-
// Determine the output file path.
22-
string outputPath = Path.Combine(Directory.GetCurrentDirectory(), "HangingIndent.docx");
23-
24-
// Ensure the directory exists (in case the current directory is a root path).
25-
Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
22+
// Ensure the output directory exists.
23+
string outputFile = Path.Combine(Directory.GetCurrentDirectory(), "HangingIndent.docx");
24+
Directory.CreateDirectory(Path.GetDirectoryName(outputFile));
2625

2726
// Save the document.
28-
doc.Save(outputPath);
27+
doc.Save(outputFile);
2928
}
3029
}

programming-with-documents/add-a-text-box-to-a-document-header-and-ensure-it-appears-on-every-page-of-the-section.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,36 @@ public static void Main()
1111
Document doc = new Document();
1212
DocumentBuilder builder = new DocumentBuilder(doc);
1313

14-
// Move the builder's cursor to the primary header of the first section.
14+
// Move the cursor to the primary header of the first (and only) section.
1515
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
1616

1717
// Create a floating text box shape.
1818
Shape textBox = new Shape(doc, ShapeType.TextBox);
19-
textBox.WrapType = WrapType.None; // No text wrapping – the box stays in the header.
20-
textBox.Height = 50; // Height in points.
21-
textBox.Width = 200; // Width in points.
19+
textBox.WrapType = WrapType.None;
20+
textBox.Width = 200;
21+
textBox.Height = 50;
2222
textBox.HorizontalAlignment = HorizontalAlignment.Center;
2323
textBox.VerticalAlignment = VerticalAlignment.Top;
2424

25-
// Add a paragraph with a run of text inside the text box.
25+
// Add a paragraph with centered text inside the text box.
2626
textBox.AppendChild(new Paragraph(doc));
27-
Paragraph para = (Paragraph)textBox.FirstParagraph;
27+
Paragraph para = textBox.FirstParagraph;
2828
para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
29-
Run run = new Run(doc, "Header Text Box");
29+
Run run = new Run(doc, "Header TextBox");
3030
para.AppendChild(run);
3131

3232
// Insert the text box into the header.
3333
builder.InsertNode(textBox);
3434

35-
// Return the cursor to the main document body.
35+
// Add some body content spanning multiple pages to demonstrate the header repeats.
3636
builder.MoveToSection(0);
37-
38-
// Add enough content to generate multiple pages.
3937
builder.Writeln("Page 1");
4038
builder.InsertBreak(BreakType.PageBreak);
4139
builder.Writeln("Page 2");
4240
builder.InsertBreak(BreakType.PageBreak);
4341
builder.Writeln("Page 3");
4442

45-
// Save the document. The text box will appear in the header on every page.
43+
// Save the document.
4644
doc.Save("HeaderWithTextBox.docx");
4745
}
4846
}

0 commit comments

Comments
 (0)