Reputation: 211
I am using iTextsharp tp create PDF. I have following lines of code to show text on PDF.
var contentByte = pdfWriter.DirectContent;
contentByte.BeginText();
contentByte.SetFontAndSize(baseFont, 10);
var multiLine = " Request for grant of leave for ____2______days";
contentByte.ShowTextAligned(PdfContentByte.ALIGN_LEFT, multiLine, 100, 540, 0);
contentByte.EndText();
I need to replace "____" with underline. On underline "2" should display.
Please help me to solve this.
I solved this by your answer. thank u.. @ Chris Haas
var baseFont = BaseFont.CreateFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var mainFont = new iTextSharp.text.Font(baseFont, 10);
//Our Phrase will hold all of our chunks
var p = new Phrase();
//Add the start text
p.Add(new Chunk("Request for grant of leave for ", mainFont));
var space1 = new Chunk(" ", FontFactory.GetFont(FontFactory.HELVETICA, 12.0f, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE));
p.Add(space1);
//Add our underlined text
var c = new Chunk("2", mainFont);
c.SetUnderline(0.1f, -1f);
p.Add(c);
var space1 = new Chunk(" ", FontFactory.GetFont(FontFactory.HELVETICA, 12.0f, iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE));
p.Add(space1);
//Add our end text
p.Add(new Chunk(" days", mainFont));
//Draw our formatted text
ColumnText.ShowTextAligned(pdfWriter.DirectContent, PdfContentByte.ALIGN_LEFT, p, 100, 540, 0);
Upvotes: 0
Views: 1932
Reputation: 55427
Instead of using a PdfContentByte
directly which only allows you to draw strings you can use a ColumnText
which allows you access to iText's abstractions, specifically a Chunk
which has a SetUnderline()
method on it.
//Create our base font and actual font
var baseFont = BaseFont.CreateFont(fontFile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
var mainFont = new iTextSharp.text.Font(baseFont, 10);
//Our Phrase will hold all of our chunks
var p = new Phrase();
//Add the start text
p.Add(new Chunk("Request for grant of leave for ", mainFont));
//Add our underlined text
var c = new Chunk("2", mainFont);
c.SetUnderline(0.1f, -1f);
p.Add(c);
//Add our end text
p.Add(new Chunk(" days", mainFont));
//Draw our formatted text
ColumnText.ShowTextAligned(pdfWriter.DirectContent, PdfContentByte.ALIGN_LEFT, p, 100, 540, 0);
Upvotes: 1