To check out the Office Open XML markup for the examples of each of these content types shown earlier in Figures 5 through 8, explore the Load and write Open XML in your Word add-in code sample referenced in the overview section.
Before you move on, take a look at differences to note for a couple of these content types and how to swap out the pieces you need. If the markup for your shape or text box looks far more complex than you would expect, there is a reason for it. In the release, Word only incorporated part of that graphics engine, adopting the updated Excel charting engine, SmartArt graphics, and advanced picture tools.
For shapes and text boxes, Word continued to use legacy drawing objects VML. It was in the release that Word took the additional steps with the graphics engine to incorporate updated shapes and drawing tools. Typically, as you see for the shape and text box examples included in the Load and write Open XML in your Word add-in code sample, the fallback markup can be removed. Word automatically adds missing fallback markup to shapes when a document is saved.
However, if you prefer to keep the fallback markup to ensure that you're supporting all user scenarios, there's no harm in retaining it. If you have grouped drawing objects included in your content, you'll see additional and apparently repetitive markup, but this must be retained. Portions of the markup for drawing shapes are duplicated when the object is included in a group.
When working with text boxes and drawing shapes, be sure to check namespaces carefully before removing them from document. Or, if you're reusing markup from another object type, be sure to add back any required namespaces you might have previously removed from document. A substantial portion of the namespaces included by default in document.
Also be aware that the image examples in those code samples are setup using in line with text formatting, which positions a graphic object on the text baseline. The shape in those code samples is positioned relative to the right and bottom page margins.
Relative positioning lets you more easily coordinate with a user's unknown document setup because it will adjust to the user's margins and run less risk of looking awkward because of paper size, orientation, or margin settings. To retain relative positioning settings when you insert a graphic object, you must retain the paragraph mark w:p in which the positioning known in Word as an anchor is stored. If you insert the content into an existing paragraph mark rather than including your own, you may be able to retain the same initial visual, but many types of relative references that enable the positioning to automatically adjust to the user's layout may be lost.
Content controls are an important feature in Word that can greatly enhance the power of your add-in for Word in multiple ways, including giving you the ability to insert content at designated places in the document rather than only at the selection. Types of content controls in Word include rich text, plain text, picture, building block gallery, check box, dropdown list, combo box, date picker, and repeating section.
Use the Properties command, shown in Figure 15, to edit the title of the control and to set preferences such as hiding the control container. If your add-in works with a Word template, you can include controls in that template to enhance the behavior of the content.
You can also use XML data binding in a Word document to bind content controls to data, such as document properties, for easy form completion or similar tasks. Find controls that are already bound to built-in document properties in Word on the Insert tab, under Quick Parts. When you use content controls with your add-in, you can also greatly expand the options for what your add-in can do using a different type of binding. You can bind to a content control from within the add-in and then write content to the binding rather than to the active selection.
Don't confuse XML data binding in Word with the ability to bind to a control via your add-in. These are completely separate features. However, you can include named content controls in the content you insert via your add-in using OOXML coercion and then use code in the add-in to bind to those controls.
Also be aware that both XML data binding and Office. Working with bindings in your Word add-in is covered in the next section of the topic. First, take a look at an example of the Office Open XML required for inserting a rich text content control that you can bind to using your add-in.
Rich text controls are the only type of content control you can use to bind to a content control from within your add-in. As already mentioned, content controls, like formatted text, don't require additional document parts, so only edited versions of the. The w:sdt tag that you see within the document. If you generate the Office Open XML markup for a content control, you'll see that several attributes have been removed from this example, including the tag and document part properties. Only essential and a couple of best practice elements have been retained, including the following:.
The alias is the title property from the Content Control Properties dialog box in Word. This is a required property representing the name of the item if you plan to bind to the control from within your add-in. The unique id is a required property. If you bind to the control from within your add-in, the ID is the property the binding uses in the document to identify the applicable named content control.
The appearance attribute is used to hide the control container, for a cleaner look. This feature was introduced in Word , as you see by the use of the w15 namespace. Because this property is used, the w15 namespace is retained at the start of the document. The showingPlcHdr attribute is an optional setting that sets the default content you include inside the control text in this example as placeholder content. So, if the user clicks or taps in the control area, the entire content is selected rather than behaving like editable content in which the user can make changes.
This may be important, depending upon the type and formatting of content that will be added in the control. If you intend to bind to the control, the default content for the control what's inside the sdtContent tag must include at least one complete paragraph as in this example , in order for your binding to accept multi-paragraph rich content.
The document part attribute that was removed from this sample w:sdt tag may appear in a content control to reference a separate part in the package where placeholder content information can be stored parts located in a glossary directory in the Office Open XML package. Although document part is the term used for XML parts that is, files within an Office Open XML package, the term document parts as used in the sdt property refers to the same term in Word that is used to describe some content types including building blocks and document property quick parts for example, built-in XML data-bound controls.
If you see parts under a glossary directory in your Office Open XML package, you may need to retain them if the content you're inserting includes these features. For a typical content control that you intend to use to bind to from your add-in, they're not required. Just remember that, if you do delete the glossary parts from the package, you must also remove the document part attribute from the w:sdt tag.
You've already looked at how to insert content at the active selection in a Word document. If you bind to a named content control that's in the document, you can insert any of the same content types into that control. When you need to add or replace content at specified locations in a template, such as to populate portions of the document from a database.
When you want the option to replace content that you're inserting at the active selection, such as to provide design element options to the user. When you want the user to add data in the document that you can access for use with your add-in, such as to populate fields in the task pane based upon information the user adds in the document.
Download the code sample Word-Add-in-JavaScript-AddPopulateBindings , which provides a working example of how to insert and bind to a content control, and how to populate the binding. As previously mentioned, you must use a rich text content control in order to bind to the control from your Word add-in.
This is how the code identifies where to place the binding. You can have several named controls and bind to them as needed. Use a unique content control name, unique content control ID, and a unique binding ID.
Attempts to bind to the named content control, using addFromNamedItemAsync. Take this step first if there is a possible scenario for your add-in where the named control could already exist in the document when the code executes.
For example, you'll want to do this if the add-in was inserted into and saved with a template that's been designed to work with the add-in, where the control was placed in advance. You also need to do this if you need to bind to a control that was placed earlier by the add-in. The callback in the first call to the addFromNamedItemAsync method checks the status of the result to see if the binding failed because the named item doesn't exist in the document that is, the content control named MyContentControlTitle in this example.
If so, the code adds the control at the active selection point using setSelectedDataAsync and then binds to it. As mentioned earlier and shown in the preceding code, the name of the content control is used to determine where to create the binding. However, in the Office Open XML markup, the code adds the binding to the document using both the name and the ID attribute of the content control.
After code execution, if you examine the markup of the document in which your add-in created bindings, you'll see two parts to each binding. In the markup for the content control where a binding was added in document. In the document part named webExtensions1. You must add the binding at the time you intend to act upon it.
Don't include the markup for the binding in the Office Open XML for inserting the content control because the process of inserting that markup will strip the binding. As with setSelectedDataAsync , you specify the content to be inserted and the coercion type. The only additional requirement for writing to a binding is to identify the binding by ID.
Notice how the binding ID used in this code bindings myBinding corresponds to the binding ID established myBinding when the binding was created in the previous function. The preceding code is all you need whether you are initially populating or replacing the content in a binding.
When you insert a new piece of content at a bound location, the existing content in that binding is automatically replaced. Check out an example of this in the previously-referenced code sample Word-Add-in-JavaScript-AddPopulateBindings , which provides two separate content samples that you can use interchangeably to populate the same binding.
Many types of content require additional document parts in the Office Open XML package, meaning that they either reference information in another part or the content itself is stored in one or more additional parts and referenced in document. Content that uses styles for formatting such as the styled text shown earlier in Figure 2 or the styled table shown in Figure 9 requires the styles.
Images such as those shown in Figures 3 and 4 include the binary image data in one and sometimes two additional parts. SmartArt diagrams such as the one shown in Figure 10 require multiple additional parts to describe the layout and content.
Charts such as the one shown in Figure 11 require multiple additional parts, including their own relationship.
You can see edited examples of the markup for all of these content types in the previously-referenced code sample Load and write Open XML in your Word add-in. You can insert all of these content types using the same JavaScript code shown earlier and provided in the referenced code samples for inserting content at the active selection and writing content to a specified location using bindings.
Before you explore the samples, take a look at a few tips for working with each of these content types. Remember, if you are retaining any additional parts referenced in document.
The same approach to editing the markup that you looked at for the preceding example with directly-formatted text applies when using paragraph styles or table styles to format your content. However, the markup for working with paragraph styles is considerably simpler, so that is the example described here.
As you see, the markup for formatted text in document. However, as explained earlier, you might want to use styles or direct formatting for different purposes: use direct formatting to specify the appearance of your text regardless of the formatting in the user's document; use a paragraph style particularly a built-in paragraph style name, such as Heading 1 shown here to have the text formatting automatically coordinate with the user's document.
Use of a style is a good example of how important it is to read and understand the markup for the content you're inserting, because it's not explicit that another document part is referenced here. If you include the style definition in this markup and don't include the styles. However, if you take a look at the styles. The styles. If you are only retaining the required style information for your content, in most cases you only need to keep the xmlns:w namespace.
The w:docDefaults tag content that falls at the top of the styles part will be ignored when your markup is inserted via the add-in and can be removed. The largest piece of markup in a styles. This information is also ignored when inserting content via your add-in and so it can be removed. Following the latent styles information, you see a definition for each style in use in the document from which you're markup was generated.
This includes some default styles that are in use when you create a new document and may not be relevant to your content. You can delete the definitions for any styles that aren't used by your content. Each built-in heading style has an associated Char style that is a character style version of the same heading format.
Unless you've applied the heading style as a character style, you can remove it. If the style is used as a character style, it appears in document. After you have opened the document, add the ImagePart object to the MainDocumentPart object by using a file stream as shown in the following code segment.
To add the image to the body, first define the reference of the image. Then, append the reference to the body. The element should be in a Run. The following code example adds a picture to an existing word document. In your code, you can call the InsertAPicture method by passing in the path of the word document, and the path of the file that contains the picture. For example, the following call inserts the picture "MyPic. Skip to main content. My download speeds are, frankly, horrible with both Edge and Firefox.
After a few tries at downloading the part I needed I thought maybe I could get a better speed with a less sophisticated browser, so I opened the site section, Part , with Internet Explorer. Once the Part had loaded into the browser I wondered what the product would look like if I printed it. So, blah, blah, blah, and then I tried printing to the PDF printer for my. The editor popped up, the document popped up and know what?
It looked pretty good! Exported that to Word and, while it didn't get any better many of the indents are missing it didn't get any worse either. I got your response a little later and tried the first part of the solution; the final is much like output from IE. Was this reply helpful? Yes No. Sorry this didn't help. Thanks for your feedback. It's reasonably easy and quite quick. You can use this to open a formatted document in Word.
What I do not know is whether this will help with the chunk of CFR that you want to format. Download that and make sure you know where to find it on your computer it may help to copy it to a folder with a name you can remember easily. You have to open this file twice to make this work, so do not be dismayed when you open something that looks like rubbish.
But do not just click the Open button as you would normally do. Click the down arrow to the right of the Open button and select Open with Transform.
0コメント