Text2Pdf.java(iText 5)
假设我们有以下文本文件: jekyll_hyde.txt
我们如何将其转换为如下所示的 PDF:
使用 iText 5 时,我们使用以下代码:
public void createPdf(String dest)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BufferedReader br = new BufferedReader(new FileReader(TEXT));
String line;
Paragraph p;
Font normal = new Font(FontFamily.TIMES_ROMAN, 12);
Font bold = new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD);
boolean title = true;
while ((line = br.readLine()) != null) {
p = new Paragraph(line, title ? bold : normal);
p.setAlignment(Element.ALIGN_JUSTIFIED);
title = line.isEmpty();
document.add(p);
}
document.close();
}
资料来源: developers.itextpdf.com