Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it
    Registrato dal
    Jun 2009
    Messaggi
    80

    PDDocument to ByteArray

    Ciao a tutti,
    sto cercando di ottenere un bytearray da un PDDocument (PDFBox library).
    Sto provando con il PDStream ma ottengo un pdStream.getByteArray() con length a 0.

    codice:
    public static byte[] toByteArray(PDDocument pdDoc) throws IOException{ 
    PDStream pdStream = new PDStream(pdDoc); 
    return pdStream.getByteArray(); }
    Sapreste aiutarmi??

    Grazie

  2. #2
    Basta una breve occhiata al javadoc per accorgersi del metodo "save" che accetta un outputStream...

  3. #3
    Utente di HTML.it
    Registrato dal
    Jun 2009
    Messaggi
    80
    L'occhiata in realtà l'avrei data ma con questa soluzione ho un problema.
    Dovendo salvare questo pdf su DB lo converto in un bytearray.
    Ho fatto un controllo e ho provato a generare il file a partire dal bytearray e il risultato che ho è un pdf vuoto.

    Dove sbaglio?
    Grazie

    codice:
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    try { pdDoc.save(out); 
            pdDoc.close(); 
    } catch (Exception ex) {logger.log(Level.SEVERE, null, ex);}
    
    byte[] bytes= out.toByteArray();
    
    try {
          FileOutputStream fos = new FileOutputStream(theFile);
          bos = new BufferedOutputStream(fos); 
          bos.write(bytes);
        }finally {
          if(bos != null) {
            try  {
              //flush and close the BufferedOutputStream
              bos.flush();
              bos.close();
            } catch(Exception e){}
          }
        }

  4. #4
    Molto probabilmente sbagli qualcosa nella costruzione del documento quel codice che hai postato è formalmente corretto.
    Di seguito ti posto la classe con cui ho eseguito il test
    codice:
    public class Test {
    
    	public PDDocument createDocument() throws Exception {
    		PDDocument document = new PDDocument();
    		PDPage page = new PDPage();
    		document.addPage(page);
    		PDFont font = PDType1Font.HELVETICA_BOLD;
    		PDPageContentStream contentStream = new PDPageContentStream(document,
    				page);
    		contentStream.beginText();
    		contentStream.setFont(font, 12);
    		contentStream.moveTextPositionByAmount(100, 700);
    		contentStream.drawString("Hello World");
    		contentStream.endText();
    		contentStream.close();
    		return document;
    	}
    
    	public byte[] getBytes(PDDocument document) throws Exception {
    		ByteArrayOutputStream out = new ByteArrayOutputStream();
    		document.save(out);
    
    		byte[] bytes = out.toByteArray();
    		return bytes;
    
    	}
    
    	public static void main(String[] args) throws Exception {
    		Test test = new Test();
    		PDDocument document = test.createDocument();
    		byte[] bytes = test.getBytes(document);
    		document.close();
    		System.out.println("La lunghezza della array è "+bytes.length);
    	}
    
    }

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.