Free Support Forum - aspose.com

Read and update an existing word document. Attached sample documents

Hi,
I’ve a word document with textboxes, dropdown lists, checkboxes. I need to load that document and have to update some text boxes values. So loading document, updating document like below:

Document doc = new Document(“Path to word document”);

Paragraph textboxParagraph = doc.Firstsection.Body.GetNode(NodeType.Paragraph, 6, true);

Run run = textboxParagraph.Runs[6];

run.Text = “New value”;

doc.Save(memoryStream, SaveOptions.docx);

the above code updating the text box value but when I open the document, it was all read only and gave me “The document is protected from unintentional editing” and I’ve removed the restrictions by going to Review tab, still all the editable controls (textboxes, checkboxes) are read only. What is the proper approach to load and update the existing word document? Any code examples?

Thanks in advance.

Hi,

Attached is my word document which has textbox to enter name and address, date calendar to select date. I am loading docuemnt with the following code and updating name textbox, saving document into memory stream:

Document document = new Document(System.Web.Hosting.HostingEnvironment.MapPath("~/Sample.docx"));

Paragraph nameParagrap = (Paragraph)document.FirstSection.Body.GetChild(NodeType.Paragraph, 3, true);

Run nameDataRun = nameParagraph.Runs[4];

nameDataRun.Text = "Jack";

MemoryStream ms = new MemoryStream();

document.Save(ms, SaveFormat.docx);

but all the address textbox, name textbox, calendar control is not there in the saved document. I am attaching the saved document also.

What I am doing wrong?

Thanks in advance.

Hi,


Thanks for your inquiry. I have worked with your document and have found that your document contains Structured document tags of type RichText and Date. The third text box is shape (address). Please see the attached image file for details. You can work with TextBox shape and Structured document tags by using Aspose.Words for .NET.

Please read following documentation links for your kind reference.
http://www.aspose.com/docs/display/wordsnet/StructuredDocumentTag+Class
http://www.aspose.com/docs/display/wordsnet/TextBox+Class
http://www.aspose.com/docs/display/wordsnet/About+Shapes+in+Aspose.Words

Please use the following code snippet to work with StructuredDocumentTag and TextBox.

<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document(MyDir

  • “input.docx”);<o:p></o:p>

NodeCollection sdtNodes = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);

foreach (StructuredDocumentTag sdt in sdtNodes)

{

if (sdt.SdtType == SdtType.Date)

{

sdt.DateDisplayFormat = "MM/dd/yyyy";

sdt.DateStorageFormat = SdtDateStorageFormat.Date;

sdt.RemoveAllChildren();

Run r1 = new Run(doc);

r1.Text = "25/10/2012";

sdt.AppendChild(r1);

}

else if (sdt.SdtType == SdtType.RichText)

{

sdt.RemoveAllChildren();

Run run = new Run(doc);

run.Text = "New value of Text Box";

sdt.AppendChild(run);

}

}

Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

if (shape.ShapeType == ShapeType.TextBox)

{

shape.RemoveAllChildren();

Paragraph para = new Paragraph(doc);

para.AppendChild(new Run(doc, "New Text"));

shape.AppendChild(para);

}

doc.Save(MyDir + "AsposeOut.docx");

Does the above code supports by my Aspose.Words.dll which is version 6.3? Can we write the above code with version 6.3?

Thanks for your support.

Hi,

As SturcturedDocumentTag not available in my version, so tried to get the shape for the name text box with the following line:

Shape shape = (Shape)document.GetChild(NodeType.Shape, 0, true);

but shape is null, but when I checked my input document with Document Explorer it is displaying shape for name textbox. But codee is returning me null to shape?

Thanks in advance.

Please ignore above two posts. With latest version dll, everything is working fine. But If my document has multiple textboxes how to update a particula text box?

Thanks in advance

Hi,


Thanks for your inquiry.

First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
http://www.aspose.com/docs/display/wordsnet/Object+Model+Overview
http://www.aspose.com/docs/display/wordsnet/Composition+Diagrams

Please use Document.GetChild method, it returns an Nth child node that matches the specified type.


<span lang=“EN-GB” style=“font-size:10.0pt;font-family:“Courier New”;color:#2B91AF;mso-font-kerning:
0pt;mso-no-proof:yes”>Document<span lang=“EN-GB” style=“font-size:10.0pt;
font-family:“Courier New”;mso-font-kerning:0pt;mso-no-proof:yes”> doc = new Document(MyDir

  • “in.docx”);<o:p></o:p>

Shape shape = (Shape)doc.GetChild(NodeType.Shape, 0, true);

if (shape.ShapeType == ShapeType.TextBox)

{

shape.RemoveAllChildren();

Paragraph para = new Paragraph(doc);

para.AppendChild(new Run(doc, "New Text"));

shape.AppendChild(para);

}

Please let us know if you have any more queries.