2016-01-13 155 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

相關問題