Milos Gavrilov
Milos Gavrilov

Reputation: 436

Java PDFBox, how to get File object from PDDocument

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?

Upvotes: 9

Views: 27454

Answers (5)

sweetcookitalys
sweetcookitalys

Reputation: 1

**I'm trying the same thing I discovered this class

Class PageExtractor

you can try it like this**

Upvotes: -1

mkl
mkl

Reputation: 95888

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

[...]

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

Obviously PDFBox cannot return a meaningful File object without saving a PDDocument to the file system.

It does not offer a method providing an InputStream directly either but it is easy to write code around it that does. e.g.:

InputStream docInputStream = null;

try (   ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PDDocument doc = new PDDocument()   )
{
    [...]
    doc.save(baos);
    docInputStream = new ByteArrayInputStream(baos.toByteArray());
}

Upvotes: 0

Tomasz Przybylski
Tomasz Przybylski

Reputation: 495

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();

Upvotes: 5

Milos Gavrilov
Milos Gavrilov

Reputation: 436

I solved it:

PDDocument doc=new PDDocument();        
PDStream ps=new PDStream(doc);
InputStream is=ps.createInputStream();

Upvotes: 9

fGo
fGo

Reputation: 1146

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)

Upvotes: 1

Related Questions