简体   繁体   English

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

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

I'm trying to convert a binary file that contains multiple images inside to a pdf doc using java, using itextpdf was the only solutions that I get the converted file in the correct format, but the issue here is that on the output it provide me only one image(the first one), and lost the other ones that are inside the binary file. 我正在尝试使用java将包含多个图像的二进制文件转换为pdf doc,使用itextpdf是我以正确格式获取转换文件的唯一解决方案,但是这里的问题是在输出上它为我提供了只有一个图像(第一个),而丢失了二进制文件中的其他图像。

I've already prove to use itextpdf in order to add the images in a document also some other solutions like this one : https://www.mkyong.com/java/how-to-convert-array-of-bytes-into-file/ or 我已经证明可以使用itextpdf来将图像添加到文档中,还可以像这样使用其他解决方案: https ://www.mkyong.com/java/how-to-convert-array-of-bytes-into -文件/
create pdf from binary data in java 从Java中的二进制数据创建pdf

As I understand the issue in my case is that I've read my binary file and store them on a byte[] and after I've pass the content of the file to a Vector, 据我所知,问题在于我已经读取了二进制文件并将其存储在byte []中,并将文件的内容传递给Vector之后,

I've create a function that get as argument Vector and create a pdf with the images inside, the issue is that it insert only the first image on the pdf, because it can not separate inside the Vector the end of the first image and the start of the second image like in this case (JPEG image files begin with FF D8 and end with FF D9.) : 我创建了一个函数,将其作为参数Vector并创建带有内部图像的pdf,问题是它仅在pdf上插入了第一张图像,因为它无法在Vector内部将第一张图像的末尾和在这种情况下,第二个图像的开头(JPEG图像文件以FF D8开头,以FF D9结尾):

How to identify contents of a byte[] is a jpeg? 如何识别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 ;

    }

I expect that in the pdf to have all the images inside, not only one. 我希望pdf中包含所有图像,而不仅仅是一个。

I've made a workaround for the solution here using the itextpdf library. 我在这里使用itextpdf库为该解决方案提供了解决方法。

First I convert the Binary file to bytes, after use the cast in order to convert the bytes to Integer and define the type of image through Byte Array, http://www.sparkhound.com/blog/detect-image-file-types-through-byte-arrays 首先,我将二进制文件转换为字节,然后使用强制转换将字节转换为Integer并通过字节数组( http://www.sparkhound.com/blog/detect-image-file-types)定义图像类型-通过字节阵列

I found out that my type was Tiff from the output: var tiff2 = new byte[] { 77, 77, 42 }; 我从输出中发现我的类型是Tiff:var tiff2 = new byte [] {77,77,42}​​; // TIFF // TIFF

I've changed the parameters from Vector imageVector, to ==> byte[] bytes when I pass the array of bytes byte[] fileContent; 当我通过字节数组byte [] fileContent时,我已经将参数从Vector imageVector更改为==> byte [] bytes;

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

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

Now I get the number of pages the the binary file using: int numberOfPages = TiffImage.getNumberOfPages(ra); 现在,我使用以下方法获取二进制文件的页数:int numberOfPages = TiffImage.getNumberOfPages(ra); // From itextpdf //来自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;

}

This one works for my case because as I've checked some of the binary files I'm using as source all of them are as TIFF image type, for sure in order to check all the kind of image type need to apply more conditions because this use case is for a particular image type. 这适用于我的情况,因为当我检查了一些用作源的二进制文件时,所有二进制文件都是TIFF图像类型,因此,为了检查所有类型的图像类型,肯定需要应用更多条件,因为该用例适用于特定的图像类型。

暂无
暂无

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

相关问题 例如将包含“ 0101010101001”的二进制字符串转换为实际的二进制文件 - convert a binary string contains “0101010101001” for instance to actual binary file 如何在java中将PDF(它只包含tiff图像)转换为JPG图像 - How to convert PDF (it contains only tiff Images) to JPG Image in java 使用 PDFBox 将图像转换为 PDF 文件 - Convert images to PDF file using PDFBox 将 HTML 页面转换为 PDF 文件的最佳方法 - Best way to convert HTML page into PDF file 怎么把包含(图像,条形码字体)的XLSX文件转换成PDF文件? - How to convert XLSX file containing (images ,barcode font) into PDF file? PDF小丑突出显示多个搜索词失败,因为PDF包含图像,彩色文本,复杂图 - PDF Clown Highlight multiple search word is failing for PDF contains images, color text, Complex Diagrams 如何将多个图像从android中的文件夹转换为单个PDF? - how convert multiple images to single PDF from folder in android? 如何使用多线程将一个 pdf 转换为多个 png 图像 - How to convert one pdf to multiple png images with multithreading 替换pdf文件-可以与sed一起使用,但是使用Java时,pdf文件中的图像会消失。 为什么? - Replacing a pdf file — it works with sed, but when using Java, the images inside the pdf file disappear. Why? 使用Java使用iText将多个图像添加到单个pdf文件中 - Add multiple images into a single pdf file with iText using java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM