5

I've read the documentation and the examples but I'm having a hard time putting it all together. I'm just trying to take a test pdf file and then convert it to a byte array then take the byte array and convert it back into a pdf file then create the pdf file onto disk.

It probably doesn't help much, but this is what I've got so far:

package javaapplication1;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.pdfbox.cos.COSStream;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;

public class JavaApplication1 {

    private COSStream stream;

    public static void main(String[] args) {
        try {
            PDDocument in = PDDocument.load("C:\\Users\\Me\\Desktop\\JavaApplication1\\in\\Test.pdf");
            byte[] pdfbytes = toByteArray(in);
            PDDocument out;
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    private static byte[] toByteArray(PDDocument pdDoc) throws IOException, COSVisitorException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            pdDoc.save(out);
            pdDoc.close();
        } catch (Exception ex) {
            System.out.println(ex);
        }
        return out.toByteArray();
    }

    public void PDStream(PDDocument document) {
        stream = new COSStream(document.getDocument().getScratchFile());
    }
}
Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
  • PDF is a byte array, just read binary file and that's it. But maybe you mean something different? – Andrey Jul 17 '13 at 19:08
  • For some reason when I convert a PDF to byte array and then back to PDF, when I try to open the newly created PDF it says it's corrupt. So I decided to use pdfbox to avoid that. Is it possible to skip pdfbox and do that and get a working pdf back? –  Jul 17 '13 at 19:09
  • What is your ultimate goal? I can't see any self sufficient use of converting PDF to byte array and back. – Andrey Jul 17 '13 at 19:13
  • Well, I'm testing to see if I can store a PDF in a database. I know that a file system should be used for storage, but I'd like to store it in a database anyway. The only way I can do that is if it's in the form of a byte[] array –  Jul 17 '13 at 19:18
  • Just read it as file stream and that's it. I really see no reasons it should not work. – Andrey Jul 17 '13 at 19:35
  • Possible duplicate of [Using PdfBox, how do I retrieve contents of PDDocument as a byte array?](https://stackoverflow.com/questions/11593116/using-pdfbox-how-do-i-retrieve-contents-of-pddocument-as-a-byte-array) – vinS Jun 06 '18 at 07:12

1 Answers1

7

You can use Apache commons, which is essential in any java project IMO.

Then you can use FileUtils's readFileToByteArray(File file) and writeByteArrayToFile(File file, byte[] data).

(here is commons-io, which is where FileUtils is: http://commons.apache.org/proper/commons-io/download_io.cgi )

For example, I just tried this here and it worked beautifully.

try {
    File file = new File("/example/path/contract.pdf");
    byte[] array = FileUtils.readFileToByteArray(file);
    FileUtils.writeByteArrayToFile(new File("/example/path/contract2.pdf"), array);

} catch (IOException e) {
    e.printStackTrace();
}
monojohnny
  • 5,894
  • 16
  • 59
  • 83
Doodad
  • 1,518
  • 1
  • 15
  • 20
  • When I use this it creates corrupt pdf's when converting the byte[] back to a file though... Have you been able to do this successfully? –  Jul 17 '13 at 19:19
  • Even with FileUtils? I use it a lot with a lot of different files including some pdf's and never had any problem with it ): – Doodad Jul 17 '13 at 19:22
  • Do you have any old code that you could paste demonstrating it by any chance? –  Jul 17 '13 at 19:24
  • Updated the code with the code from a test I ran just now here and worked (except for "/example/path" which was a real path) – Doodad Jul 17 '13 at 19:30
  • Yes, that did work very well. Thank you for your help, I appreciate that. –  Jul 17 '13 at 19:35
  • 1
    No problem, mate, we are here to help each other :D Glad I could help. – Doodad Jul 17 '13 at 19:37