2016-01-13 104 views
0

我正在使用iTextsharp tp创建PDF。我有以下几行代码在PDF上显示文本。iTextsharp字符串之间的下划线

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

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

请帮我解决这个问题。

我解决了你的答案。感谢ü.. @克里斯·哈斯

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

请任何人都知道这??? – RUPA

+0

您可以将html输出用于在itextsharp中创建pdf。因此,在HTML中,你可以把标签来完成 – Anabik

回答

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

感谢克里斯哈斯......它的工作。我如何扩展下划线? – RUPA

+0

used var space1 = new Chunk(“”,FontFactory.GetFont(FontFactory.HELVETICA,12.0f,iTextSharp.text.Font.BOLD | iTextSharp.text.Font.UNDERLINE)); p.Add(space1);用于扩展下划线。 – RUPA

相关问题