简体   繁体   中英

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 ?

I solved it:

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

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

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)

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. eg:

InputStream docInputStream = null;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM