[英]Text File Data into PDF File using iText Java
我已经成功创建了文本文件。 我需要将这些文本添加到PDF中。 就像我的文本文件中包含日常细节一样。 月底,我需要生成包含所有数据的pdf文件。
我在这里所做的是
try
{
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("MonthlyReport.pdf"));
document.open();
document.add(new Paragraph("Month End Report."));
//Text file Data
try {
FileReader reader = new FileReader("MyFile.txt");
int character;
while ((character = reader.read()) != -1) {
document.add(new Paragraph((char) character));
}
reader.close();
} catch (IOException exception) {
exception.printStackTrace();
}
//Text File Data
document.close();
writer.close();
} catch (DocumentException exception)
{
exception.printStackTrace();
} catch (FileNotFoundException exception)
{
exception.printStackTrace();
}
但是问题是它仅创建了Month End Report
部分。 我已经检查过FileReader
正常工作,并且工作正常。 我不明白为什么它不能与document.add
不知道是什么问题。 但是在我用BufferReader替换FileReader之后。 工作正常
try (BufferedReader br = new BufferedReader(new FileReader("MyFile.txt")))
{
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
document.add(new Paragraph(sCurrentLine));
}
} catch (IOException exception) {
exception.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.