Java 如何读取pdf文件并将其写入outputStream
我需要读取一个文件路径为“C:\file.pdf”的pdf文件,并将其写入outputStream。最简单的方法是什么Java 如何读取pdf文件并将其写入outputStream,java,pdf,outputstream,Java,Pdf,Outputstream,我需要读取一个文件路径为“C:\file.pdf”的pdf文件,并将其写入outputStream。最简单的方法是什么 @Controller public class ExportTlocrt { @Autowired private PhoneBookService phoneBookSer; private void setResponseHeaderTlocrtPDF(HttpServletResponse response) { response.setContentTyp
@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();
}
}
}
您可以使用Apache,它使用简单,性能良好
以下是从PDF文件中提取文本的示例(您可以阅读更多内容):
更新:
您可以使用以下方式获取文本:
到目前为止最简单的方法。希望这有帮助。Pdf包含带有一些小文本的图片。我不需要将其写入txt或其他文件,只需要将其写入OutputStream。这只是一个示例,您可以轻松修改它查看我的最新答案使用PDF处理库复制PDF是一个相当过分的做法,不是吗…它值得商榷。。。如果将来它需要对pdf进行一些修改和/或性能不是一个很大的因素,我不明白为什么不使用lib,特别是因为lib与主应用程序分离,所以它更干净。你的问题似乎要求将文件复制到一个专用的
输出流
,而@Pheonix的答案说明了如何要做到这一点——你有什么理由把你的问题标记为[pdf]更不用说[itext]?我在我的项目中使用了itext来做其他事情,所以我认为它在这个例子中可能有用。我错了。事实上,就像@Stephan的答案提供了一个使用PDFBox的解决方案一样,您也可以使用iText首先解析整个PDF,然后再次序列化它。但是使用PDF库(无论是PDFBox还是iText)以这种方式复制PDF是一种巨大的资源浪费,并且可能会更改所讨论的PDF代码>Thx以获取帮助。这正是我所需要的。是你的代码中缺少了什么,还是我遗漏了什么?我得到的文件有0个字节,我无法打开它。我将用代码编辑我的问题。@JurajVlahović:效果很好。你能看一下我的代码吗,也许我遗漏了一些我发现问题的东西。现在它可以正常工作了,我将编辑我的代码。再一次
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();
}
}
}
PDFTextStripper reader = new PDFTextStripper();
String pageText = reader.getText(pd); // PDDocument object created
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();
}
}