Skip to content

Commit 6067efb

Browse files
ES-958622-EditableRanges
1 parent 34cf45d commit 6067efb

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

Security/Add-editable-range-in-a-table/.NET/Add-editable-range-in-a-table/Add-editable-range-in-a-table.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
</ItemGroup>
1212

1313
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
1417
<None Update="Output\.gitkeep">
1518
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
1619
</None>

Security/Add-editable-range-in-a-table/.NET/Add-editable-range-in-a-table/Program.cs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,28 @@ class Program
88
{
99
static void Main(string[] args)
1010
{
11-
//Creates a Word document
12-
using (WordDocument document = new WordDocument())
11+
// Load the Word document
12+
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
1313
{
14-
//Adds a section and a paragraph in the Word document
15-
document.EnsureMinimal();
16-
17-
//Adds a table
18-
WTable table = document.LastSection.AddTable() as WTable;
19-
table.ResetCells(2, 3);
20-
21-
//Access each table cell and append text
22-
table[0, 0].AddParagraph().AppendText("Row1 Col1");
23-
table[0, 1].AddParagraph().AppendText("Row1 Col2");
24-
table[0, 2].AddParagraph().AppendText("Row1 Col3");
25-
table[1, 0].AddParagraph().AppendText("Row2 Col1");
26-
table[1, 1].AddParagraph().AppendText("Row2 Col2");
27-
table[1, 2].AddParagraph().AppendText("Row2 Col3");
28-
29-
//Starts the editable range in a table cell
30-
EditableRangeStart editableRangeStart = table[0, 1].Paragraphs[0].AppendEditableRangeStart();
31-
32-
//Sets the first column where the editable range starts within a table
33-
editableRangeStart.EditableRange.FirstColumn = 1;
34-
35-
//Ends the ediatble range in a table cell
36-
EditableRangeEnd rangeEnd = table[1, 2].Paragraphs[0].AppendEditableRangeEnd(editableRangeStart);
37-
38-
//Sets the last column where the editable range ends within a table
39-
editableRangeStart.EditableRange.LastColumn = 2;
40-
14+
// Access the first table in the first section of the document
15+
WTable table = document.Sections[0].Tables[0] as WTable;
16+
// Access the paragraph in the third row and third column of the table
17+
WParagraph paragraph = table[2, 2].ChildEntities[0] as WParagraph;
18+
// Create a new editable range start for the table cell paragraph
19+
EditableRangeStart editableRangeStart = new EditableRangeStart(document);
20+
// Insert the editable range start at the beginning of the paragraph
21+
paragraph.ChildEntities.Insert(0, editableRangeStart);
22+
// Set the editor group for the editable range to allow everyone to edit
23+
editableRangeStart.EditorGroup = EditorType.Everyone;
24+
// Apply editable range to second column only
25+
editableRangeStart.FirstColumn = 1;
26+
editableRangeStart.LastColumn = 1;
27+
// Access the paragraph
28+
paragraph = table[5, 2].ChildEntities[0] as WParagraph;
29+
// Append an editable range end to close the editable region
30+
paragraph.AppendEditableRangeEnd();
4131
//Sets the protection with password and allows only reading
4232
document.Protect(ProtectionType.AllowOnlyReading, "password");
43-
4433
//Creates file stream
4534
using (FileStream outputStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
4635
{

0 commit comments

Comments
 (0)