Free Support Forum - aspose.com

Importing FormField Nodes from one document to another

Hi,

I am trying to import Form Fields from one document to another using the following code. The form field is not being imported to the new document. What am I missing?

protected void CreateDocument(object sender, EventArgs e)

{

Document doc = new Document();

DocumentBuilder builder = new DocumentBuilder(doc);

string html1 = "

Para1

";

doc.Protect(ProtectionType.AllowOnlyFormFields);

builder.InsertHtml(html1);

builder.InsertBreak(BreakType.SectionBreakContinuous);

builder.InsertTextInput("FF1", TextFormFieldType.Regular, "", "FormField2", 0);

builder.InsertBreak(BreakType.SectionBreakContinuous);

builder.InsertTextInput("FF2", TextFormFieldType.Regular, "", "FormField3", 0);

builder.InsertBreak(BreakType.SectionBreakContinuous);

doc.Sections[0].ProtectedForForms = false;

string location = @"C:\Documents\";

doc.Save(location + "DummyDoc.docx");

ImportFormFields();

}

protected void ImportFormFields()

{

string location = @"C:\Documents\DummyDoc.docx";

Document doc = new Document(location);

//I tried using FormFieldCollection too

NodeCollection nodes = doc.GetChildNodes(NodeType.FormField, true);

Document newDoc = new Document();

DocumentBuilder newDocBuilder = new DocumentBuilder(newDoc);

FormField frm = (FormField)nodes[0];

newDocBuilder.Document.ImportNode(frm, true);

string saveLocation = @"C:\Documents\";

newDoc.Save(saveLocation + "DummyDoc2.docx");

}

Hi

Thanks for your request. The problem occurs because FormField is actually not a single Node. This is a field and consists of FieldStart, FieldEnd, FieldSeparator, FormField, BoormarkStart, BoormarkEnd and Run Nodes. Please see the attached screenshot. So in order to copy form field, you have to copy all these nodes. For example, see the following modified code:

protected void ImportFormFields()

{

Document newDoc = new Document();

DocumentBuilder newDocBuilder = new DocumentBuilder(newDoc);

Document doc = new Document(@"Test001\out.docx");

// Get collection of FieldStart Nodes.

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);

// Get starts of FormText fields and copy these fields into the destination document.

foreach (FieldStart fieldStart in starts)

{

if(fieldStart.FieldType!=FieldType.FieldFormTextInput)

continue;

Node currentNode = fieldStart;

while (currentNode!=null && currentNode.NodeType!=NodeType.FieldEnd)

{

Node nextNode = currentNode.NextSibling;

// insert the current node into the destination document.

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode, true));

// move to the next node.

currentNode = nextNode;

}

if (currentNode != null)

{

// Copy the FieldEnd node.

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode, true));

// If bookmark end is after the current node, we shoudl copy it too.

if (currentNode.NextSibling != null && currentNode.NextSibling.NodeType == NodeType.BookmarkEnd)

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode.NextSibling, true));

}

}

newDoc.Save(@"Test001\out1.docx");

}

Hope this helps.

Best regards,

Alexey,

I've implemented the method you've provided. However, the problem with using this logic is that if a FormField had multiple lines (line1\rline2) then only Line1 is getting retrieved and inserted into the new document.

Really appreciate your help!!

Thanks

Hi

Thanks for your request. You can try using the following modified code:

protected void ImportFormFields()

{

Document newDoc = new Document();

DocumentBuilder newDocBuilder = new DocumentBuilder(newDoc);

Document doc = new Document(@"Test001\out.docx");

// Get collection of FieldStart Nodes.

NodeCollection starts = doc.GetChildNodes(NodeType.FieldStart, true);

// get values from the formfields. We will temporary store them in a hashtable.

Hashtable ffValues = new Hashtable();

foreach (FormField formField in doc.Range.FormFields)

{

ffValues[formField.Name] = formField.Result;

formField.Result = "";

}

// Get starts of FormText fields and copy these fields into the destination document.

foreach (FieldStart fieldStart in starts)

{

if(fieldStart.FieldType!=FieldType.FieldFormTextInput)

continue;

Node currentNode = fieldStart;

while (currentNode!=null && currentNode.NodeType!=NodeType.FieldEnd)

{

Node nextNode = currentNode.NextSibling;

// insert the current node into the destination document.

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode, true));

// move to the next node.

currentNode = nextNode;

}

if (currentNode != null)

{

// Copy the FieldEnd node.

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode, true));

// If bookmark end is after the current node, we shoudl copy it too.

if (currentNode.NextSibling != null && currentNode.NextSibling.NodeType == NodeType.BookmarkEnd)

newDocBuilder.InsertNode(newDoc.ImportNode(currentNode.NextSibling, true));

}

}

// Reset values of formfields.

foreach (DictionaryEntry dictionaryEntry in ffValues)

{

doc.Range.FormFields[(string)dictionaryEntry.Key].Result = (string)dictionaryEntry.Value;

newDoc.Range.FormFields[(string)dictionaryEntry.Key].Result = (string)dictionaryEntry.Value;

}

newDoc.Save(@"Test001\out1.docx");

}

Hope this helps.

Best regards,