Hi,
with the following code you will add a pink underlined and horizontally centered text to your pdf.
There is no way to direclty underline the text, so we created the text and then we underlined it with a line.
The only way to add text, line, image, form, grid.. etc to the pdf is through PageContent.
Page page = m_doc.NewPage(pageno, w, h);//create A4 paper
PageContent content = new PageContent();
content.Create();//create content, do not forget.
//set alpha for both fill and stroke
DocGState dgs = m_doc.NewGState();
dgs.SetFillAlpha(255);//set alpha value to 0.5
dgs.SetStrokeAlpha(255);//set alpha value to 0.5
ResGState rgs = page.AddResGState(dgs);
content.GSSet(rgs);
//prepare to write texts
content.GSSave();
int style = 1;
DocFont dfont = m_doc.NewFontCID("DroidSansFallback", style);//bold-italic and embed in horizontal writing
if(dfont == null)
{
dfont = m_doc.NewFontCID("Roboto-Regular", style);
if(dfont == null)
dfont = m_doc.NewFontCID("DroidSans", style);
}
ResFont rfont = page.AddResFont(dfont);
//center text
String text = "Pink text!";
float[] size = content.TextGetSize(rfont, text, 22, 22,0, 0.2f);
content.GSSave();
Matrix mat = new Matrix( 1, 1, (w-size[0])/2, 777 );
content.GSSetMatrix(mat);
mat.Destroy();
content.TextBegin();
content.TextSetFont(rfont, 22);//set font and size
content.SetFillColor(0xff69b4);//set fill color to black-green.
content.TextSetCharSpace(0);
content.TextSetWordSpace(0.2f);
/*content.TextSetLeading(16);
content.TextSetRenderMode(0);//fill and stroke
content.TextSetHScale(120);//set horizontal scale*/
content.DrawText(text);
content.TextEnd();
content.GSRestore();
//underline
dgs = m_doc.NewGState();
dgs.SetFillAlpha(255);//set alpha value to 0.5
dgs.SetStrokeAlpha(255);//set alpha value to 0.5
rgs = page.AddResGState(dgs);
content.GSSet(rgs);
content.GSSave();
Matrix matUnder = new Matrix( 1, 1, (w-size[0])/2, 770 );
content.GSSetMatrix(matUnder);
matUnder.Destroy();
//build a path object
Path pathUnder = new Path();
pathUnder.MoveTo(1, 1);
pathUnder.LineTo(size[0] - 1, 1);
pathUnder.ClosePath();
//fill it
content.SetStrokeColor(0xff69b4);//set pink color;
content.SetStrokeWidth(2);//set stroke width
content.StrokePath(pathUnder);
content.GSRestore();
//add content to page
page.AddContent(content, true);
content.Destroy();
page.Close();