C# itext7, how can i set html value as text - itext

I need to set the value of a field with some simple html like
val = <strong>Some text</strong>
if (fields.TryGetValue(fieldId, out PdfFormField toSet))
{
toSet.SetValue(val);
toSet.SetBorderWidth(0.0f);
}
I've tested with richText without any success.
Don't know iText that much and can't seem to find any exemple.

Related

SImpler way to render text as html

I maybe overlooking a function. In order to render text such as <div>test</div as html inside another tag, I would need several lines of code to name the outside tag, then set .innerHtml, then return the outside tag. Is there a shorter way? There are also confusing conversions with .render with this method.
ex.
val content = span(color := "blue").render
content.innerHtml = "<div>test</test>" // html is escaped
outsideTag.innerHtml = content.outerHtml
Assuming you're using Scalatags here, you may be looking for the raw() function...
I don't know scala.js that well, but as far as I understand it, a div tag is added to a span tag.
You should only add inline tags to other inline tags. So it's not a good idea to add a div to a span.
I think imho you can write:
outsideTag.innerHtml="<div color='blue'>test</div>";

Kentico Phone Format not consistent

I'm adding a US phone number to a form in Kentico 9 but the format is not consistent. When I create my form in the Form Builder it looks like this:
Well Formatted US phone format
However when I view the form it Kentico splits up the phone number into it's component parts and places them all on one line. I'm not finding a place to fix this. Seems like a silly way to work...
Bad Phone image
I've tried creating Custom Layouts but it doesn't appear to allow you to control the format there.
I am not sure if I understand you right - you have a form with field (Data type: text) which is using U.S. phone number form control. You can specify proper behavior in ~/CMSFormControls/Inputs/USphone.ascx (default path where are files for this form control). You can specify css classes in USphone.ascx and general behavior in USphone.ascx.cs file. Please note this property:
public override object Value
{
get
{
if (IsEmpty())
{
return String.Empty;
}
return String.Format("({0}) {1}-{2}", txt1st.Text, txt2nd.Text, txt3rd.Text);
}
.
.
.
}
In return there is specified way you are formatting text - this might help you to achieve your desired behavior.

Setting AcroField text color in itextSharp

I am using iTextSharp 5.5.3 i have a PDF with named fields i created with Adobe lifecycle I am able to fill the fields using iTextSharp but when i change the textcolor for a field it does not change. i really dont know why this is so. here is my code below
form.SetField("name", "Michael Okpara");
form.SetField("session", "2014/2015");
form.SetField("term", "1st Term");
form.SetFieldProperty("name", "textcolor", BaseColor.RED, null);
form.RegenerateField("name");
If your form is created using Adobe LifeCycle, then there are two options:
You have a pure XFA form. XFA stands for the XML Forms Architecture and your PDF is nothing more than a container of an XML stream. There is hardly any PDF syntax in the document and there are no AcroForm fields. I don't think this is the case, because you are still able to fill out the fields (which wouldn't work if you had a pure XFA form).
You have a hybrid form. In this case, the form is described twice inside the PDF file: once using an XML stream (XFA) and once using PDF syntax (AcroForm). iText will fill out the fields in both descriptions, but the XFA description gets preference when rendering the document. Changing the color of a field (or other properties) would require changing the XML and iText(Sharp) can not do that.
If I may make an educated guess, I would say that you have a hybrid form and that you are only changing the text color of the AcroForm field without changing the text color in the XFA field (which is really hard to achieve).
Please try adding this line:
form.RemoveXfa();
This will remove the XFA stream, resulting in a form that only keeps the AcroForm description.
I have written a small example named RemoveXFA using the form you shared to demonstrate this. This is the C#/iTextSharp version of that example:
public void ManipulatePdf(String src, String dest)
{
PdfReader reader = new PdfReader(src);
PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create));
AcroFields form = stamper.AcroFields;
form.RemoveXfa();
IDictionary<String, AcroFields.Item> fields = form.Fields;
foreach (String name in fields.Keys)
{
if (name.IndexOf("Total") > 0)
form.SetFieldProperty(name, "textcolor", BaseColor.RED, null);
form.SetField(name, "X");
}
stamper.Close();
reader.Close();
}
In this example, I remove the XFA stream and I look over all the remaining AcroFields. I change the textcolor of all the fields with the word "Total" in their name, and I fill out every field with an "X".
The result looks like this: reportcard.pdf
All the fields show the letter "X", but the fields in the TOTAL column are written in red.
I finally found a way, guess the problem was coming from using Adobe LC, so i switched to Open Office it all worked but when i flatten the form everything disappears. I found a solution to that here ITextSharp PDFTemplate FormFlattening removes filled data
Thanks Mr Lowagie for your help

GWT: RichTextArea getHTML loses "outer" tags

Please bear with me as I am a newbie in gwt and front end stuff.
I have a html string:
String s=
"<html><head><title>Hello World</title></head><body><b>Hello World</b></body></html>";
(I am using spaces in the tags to prevent the text from displaying "htmlized".)
//and gwt RichTextArea control->richTextArea
richTextArea.setHTML(s);
//So far so good as the document String displays as desired.
//Now comes the problem...
String transformed = richTextArea.getHTML();
The rich text area strips the outer and returns the inner html only. i.e. The body, html and the head tags are stripped.
Q How do I get the html string returned with only the modifications which occur in the rich text area showing.. i.e. The original "outer" tags do not get lost.
Hope I am adequately clear.
You don't have to set them in setHTML <b>hello world<b/> is enough.
Also if you set the outer tags you can't get them, as they don't add anything in the formatting of the text.

HTML Tags in GWT internationalization

I'm using the internationalization of GWT to manage the different languages of my application. I have a text where some words are in bold. Therefore I did the same thing as described here.
#DefaultMessage("Welcome back, {startBold,<b>}{0}{endBold,</b>}")
String testMessage(String name);
However, when I run the application, I get "Welcome back, < b>Peter< /b>" (the HTML is written out and not interpreted. I intentionally put a space between < b so that this text editor does not interpret the html tag).
Does anyone know how to solve this issue? Many thanks in advance!
P.S.
Code fragment which gets the language string:
Label label = new Label();
label.addStyleName("intro-Text");
label.setText(new HTML(trans.testMessage(name)).getHTML());
Instead of using a Label use the HTML widget.
HTML text = new HTML();
text.addStyleName("intro-Text");
text.setHTML(trans.testMessage(name));
Hope that helps.