Hello,
We have a requirement, where
1- Code needs to find that content control by matching the “databinding” field.
2- We need to insert a text after a content control and remove that content control.
More details…
First:
This is my content control in the docx. I want to find this content control using the xpath field. I cannot use Tag or anyother things to find this content control in the document. I have looked at your documentation and couldn’t find anything about this. I saw some examples where it can get the content control using Tag but that’s not required in this scenario.
<w:sdt>
<w:sdtPr>
<w:tag w:val="Level1"/>
<w:id w:val="1192111201"/>
<w:dataBinding w:xpath="ns0:Cert[1]/ns0:ChangeReasonNotes"
w:storeItemID="{70B34362-E6E2-4414-9305-644D2EB8EC01}"/>
<w:text/>
</w:sdtPr>
<w:sdtEndPr/>
<w:sdtContent>
<w:r w:rsidR="00A71A52">
<w:t>{Change Reason Notes}</w:t>
</w:r>
</w:sdtContent>
</w:sdt>
Second:
Once I found the content control, need to navigate next to the content control, Insert the Text and delete the content control. I have attached an image that shows where I need to insert the Text. If you open the image, the orange content control is the one I described in “First”, I have to insert the text right next to it.
Can you please send me the code to do this as I couldn’t find anything in your docs about this. Also it should use DocumentBuilder.
@imran.khan1 can you please attach the image that you mention, or better, the current input and expected output documents (doc, docx).
I have attached the images. The name of the images is self-explanatory but if there is any confusion, please let me know.
This is the initial Document looks like
Initial_Doc.JPG (9.7 KB)
After Inserting Text next to the content control ( found by databinding - xpath)
After_Inserting_Text.JPG (12.7 KB)
Removing the Content Control - Final Outcome
Final_Outcome.JPG (8.9 KB)
@imran.khan1
First:
To search for an SDT using xpath
, please use the following code:
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true).FirstOrDefault(item => ((StructuredDocumentTag)item).XmlMapping.XPath == path);
Second:
The easiest way to replace the SDT with another element is using the following code:
var p = new Paragraph(doc);
p.AppendChild(new Run(doc) { Text = "Replacement" });
sdt.ParentNode.InsertBefore(p, sdt);
sdt.Remove();
I am looking for an approach to achieve the same using the DocumentBuilder
class. I will get back to you as soon as possible.
About Second part, I want to do that using DocumentBuilder, as I might need to insert image\text or HTML. I said “Text” initially just to make it simple.
Please let me know
@imran.khan1 Thank you for the additional information. The simplest approach to achieve what you want is to append a paragraph as I posted in my first reply. You can then use that paragraph to move the cursor of the document builder to the right position. Please refer to the following example:
var html = @"<style type=""text/css"">
ol,ul {margin: 0px 0px -100px 0px;}
</style>
<span style='font-family: ""Times New Roman""; font-size: 12pt;'>
<p>New paragraph test.</p>
<br> <br>
<ol>
<li>Line 1</li>
<li>Line 2</li>
</ol>
</span>";
Document doc = new Document("C:\\Temp\\Test.docx");
var path = @"/ns1:coreProperties[1]/ns0:description[1]";
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChildNodes(NodeType.StructuredDocumentTag, true).FirstOrDefault(item => ((StructuredDocumentTag)item).XmlMapping.XPath == path);
if(sdt != null)
{
DocumentBuilder builder = new DocumentBuilder(doc);
var p = new Paragraph(doc);
// If I need to append text add runs
p.AppendChild(new Run(doc) { Text = "Replacement" });
sdt.ParentNode.InsertBefore(p, sdt);
// If I need to append something else, point to the paragraph
builder.MoveTo(p);
// Insert HTML if it's required
builder.InsertHtml(html);
// Insert image if it´s required
builder.InsertImage("C:\\Temp\\img.png");
sdt.Remove();
}
doc.Save("C:\\Temp\\output.docx");
Test.docx (20.3 KB)
output.docx (23.6 KB)
One more question. Whatever I insert as html, it uses the “Times New Roman” fonts (size 12). How to enforce document fonts?
@imran.khan1 That’s great. If you have any other doubts, don’t hesitate to post them.