Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java PDFBox, how to get File object from PDDocument

Tags:

java

pdfbox

I am trying to retrieve a File or InputStream instance from PDDocument without saving a PDDocument to the file system.

 PDDocument doc= new PDDocument(); 
 ...     
 doc.save("D:\\document.pdf"); 
 File f= new File("D:\\document.pdf"); 

Is there any method in PDFBox which returns File or InputStream from an existing PDDocument?

like image 974
Milos Gavrilov Avatar asked Jun 03 '13 08:06

Milos Gavrilov


3 Answers

I solved it:

PDDocument doc=new PDDocument();        
PDStream ps=new PDStream(doc);
InputStream is=ps.createInputStream();
like image 141
Milos Gavrilov Avatar answered Nov 02 '22 10:11

Milos Gavrilov


I solve it in this way ( It's creating a file but in temporary-file directory ):

final PDDocument document = new PDDocument();
final File file = File.createTempFile(filename, ".pdf");
document.save(file);

and if you need

document.close();
like image 5
Tomasz Przybylski Avatar answered Nov 02 '22 09:11

Tomasz Przybylski


What if you first create the outputstream

PDDocument doc= new PDDocument(); 
File f= new File("D:\\document.pdf");
FileOutputStream fOut = new FileOutputStream(f);  
doc.save(fOut); 

Take a look at this http://pdfbox.apache.org/apidocs/org/apache/pdfbox/pdmodel/PDDocument.html#save(java.io.OutputStream)

like image 1
fGo Avatar answered Nov 02 '22 08:11

fGo