使用iText 设置PDF文件的样式和格式

示例:

Font blueFont = FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL, new CMYKColor(255, 0, 0, 0));
Font redFont = FontFactory.getFont(FontFactory.COURIER, 12, Font.BOLD, new CMYKColor(0, 255, 0, 0));
Font yellowFont = FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new CMYKColor(0, 0, 255, 0));
Document document = new Document();
try
{
	PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream('StylingExample.pdf'));
	document.open();
	//document.add(new Paragraph('Styling Example'));
	
	//  设置段落的样式和颜色
	Paragraph paragraphOne = new Paragraph('Some colored paragraph text', redFont);
	document.add(paragraphOne);
	
	// 创建章节
	Paragraph chapterTitle = new Paragraph('Chapter Title', yellowFont);
	Chapter chapter1 = new Chapter(chapterTitle, 1);
	chapter1.setNumberDepth(0);
	Paragraph sectionTitle = new Paragraph('Section Title', redFont);
	Section section1 = chapter1.addSection(sectionTitle);
	Paragraph sectionContent = new Paragraph('Section Text content', blueFont);
	section1.add(sectionContent);
	document.add(chapter1);
	document.close();
	writer.close();
} catch (Exception e)
{
	e.printStackTrace();
}
日期:2020-09-17 00:09:30 来源:oir作者:oir