We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Inserting RTF into a FormField

Hi,

I was wondering whether you could possibly help me. Currently trying out some of the FormField insertion functionality in AsposeWords .Net and stumbled upon a bit of a problem I cannot over come.

In a nutshell we are needing to insert RTF text into a form field.

An example of which is featured below.
{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\b\fs18 te\b0 st\par }

The method of insertion we would normally use is by setting the field.Result = “”.

Unfortunately the text in the field is displaying as above string instead of the desired format TEst.

If any of you happen to have any further information, could nudge me in the right direction I would greatly appreciate it.

Thanks for your time,

James

@james.gill,

Thanks for your inquiry. Please use the following code example to insert the shared RTF in FormField. Hope this helps you.

byte[] rtfBytes = Encoding.UTF8.GetBytes(@"{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\b\fs18 te\b0 st\par }");

//Create memorystream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
//Create document from stream

Document rtfDoc = new Document(rtfStream);

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.InsertTextInput("TextInput", TextFormFieldType.Regular, "", "", 0);

builder.MoveTo(doc.Range.Fields[0].End);
builder.InsertDocument(rtfDoc, ImportFormatMode.KeepSourceFormatting);
doc.Save(MyDir + "output.docx");

We suggest you please use content control (StructuredDocumentTag) instead of FormField. Please check the following code example. Hope this helps you.

byte[] rtfBytes = Encoding.UTF8.GetBytes(@"{\rtf1\ansi\ansicpg1252\deff0\deflang2057{\fonttbl{\f0\fnil\fcharset0 Arial;}} \viewkind4\uc1\pard\b\fs18 te\b0 st\par }");

//Create memorystream
MemoryStream rtfStream = new MemoryStream(rtfBytes);
//Create document from stream

Document rtfDoc = new Document(rtfStream);

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
sdt.RemoveAllChildren();
sdt.AppendChild(new Paragraph(doc));

doc.FirstSection.Body.AppendChild(sdt);

builder.MoveTo(sdt.FirstChild);
builder.InsertDocument(rtfDoc, ImportFormatMode.KeepSourceFormatting);
doc.Save(MyDir + "output.docx");