如何将pdf文件转换为CSV文件? [英] How to convert a pdf file into CSV file?

查看:567
本文介绍了如何将pdf文件转换为CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将PDF文件转换为CSV文件。
我正在使用iText库。
该程序工作正常,但输出格式不正确。
所有数据都来自csv文件的第一行。输出应与pdf文件完全相同(表示换行符)。
请帮忙。
提前致谢。

I want to convert a PDF file into a CSV file. I am using iText library for this. The program is working fine but the output is not in desired format. All the data is coming in first line of the csv file. The output should be exactly same as pdf file(means with line breaks). Please help. Thanks in advance.

Document document = new Document();
  document.open();
  PdfReader reader = new PdfReader("C:\\Indiaops-projects\\PREMIUM_PAID_ACKNOWLEDGEMENT.pdf");
  PdfDictionary dictionary = reader.getPageN(1);
  AcroFields fileds = reader.getAcroFields();
  PRIndirectReference reference = (PRIndirectReference) 
            dictionary.get(PdfName.CONTENTS);
                    PRStream stream = (PRStream) PdfReader.getPdfObject(reference);
                    byte[] bytes = PdfReader.getStreamBytes(stream);
                    PRTokeniser tokenizer = new PRTokeniser(bytes);
                    FileOutputStream fos=new FileOutputStream("C:\\Indiaops-projects\\pdf.csv");
                    StringBuffer buffer = new StringBuffer();
                    StringBuffer data = new StringBuffer();
                    int i=0;
                    while (tokenizer.nextToken()) {
                    if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
                        String value = tokenizer.getStringValue();

                        if("x-none".equals(value)){
                            String datastr =data.toString();
                            if(!"".equals(datastr)){
                                buffer.append("\""+datastr+"\",");
                                data = new  StringBuffer();
                            }
                        }else{
                               data.append(value);
                        }
                     }
                  }
            String test=buffer.toString();
            StringReader stReader = new StringReader(test);
            int t;
            while((t=stReader.read())>0)
            fos.write(t);
            document.add(new Paragraph(".."));
            document.close();


推荐答案

你需要引入一个换行符'\ n '在每个表行之后的缓冲区中。

You need to introduce a line break '\n' in the buffer after each table row.

buffer.append("\n");

这篇关于如何将pdf文件转换为CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆