![](/img/trans.png)
[英]convert a binary string contains “0101010101001” for instance to actual binary file
[英]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結尾):
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.