[英]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.