简体   繁体   中英

Convert pdf to byte[] and vice versa with pdfbox

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

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

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