使用 iText 将文本文件数据转换为 PDF 文件 Java
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();
}
我已成功创建文本文件。我需要将这些文本添加到 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();
}