iTextsharp字符串之间的下划线

时间:2016-01-13 11:20:54

标签: c# asp.net itextsharp

我正在使用iTextsharp创建PDF。我有以下代码行来显示PDF文本。

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);

我需要用下划线替换“____”。在下划线“2”应显示。

请帮我解决这个问题。

我通过你的回答解决了这个问题。谢谢你...... @ Chris Haas

Diagnostic Hub Tools cannot run because a required update to Windows is not installed. To resolve this issue, install the required update from 'http://support.microsoft.com/kb/2882822'.

1 个答案:

答案 0 :(得分:1)

不是直接使用只允许你绘制字符串的PdfContentByte,而是可以使用ColumnText来访问iText的抽象,特别是Chunk,其SetUnderline()方法就可以了。

//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);