字符串之间的 iTextsharp 下划线

本文关键字:下划线 iTextsharp 之间 字符串 | 更新日期: 2023-09-27 18:30:18

我正在使用iTextsharp tp create PDF。我有以下代码行在 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();

我需要用下划线替换"____"。在下划线上应显示"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);

字符串之间的 iTextsharp 下划线

与其直接使用只允许您绘制字符串的PdfContentByte,不如使用允许您访问 iText 抽象的ColumnText,特别是具有SetUnderline()方法的Chunk

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