2013-06-03 38 views
12

ファイルパス "C:\ file.pdf"でpdfファイルを読み込んでoutputStreamに書き出す必要があります。それを行う最も簡単な方法は何ですか?pdfファイルを読み込んでoutputStreamに書き込む方法

@Controller 
public class ExportTlocrt { 

@Autowired 
private PhoneBookService phoneBookSer; 

private void setResponseHeaderTlocrtPDF(HttpServletResponse response) { 
    response.setContentType("application/pdf"); 
    response.setHeader("content-disposition", "attachment; filename=Tlocrt.pdf"); 
} 

@RequestMapping(value = "/exportTlocrt.html", method = RequestMethod.POST) 
public void exportTlocrt(Model model, HttpServletResponse response, HttpServletRequest request){ 

    setResponseHeaderTlocrtPDF(response); 
    File f = new File("C:\\Tlocrt.pdf"); 

    try { 
     OutputStream os = response.getOutputStream(); 
     byte[] buf = new byte[8192]; 
     InputStream is = new FileInputStream(f); 
     int c = 0; 
     while ((c = is.read(buf, 0, buf.length)) > 0) { 
      os.write(buf, 0, c); 
      os.flush(); 
     } 
     os.close(); 
     is.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 
} 

........................................... ................................................

+0

あなたの質問はファイルから専用の 'OutputStream'と@Pheonixへのコピールーチンを尋ねるようです - それを行う方法を示す---あなたの質問にタグを付けた理由は何ですか[pdf] ? – mkl

+0

私のプロジェクトでは別のもののために私はItextを使いましたので、この例では役に立つかもしれないと思いました。私は間違っていた。 –

+0

実際、@ Stephanの答えがPDFBoxを使用したソリューションを提示したのと同じように、iTextを使って最初にPDF全体を解析してから再度シリアル化することもできました。 PDFライブラリー(PDFBoxやiTextなど)でPDFをそのままコピーすることは、リソースを大量に消費し、問題のPDFを変更する可能性があります。 – mkl

答えて

24
import java.io.*; 


public class FileRead { 


    public static void main(String[] args) throws IOException { 


     File f=new File("C:\\Documents and Settings\\abc\\Desktop\\abc.pdf"); 

     OutputStream oos = new FileOutputStream("test.pdf"); 

     byte[] buf = new byte[8192]; 

     InputStream is = new FileInputStream(f); 

     int c = 0; 

     while ((c = is.read(buf, 0, buf.length)) > 0) { 
      oos.write(buf, 0, c); 
      oos.flush(); 
     } 

     oos.close(); 
     System.out.println("stop"); 
     is.close(); 

    } 

} 

これまでの最も簡単な方法。お役に立てれば。

+0

Thxのヘルプ。これは私が必要とするものです。 –

+1

コードに何か不足しているのでしょうか、何かを見逃しましたか?取得するファイルは0バイトで、開くことはできません。私はコードで自分の質問を編集します。 –

+0

@JurajVlahović:完全に機能します。 – ankurtr

10

ApacheのPdfBoxを使用すると、使いやすく、パフォーマンスが優れています。ここで

は(あなたがhere詳細を読むことができます)PDFファイルからテキストを抽出する例です:

import java.io.*; 
import org.apache.pdfbox.pdmodel.*; 
import org.apache.pdfbox.util.*; 

public class PDFTest { 

public static void main(String[] args){ 
PDDocument pd; 
BufferedWriter wr; 
try { 
     File input = new File("C:\\Invoice.pdf"); // The PDF file from where you would like to extract 
     File output = new File("C:\\SampleText.txt"); // The text file where you are going to store the extracted data 
     pd = PDDocument.load(input); 
     System.out.println(pd.getNumberOfPages()); 
     System.out.println(pd.isEncrypted()); 
     pd.save("CopyOfInvoice.pdf"); // Creates a copy called "CopyOfInvoice.pdf" 
     PDFTextStripper stripper = new PDFTextStripper(); 
     wr = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output))); 
     stripper.writeText(pd, wr); 
     if (pd != null) { 
      pd.close(); 
     } 
     // I use close() to flush the stream. 
     wr.close(); 
} catch (Exception e){ 
     e.printStackTrace(); 
     } 
    } 
} 

UPDATE:

あなたはPDFTextStripperを使用してテキストを取得することができます:

PDFTextStripper reader = new PDFTextStripper(); 
String pageText = reader.getText(pd); // PDDocument object created 
+0

Pdfには小さなテキストの写真が含まれています。私はtxtやその他のファイルに書き込む必要はなく、ただOutputStreamに書き込む必要があります。 –

+0

これはあなたが簡単にそれを変更することができる唯一の例であった – Stephan

+0

私の更新された回答を参照してください – Stephan

関連する問題