繁体   English   中英

有没有办法将其中包含多个图像的二进制文件转换为pdf

[英]Is there a way to convert a Binary file that contains multiple images inside to pdf

我正在尝试使用java将包含多个图像的二进制文件转换为pdf doc,使用itextpdf是我以正确格式获取转换文件的唯一解决方案,但是这里的问题是在输出上它为我提供了只有一个图像(第一个),而丢失了二进制文件中的其他图像。

我已经证明可以使用itextpdf来将图像添加到文档中,还可以像这样使用其他解决方案: https ://www.mkyong.com/java/how-to-convert-array-of-bytes-into -文件/
从Java中的二进制数据创建pdf

据我所知,问题在于我已经读取了二进制文件并将其存储在byte []中,并将文件的内容传递给Vector之后,

我创建了一个函数,将其作为参数Vector并创建带有内部图像的pdf,问题是它仅在pdf上插入了第一张图像,因为它无法在Vector内部将第一张图像的末尾和在这种情况下,第二个图像的开头(JPEG图像文件以FF D8开头,以FF D9结尾):

如何识别byte []的内容是jpeg?

File imgFront = new File("C:/Users/binaryFile");
byte[] fileContent;       

Vector<byte[]> records = new Vector<byte[]>();

try {

    fileContent = Files.readAllBytes(imgFront.toPath());
    records.add(fileContent);  // add the result on Vector<byte[]>

} catch (IOException e1) {
    System.out.println( e1 );
}

...

 public static String ImageToPDF(Vector<byte[]> imageVector, String pathFile) {
        String FileoutputName = pathFile + ".pdf";
        Document document = null;

        try {
            FileOutputStream fos = new FileOutputStream(FileoutputName );
            PdfWriter writer = PdfWriter.getInstance(document, fos);

            writer.open();
            document.open();  

     //loop here the ImageVector in order to get one by one the images, 
     //but I get only the first one 

            for (byte[] img : imageVector) {
                Image image = Image.getInstance(img);

                image.scaleToFit(500, 500); //size

                document.add(image);
            }
            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return FileoutputName ;

    }

我希望pdf中包含所有图像,而不仅仅是一个。

我在这里使用itextpdf库为该解决方案提供了解决方法。

首先,我将二进制文件转换为字节,然后使用强制转换将字节转换为Integer并通过字节数组( http://www.sparkhound.com/blog/detect-image-file-types)定义图像类型-通过字节阵列

我从输出中发现我的类型是Tiff:var tiff2 = new byte [] {77,77,42}​​; // TIFF

当我通过字节数组byte [] fileContent时,我已经将参数从Vector imageVector更改为==> byte [] bytes;

byte[] fileContent; 
fileContent = Files.readAllBytes(ImgFront.toPath());

ImageToPDF(fileContent, "C:/Users/Desktop/pdfWithImages");

现在,我使用以下方法获取二进制文件的页数:int numberOfPages = TiffImage.getNumberOfPages(ra); //来自itextpdf

    public static String ImageToPDF(byte[] bytes, String pathFile) {
        String fileName= pathFile + ".pdf";
        Document document = null;

            document = new Document();

        try {
            FileOutputStream fos = new FileOutputStream(fileName);
            PdfWriter writer = PdfWriter.getInstance(document, fos);

            writer.open();
            document.open();

            // Array of bytes we have read from the Binary file
            RandomAccessFileOrArray ra = new RandomAccessFileOrArray(bytes);

            // Get the number of pages the the binary file have inside
            int numberOfPages = TiffImage.getNumberOfPages(ra);

            // Loop through numberOfPages and add them on the document 
            // one by one
            for(int page = 1; page <= numberOfPages; page ++){
                Image image = TiffImage.getTiffImage(new RandomAccessFileOrArray(bytes),page);
                image.scaleAbsolute(500, 500);
                document.add(image);
            }                   

            document.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;

}

这适用于我的情况,因为当我检查了一些用作源的二进制文件时,所有二进制文件都是TIFF图像类型,因此,为了检查所有类型的图像类型,肯定需要应用更多条件,因为该用例适用于特定的图像类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM