2013-06-03 142 views
12

我需要读取带有filepath“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]更不用说[itext] ? – 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(); 

    } 

} 

The到目前为止最简单的方法。希望这可以帮助。

+0

Thx寻求帮助。这正是我需要的。 –

+1

您的代码中可能缺少某些东西,或者我错过了某些东西?我得到的文件有0个字节,我无法打开它。我将用代码编辑我的问题。 –

+0

@JurajVlahović:完美的作品。 – ankurtr

10

您可以使用Apache的PdfBox,它易于使用且性能良好。

下面是一个PDF文件中提取文本的例子(你可以阅读更多here):

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

相关问题