Me again,
After few hours, finally did something that's working. But, with *memory
leaks* ...
juin 26, 2015 3:39:22 PM org.apache.pdfbox.cos.COSDocument finalize
>
> AVERTISSEMENT: Warning: You did not close a PDF Document
>
>
My code to split a PDF (i'm now returning a PDF byte[]) :
private byte[] decouperDesPagesSymetriques(int nbOfCrops, PDPageTree pages,
> int CropTop, int CropBottom) throws IOException{
> PDDocument pdfSplit = new PDDocument();
> PDDocument pagesSplit =new PDDocument();
> byte[] pdfBytes = null;
> [...] processing
> }
> try {
> pdfBytes = toByteArray(pdfSplit);
> }
> catch (Exception e) {
> e.printStackTrace();
> }
> finally{
> pagesSplit.close();
> pdfSplit.close();
> }
> return pdfBytes;
> }
> private static byte[] toByteArray(PDDocument pdDoc) throws IOException{
> ByteArrayOutputStream out = new ByteArrayOutputStream();
> try {
> pdDoc.save(out);
> } catch (Exception e) { e.printStackTrace();}
> finally{
> pdDoc.close();
> }
> return out.toByteArray();
> }
My new code to generate images (pdfSplitted is now global) :
private String pdfImgsToXml(byte[] pdfBytes) throws IOException {
> int pageNumber = 1;
> String imgs = "<imgs>\n";
> pdfSplitted = new PDDocument();
> System.out.println(new String(pdfBytes));
> pdfSplitted = PDDocument.load(pdfBytes);
> PDFRenderer renderer = new PDFRenderer(pdfSplitted);
> /* Test enregistrement
> pdfSplitted.save("D:/Stage_DUT/pdfConverter/pdf/test4.pdf");
> */
> int numPages = pdfSplitted.getNumberOfPages();
> for (int i = 0; i < numPages; i++) {
> imgs = imgs + "<img" + pageNumber + ">";
> try {
> BufferedImage image = renderer.renderImageWithDPI(i, 200);
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> ImageIO.write( image, "jpg", baos );
> //baos.flush();
> byte[] imageInByte = baos.toByteArray();
> baos.close();
> String imgString = Base64.encode(imageInByte);
> imgs = imgs + imgString + "</img" + pageNumber + ">\n";
> } catch (Exception e){
> e.printStackTrace();
> }
> pageNumber++;
> }
> return imgs + "</imgs>";
> }
As you can see,* all documents are closed (theory not practice)*. Strange
thing. (Used try-catch-finally to force this). Any advice ?