我有一个由恒定部分和可变部分组成的字符串。我希望变量在文本段落中使用常规字体设置格式,而我希望常量部分为粗体。
这是我的代码:
val boldFont = Font(Font.FontFamily.TIMES_ROMAN, 22f, Font.BOLD)
val semiBoldFont = Font(Font.FontFamily.TIMES_ROMAN, 16f, Font.BOLD)
val normalFont = Font(Font.FontFamily.TIMES_ROMAN, 16f, Font.NORMAL)
val lineSeparator = LineSeparator()
lineSeparator.lineColor = BaseColor(0, 0, 0, 68)
// NAME OF THE STUDENT
val paragraph = Paragraph(student?.Student_Name, boldFont)
paragraph.alignment = Paragraph.ALIGN_CENTER
// DOB
val paragraphTwo = Paragraph("Date of Birth: ", semiBoldFont)
paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
paragraphTwo.alignment = Paragraph.ALIGN_CENTER
// Place and Country of Birth
val paragraphThree = Paragraph("Place and Country of Birth: ", semiBoldFont)
paragraphThree.add(Chunk(student?.Student_City + ", " + student?.Student_Country, normalFont))
paragraphThree.alignment = Paragraph.ALIGN_CENTER
// Address
val paragraphFour = Paragraph("Address: ", semiBoldFont)
paragraphFour.add(Chunk(student?.Student_Address + ", " + student?.Student_City + ", " + student?.Student_Country, normalFont))
paragraphFour.alignment = Paragraph.ALIGN_CENTER
// Nationality
val paragraphFive = Paragraph("Nationality: ", normalFont)
paragraphFive.add(Chunk(student?.Student_Nationality_One + ", " + student?.Student_Nationality_Two, normalFont))
paragraphFive.alignment = Paragraph.ALIGN_CENTER
try {
document.add(paragraph)
document.add(Chunk(lineSeparator))
document.add(paragraphTwo)
document.add(paragraphThree)
document.add(paragraphFour)
document.add(paragraphFive)
if (educationList.size > 0) {
document.add(Paragraph("Education", boldFont))
document.add(Paragraph(" "))
}
} catch (e: DocumentException) {
e.printStackTrace()
}
答案 0 :(得分:1)
如果您在Paragraph
构造函数中设置了字体,则稍后添加的Chunk
对象使用的字体是该块的字体,并由段落字体中的数据补充属性未设置为块字体 。
字体的样式是一个位字段,不幸的是,样式字段中的这个补充是通过按位或运算实现的。因此,将段落字体中的 BOLD 标志与添加到段落中的所有块的样式相加!
您可以通过根本不设置段落级别的字体(或者至少不设置带有样式位的字体)来避免这种情况,还可以将标签作为单独的块添加,例如代替
// DOB
val paragraphTwo = Paragraph("Date of Birth: ", semiBoldFont)
paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
paragraphTwo.alignment = Paragraph.ALIGN_CENTER
做
// DOB
val paragraphTwo = Paragraph()
paragraphTwo.add(Chunk("Date of Birth: ", semiBoldFont))
paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
paragraphTwo.alignment = Paragraph.ALIGN_CENTER
或者也许
// DOB
val paragraphTwo = Paragraph("", normalFont)
paragraphTwo.add(Chunk("Date of Birth: ", semiBoldFont))
paragraphTwo.add(Chunk(student?.Student_DOB , normalFont))
paragraphTwo.alignment = Paragraph.ALIGN_CENTER
(这些选项的区别在于,基于段落字体信息,在段落之前添加了一些间距。)