我正在尝试将一个csv文件解析成pdf格式。我目前的代码如下所示。
我的问题是在这个代码中生成的pdf文件只包含csv文件的第一行,我无法找出原因。我希望能够得到一个与csv文件相同的pdf版本。我确定问题出在如何向itext pdf添加数据上,但我无法找到其他将数组转发到pdf文件的方法。您有没有任何关于修复代码或更简单的解决方案的想法?
我的问题是在这个代码中生成的pdf文件只包含csv文件的第一行,我无法找出原因。我希望能够得到一个与csv文件相同的pdf版本。我确定问题出在如何向itext pdf添加数据上,但我无法找到其他将数组转发到pdf文件的方法。您有没有任何关于修复代码或更简单的解决方案的想法?
public static void main(String[] args) throws IOException, DocumentException {
@SuppressWarnings("resource")
CSVReader reader = new CSVReader(new FileReader("data1.csv") ,'\'');
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0]);
String test;
test = nextLine[0];
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream("Test.pdf"));
// step 3
document.open();
// step 4
PdfPTable arrayTable3 = new PdfPTable(1);
arrayTable3.setHorizontalAlignment(Element.ALIGN_LEFT);
Phrase phrase1 = new Phrase(nextLine[0]);
PdfPCell arrayDetailsCell3 = new PdfPCell();
arrayDetailsCell3.addElement(phrase1);
// Add the cell to the table
arrayTable3.addCell(arrayDetailsCell3);
// Add table to the document
document.add(arrayTable3);
// step 5
document.close();
}
}
CSV文件:http://dl.dropbox.com/u/11365830/data1.csv
PDF文件:http://dl.dropbox.com/u/11365830/Test.pdf