PDF to byte array and vice versa

前端 未结 13 1221
独厮守ぢ
独厮守ぢ 2020-11-27 04:19

I need to convert pdf to byte array and vice versa.

Can any one help me?

This is how I am converting to byte array

public static byte[] conve         


        
13条回答
  •  隐瞒了意图╮
    2020-11-27 04:34

    This worked for me. I haven't used any third-party libraries. Just the ones that are shipped with Java.

    import java.io.*;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    
    public class PDFUtility {
    
    public static void main(String[] args) throws IOException {
        /**
         * Converts byte stream into PDF.
         */
        PDFUtility pdfUtility = new PDFUtility();
        byte[] byteStreamPDF = pdfUtility.convertPDFtoByteStream();
        FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\aseem\\Desktop\\BlaFolder\\BlaFolder2\\aseempdf.pdf");
        fileOutputStream.write(byteStreamPDF);
        fileOutputStream.close();
        System.out.println("File written successfully");
    }
    
    /**
     * Creates PDF to Byte Stream
     *
     * @return
     * @throws IOException
     */
    protected byte[] convertPDFtoByteStream() throws IOException {
        Path path = Paths.get("C:\\Users\\aseem\\aaa.pdf");
        return Files.readAllBytes(path);
    }
    
    }
    

提交回复
热议问题