首先,我对 C# 不是很在行,而且我已经有一段时间没有使用它了..
我正在为运送包裹的 friend 制作一个 Windows 窗体。所以我想将他当前的纸质表格转移到带有 iTextSharp 库的 .pdf 中。他还需要打印表格以获得客户签名等。
我需要什么: 我希望表格有一个小标题,例如“公司名称”,文本应该比从 windows 窗体(richTextBox1)输入的文本小一点
目前我正在使用单元格,想知道是否可以在同一个单元格中使用 2 种不同的字体大小?
我有什么:
table.AddCell("Static headline" + Chunk.NEWLINE + richTextBox1.Text);
我“想要”的:
var normalFont = FontFactory.GetFont(FontFactory.HELVETICA, 9);
var boldFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 12);
table.AddCell("Static headline", boldFont + Chunk.NEWLINE + richTextBox1.Text, normalFont);
最佳答案
您将 String
和 Font
传递给 AddCell()
方法。那是行不通的。您需要采用 Phrase
对象或 PdfPCell
对象作为参数的 AddCell()
方法。
Phrase
是由不同的Chunk
组成的对象,不同的Chunk
可以有不同的字体大小。请阅读chapter 2 of my book有关此对象的更多信息。
Phrase phrase = new Phrase();
phrase.Add(
new Chunk("Some BOLD text", new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD))
);
phrase.Add(new Chunk(", some normal text", new Font()));
table.AddCell(phrase);
PdfPCell
是一个对象,您可以向其添加不同的对象,例如 Phrase
、Paragraph
、Image
s,...
PdfPCell cell = new PdfPCell();
cell.AddElement(new Paragraph("Hello"));
cell.AddElement(list);
cell.AddElement(image);
在此代码段中,list
是List
类型,image
是Image
类型。
第一个片段使用文本模式;第二个片段使用复合模式。细胞的行为因您使用的模式而异。
这一切都在 documentation 中进行了解释;您可以找到数百个 C# 示例 here .
关于C# iTextSharp 单个单元格中的多种字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21750597/