We are trying to get formatted text (some words bold, some superscript, etc) and line breaks in a rich text box field. We were able to create the xml for the RValue of a RichTextBoxField to display some words as bold and some words as underlines, however we can’t get line breaks in the RichTextBoxField. We have also been able to get line breaks in regular TextBoxFields, but then we lose the ability to make certain words bold and underlined.
Hi,
Here is the code we are using to try and populate the RichTextBoxField with formatted data:
private Stream FillInDocument(Stream stream)
{
Stream ret = new MemoryStream();
Aspose.Pdf.Document doc;
try
{
doc = new Aspose.Pdf.Document(stream);
}
catch (Exception)
{
return stream;
}
Assembly assembly = Assembly.GetExecutingAssembly();
string path = System.IO.Path.GetDirectoryName(assembly.Location) + @"\Aspose.Pdf.lic";
Aspose.Pdf.License lic = new Aspose.Pdf.License();
lic.SetLicense(path);
// Method returns the aspose field given the field name
Aspose.Pdf.InteractiveFeatures.Forms.Field formField = getField(doc, “DrugClass1Bin1Drugs”);
Aspose.Pdf.InteractiveFeatures.Forms.RichTextBoxField currentTextbox = formField as Aspose.Pdf.InteractiveFeatures.Forms.RichTextBoxField;
// The formfield wasn’t a richtextfield
if (currentTextbox == null)
return stream;
string htmlText = String.Empty;
string text = “This is some **bolded** text. This is some underlined text.
Here is some text on a new line.”;
htmlText = CreateXmlText(text);
currentTextbox.RValue = htmlText;
doc.Flatten();
doc.Save(ret);
return ret;
}
private Aspose.Pdf.InteractiveFeatures.Forms.Field getField(Document doc, string p)
{
foreach (Aspose.Pdf.InteractiveFeatures.Forms.Field formField in doc.Form)
{
if (formField.PartialName == p)
return formField;
}
return null;
}
private string CreateXmlText(string mainText)
{
string xmlString = String.Empty;
xmlString += “<?xml version=\"1.0\"?><body xfa:APIVersion=“Acroform:2.7.0.0” xfa:spec=“2.1” xmlns=“http://www.w3.org/1999/xhtml” xmlns:xfa=“[http://www.xfa.org/schema/xfa-data/1.0/">";](http://www.xfa.org/schema/xfa-data/1.0/%22%3E%22;)
xmlString += “<p dir=“ltr” style=“margin-top:0pt;margin-bottom:0pt;font-family:‘Arial’;font-size:11pt;”>”;
xmlString += mainText;
xmlString += “”;
xmlString = ReplaceBoldTags(xmlString);
xmlString = ReplaceUTags(xmlString);
xmlString = ReplaceBRTags(xmlString);
return xmlString;
}
private string ReplaceBoldTags(string mainText)
{
mainText = mainText.Replace(”**”, “<span dir=“ltr” style=“font-weight:bold;”>”);**
**mainText = mainText.Replace("**", “”);
return mainText;
}
private string ReplaceUTags(string mainText)
{
mainText = mainText.Replace("", “<span dir=“ltr” style=“text-decoration:underline;”>”);
mainText = mainText.Replace("", “”);
return mainText;
}
private string ReplaceBRTags(string mainText)
{
// doens’t work
//mainText = mainText.Replace("
", “
\r\n\n”);
//doesn’t work
//mainText = mainText.Replace("
", “<span style=“xfa-spacerun:yes”><p dir=“ltr” style=“margin-top:0pt;margin-bottom:0pt;font-family:Arial;font-size:11pt”>”);
return mainText;
}
Hi there,
The issues you have found earlier (filed as PDFNEWNET-34975) have been fixed in Aspose.Pdf for .NET 8.0.0.
This message was posted using Notification2Forum from Downloads module by Aspose Notifier.