itext将PDF转换为csv [英] itext Converting PDF to csv

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

问题描述

我正在尝试使用itext框架将pdf文件转换为csv,以便导入到excel.

I am trying to use itext framework to convert a pdf file into a csv for import into excel.

输出乱码,我想我在格式转换方面缺少一步,但是我似乎无法在itext网站上找到信息并寻求帮助.

The output is garbled and I pressume I am missing a step in regards to format conversion however I can't seem to find the information in the itext site and am looking for assistance.

电流如下.

package com.pdf.convert;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class ThirdPDF {

    private static String INPUTFILE = "/location/test.pdf";
    private static String OUTPUTFILE = "/location/test.csv";

    public static void main(String[] args) throws DocumentException,
            IOException {
        Document document = new Document();

        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream(OUTPUTFILE));
        document.open();
        PdfReader reader = new PdfReader(INPUTFILE);
        int n = reader.getNumberOfPages();
        PdfImportedPage page;
        // Go through all pages
        for (int i = 1; i <= n; i++) {
            // Only page number 2 will be included
            if (i == 2) {
                page = writer.getImportedPage(reader, i);
                Image instance = Image.getInstance(page);
                document.add(instance);
            }
        }
        document.close();
    }
} 

推荐答案

将PDF文件转换为CSV文件. 当前的目录和文件创建基于Android Framework. 根据您的框架更改路径和目录.

Converting PDF file to CSV file. Present Directory and File creation is based on Android Framework. Change your path and Directory as per your Framework Accordingly.

private void convertPDFToCSV(String pdfFilePath) {
        String myfolder = Environment.getExternalStorageDirectory() + "/Mycsv";
        if (createFolder(myfolder)) {
            try {
                Document document = new Document();
                document.open();
                FileOutputStream fos=new FileOutputStream(myfolder + "/MyCSVFile.csv");
                StringBuilder parsedText=new StringBuilder();
                PdfReader reader1 = new PdfReader(pdfFilePath);
                int n = reader1.getNumberOfPages();
                for (int i = 0; i <n ; i++) {
                    parsedText.append(parsedText+PdfTextExtractor.getTextFromPage(reader1, i+1).trim()+"\n") ;
                    //Extracting the content fromx the different pages
                }
                StringReader stReader = new StringReader(parsedText.toString());
                int t;
                while((t=stReader.read())>0)
                    fos.write(t);
                document.close();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean createFolder(String myfolder) {

        File f = new File(myfolder);
        if (!f.exists()) {
            if (!f.mkdir()) {
                return false;
            } else {
                return true;
            }
        }else{
            return true;
        }
    }

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

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