Skip to content

Commit 45cd994

Browse files
authored
Extract Code Snippets from MD Files (#309)
1 parent da68a69 commit 45cd994

22 files changed

+606
-1037
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ISO/IEC 29500-2](https://www.iso.org/standard/77818.html)

docs/word/how-to-remove-hidden-text-from-a-word-processing-document.md

Lines changed: 48 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.suite: office
1111
ms.author: o365devx
1212
author: o365devx
1313
ms.topic: conceptual
14-
ms.date: 11/01/2017
14+
ms.date: 02/02/2024
1515
ms.localizationpriority: medium
1616
---
1717
# Remove hidden text from a word processing document
@@ -21,100 +21,26 @@ Office to programmatically remove hidden text from a word processing
2121
document.
2222

2323

24-
25-
---------------------------------------------------------------------------------
26-
## Getting a WordprocessingDocument Object
27-
To open an existing document, you instantiate the <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> class as shown in the
28-
following **using** statement. In the same
29-
statement, you open the word processing file with the specified
30-
*fileName* by using the **Open** method, with
31-
the Boolean parameter set to **true** in order
32-
to enable editing the document.
33-
34-
### [C#](#tab/cs-0)
35-
```csharp
36-
using (WordprocessingDocument doc =
37-
WordprocessingDocument.Open(fileName, true))
38-
{
39-
// Insert other code here.
40-
}
41-
```
42-
43-
### [Visual Basic](#tab/vb-0)
44-
```vb
45-
Using wdDoc As WordprocessingDocument = _
46-
WordprocessingDocument.Open(filepath, True)
47-
' Insert other code here.
48-
End Using
49-
```
50-
***
51-
52-
53-
The **using** statement provides a recommended
54-
alternative to the typical .Create, .Save, .Close sequence. It ensures
55-
that the **Dispose** method (internal method
56-
used by the Open XML SDK to clean up resources) is automatically called
57-
when the closing brace is reached. The block that follows the using
58-
statement establishes a scope for the object that is created or named in
59-
the using statement, in this case doc. Because the <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument> class in the Open XML SDK
60-
automatically saves and closes the object as part of its **System.IDisposable** implementation, and because
61-
**Dispose** is automatically called when you
62-
exit the block, you do not have to explicitly call **Save** and **Close**─as
63-
long as you use **using**.
64-
65-
6624
--------------------------------------------------------------------------------
67-
## Structure of a WordProcessingML Document
68-
The basic document structure of a **WordProcessingML** document consists of the **document** and **body**
69-
elements, followed by one or more block level elements such as **p**, which represents a paragraph. A paragraph
70-
contains one or more **r** elements. The **r** stands for run, which is a region of text with
71-
a common set of properties, such as formatting. A run contains one or
72-
more **t** elements. The **t** element contains a range of text. The following
73-
code example shows the **WordprocessingML**
74-
markup for a document that contains the text "Example text."
75-
76-
```xml
77-
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
78-
<w:body>
79-
<w:p>
80-
<w:r>
81-
<w:t>Example text.</w:t>
82-
</w:r>
83-
</w:p>
84-
</w:body>
85-
</w:document>
86-
```
87-
88-
Using the Open XML SDK, you can create document structure and
89-
content using strongly-typed classes that correspond to **WordprocessingML** elements. You will find these
90-
classes in the [DocumentFormat.OpenXml.Wordprocessing](/dotnet/api/documentformat.openxml.wordprocessing)
91-
namespace. The following table lists the class names of the classes that
92-
correspond to the **document**, **body**, **p**, **r**, and **t** elements.
93-
94-
WordprocessingML Element|Open XML SDK Class|Description
95-
--|--|--
96-
document|[Document](/dotnet/api/documentformat.openxml.wordprocessing.document) |The root element for the main document part.
97-
body|[Body](/dotnet/api/documentformat.openxml.wordprocessing.body) |The container for the block level structures such as paragraphs, tables, annotations and others specified in the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification.
98-
p|[Paragraph](/dotnet/api/documentformat.openxml.wordprocessing.paragraph) |A paragraph.
99-
r|[Run](/dotnet/api/documentformat.openxml.wordprocessing.run) |A run.
100-
t|[Text](/dotnet/api/documentformat.openxml.wordprocessing.text) |A range of text.
10125

26+
[!include[Word Structure](../includes/word/structure.md)]
10227

10328
---------------------------------------------------------------------------------
10429
## Structure of the Vanish Element
105-
The **vanish** element plays an important role in hiding the text in a
106-
Word file. The **Hidden** formatting property is a toggle property,
30+
31+
The `vanish` element plays an important role in hiding the text in a
32+
Word file. The `Hidden` formatting property is a toggle property,
10733
which means that its behavior differs between using it within a style
10834
definition and using it as direct formatting. When used as part of a
10935
style definition, setting this property toggles its current state.
110-
Setting it to **false** (or an equivalent)
36+
Setting it to `false` (or an equivalent)
11137
results in keeping the current setting unchanged. However, when used as
112-
direct formatting, setting it to **true** or
113-
**false** sets the absolute state of the
38+
direct formatting, setting it to `true` or
39+
`false` sets the absolute state of the
11440
resulting property.
11541

11642
The following information from the [!include[ISO/IEC 29500 URL](../includes/iso-iec-29500-link.md)] specification
117-
introduces the **vanish** element.
43+
introduces the `vanish` element.
11844

11945
> **vanish (Hidden Text)**
12046
>
@@ -126,7 +52,7 @@ introduces the **vanish** element.
12652
> This formatting property is a *toggle property* (§17.7.3).
12753
>
12854
> If this element is not present, the default value is to leave the
129-
> formatting applied at previous level in the *style hierarchy* .If this
55+
> formatting applied at previous level in the *style hierarchy*. If this
13056
> element is never applied in the style hierarchy, then this text shall
13157
> not be hidden when displayed in a document.
13258
>
@@ -154,44 +80,59 @@ The following XML schema segment defines the contents of this element.
15480
</complexType>
15581
```
15682

157-
The **val** property in the code above is a binary value that can be
158-
turned on or off. If given a value of **on**, **1**, or **true** the property is turned on. If given the
159-
value **off**, **0**, or **false** the property
83+
The `val` property in the code above is a binary value that can be
84+
turned on or off. If given a value of `on`, `1`, or `true` the property is turned on. If given the
85+
value `off`, `0`, or `false` the property
16086
is turned off.
16187

88+
## How the Code Works
16289

163-
--------------------------------------------------------------------------------
164-
## Sample Code
165-
The following code example shows how to remove all of the hidden text
166-
from a document. You can call the method, WDDeleteHiddenText, by using
167-
the following call as an example to delete the hidden text from a file
168-
named "Word14.docx."
169-
170-
### [C#](#tab/cs-1)
171-
```csharp
172-
string docName = @"C:\Users\Public\Documents\Word14.docx";
173-
WDDeleteHiddenText(docName);
174-
```
90+
The `WDDeleteHiddenText` method works with the document you specify and removes all of the `run` elements that are hidden and removes extra `vanish` elements. The code starts by opening the
91+
document, using the <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument.Open%2A> method and indicating that the
92+
document should be opened for read/write access (the final true
93+
parameter). Given the open document, the code uses the <xref:DocumentFormat.OpenXml.Packaging.WordprocessingDocument.MainDocumentPart> property to navigate to
94+
the main document, storing the reference in a variable.
17595

176-
### [Visual Basic](#tab/vb-1)
177-
```vb
178-
Dim docName As String = "C:\Users\Public\Documents\Word14.docx"
179-
WDDeleteHiddenText(docName)
180-
```
96+
### [C#](#tab/cs)
97+
[!code-csharp[](../../samples/word/remove_hidden_text/cs/Program.cs#snippet1)]
98+
### [Visual Basic](#tab/vb)
99+
[!code-vb[](../../samples/word/remove_hidden_text/vb/Program.vb#snippet1)]
181100
***
182101

102+
## Get a List of Vanish Elements
103+
104+
The code first checks that `doc.MainDocumentPart` and `doc.MainDocumentPart.Document.Body` are not null and throws an exception if one is missing. Then uses the <xref:DocumentFormat.OpenXml.OpenXmlElement.Descendants> passing it the <xref:DocumentFormat.OpenXml.Wordprocessing.Vanish> type to get an `IEnumerable` of the `Vanish` elements and casts them to a list.
105+
106+
### [C#](#tab/cs)
107+
[!code-csharp[](../../samples/word/remove_hidden_text/cs/Program.cs#snippet2)]
108+
### [Visual Basic](#tab/vb)
109+
[!code-vb[](../../samples/word/remove_hidden_text/vb/Program.vb#snippet2)]
110+
***
111+
112+
## Remove Runs with Hidden Text and Extra Vanish Elements
113+
114+
To remove the hidden text we next loop over the `List` of `Vanish` elements. The `Vanish` element is a child of the <xref:DocumentFormat.OpenXml.Wordprocessing.RunProperties> but `RunProperties` can be a child of a <xref:DocumentFormat.OpenXml.Wordprocessing.Run> or xref:DocumentFormat.OpenXml.Wordprocessing.ParagraphProperties>, so we get the parent and grandparent of each `Vanish` and check its type. Then if the grandparent is a `Run` we remove that run and if not
115+
we we remove the `Vanish` child elements from the parent.
116+
117+
### [C#](#tab/cs)
118+
[!code-csharp[](../../samples/word/remove_hidden_text/cs/Program.cs#snippet3)]
119+
### [Visual Basic](#tab/vb)
120+
[!code-vb[](../../samples/word/remove_hidden_text/vb/Program.vb#snippet3)]
121+
***
122+
--------------------------------------------------------------------------------
123+
## Sample Code
183124

184125
> [!NOTE]
185-
> This example assumes that the file Word14.docx contains some hidden text. In order to hide part of the file text, select it, and click CTRL+D to show the **Font** dialog box. Select the **Hidden** box and click **OK**.
126+
> This example assumes that the file being opened contains some hidden text. In order to hide part of the file text, select it, and click CTRL+D to show the **Font** dialog box. Select the **Hidden** box and click **OK**.
186127
187128

188129
Following is the complete sample code in both C\# and Visual Basic.
189130

190131
### [C#](#tab/cs)
191-
[!code-csharp[](../../samples/word/remove_hidden_text/cs/Program.cs)]
132+
[!code-csharp[](../../samples/word/remove_hidden_text/cs/Program.cs#snippet0)]
192133

193134
### [Visual Basic](#tab/vb)
194-
[!code-vb[](../../samples/word/remove_hidden_text/vb/Program.vb)]
135+
[!code-vb[](../../samples/word/remove_hidden_text/vb/Program.vb#snippet0)]
195136

196137
--------------------------------------------------------------------------------
197138
## See also

0 commit comments

Comments
 (0)