I am trying to split a pdf document through org.apache.pdfbox.multipdf.Splitter
and need to perform certain file operations on this single page PDDocument
,
How can I convert PDDocument
to File Object in java?
I am trying to split a pdf document through org.apache.pdfbox.multipdf.Splitter
and need to perform certain file operations on this single page PDDocument
,
How can I convert PDDocument
to File Object in java?
Very simple. I am using 1.8.16
try {
PDDocument document = PDDocument.load(new File(filename));
// do what ever you want
document.save(newfilename);
} catch (IOException | BadSecurityHandlerException | CryptographyException e) {
e.printStackTrace();
}
finally {
if(document != null )
try {
document.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//return tmpFile != null ? tmpFile.getAbsolutePath() : null;
return tmpFilename;
}
with Apache commons
InputStream is = null
try {
PDDocument document = PDDocument.load(filePath);
File targetFile = new File("nameoffile.pdf");
PDStream ps = new PDStream(document);
is = ps.createInputStream();
FileUtils.copyInputStreamToFile(is, targetFile);
} catch (IOException io) {} finally {
if (is != null)
IOUtils.closeQuietly(is);
}