C#iTextSharp单个单元格中的多种字体
首先,我对C#并不是那么好,自从我使用它以来已经有一段时间了。
我正在为一个提供包裹的朋友制作一个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
可以具有不同的字体大小。 有关此对象的更多信息,请阅读我的书的第2章 。
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
s, Paragraph
s, Image
s,…
PdfPCell cell = new PdfPCell(); cell.AddElement(new Paragraph("Hello")); cell.AddElement(list); cell.AddElement(image);
在此代码段list
中,类型为List
, image
的类型为Image
。
第一个片段使用文本模式 ; 第二个片段使用复合模式 。 根据您使用的模式,单元格的行为会有很大差异。
这一切都在文档中解释; 你可以在这里找到数百个C#示例。