㈠ java怎麼將pdf的base64轉換成jpg的base64
package com.aiait.base.util;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aiait.base.service.impl.base.SearchServiceImpl;
import org.apache.pdfbox.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class PDFUtil {
// logger
private static final Logger lOGGER = LoggerFactory.getLogger(PDFUtil.class);
public static void main(String[] args) {
// pdfTojpg("C://Test//eClaimPDF//1//Others.pdf","C://Test//eClaimPDF//WrittenConfirmation.jpg");
Date timeDiffE = null;
Date timeDiffL = null;
timeDiffE = new Date();
base64PdfToJpg(pdfBase64);
timeDiffL = new Date();
lOGGER.info("base64PdfToJpg use time: " + getDiffTime(timeDiffL, timeDiffE) + "s");
}
private static String base64PdfToJpg(String base64Pdf) {
String jpg_base64 = null;
int pdfdpi = 400;
BASE64Decoder decoder = new BASE64Decoder();
byte[] pdf_bytes = null;
try {
pdf_bytes = decoder.decodeBuffer(base64Pdf);
} catch (IOException e1) {
e1.printStackTrace();
}
try (final PDDocument document = PDDocument.load(pdf_bytes)) {
int size = document.getNumberOfPages();
/*圖像合並使用參數*/
// 定義寬度
int width = 0;
// 保存一張圖片中的RGB數據
int[] singleImgRGB;
// 定義高度,後面用於疊加
int shiftHeight = 0;
//保存每張圖片的像素值
BufferedImage imageResult = null;
// 利用PdfBox生成圖像
PDDocument pdDocument = document;
PDFRenderer renderer = new PDFRenderer(pdDocument);
/*根據總頁數, 按照50頁生成一張長圖片的邏輯, 進行拆分*/
// 每50頁轉成1張圖片
int pageLength = size; //有多少轉多少
// 總計循環的次數
int totalCount = pdDocument.getNumberOfPages() / pageLength + 1;
for (int m = 0; m < totalCount; m++) {
for (int i = 0; i < pageLength; i++) {
int pageIndex = i + (m * pageLength);
if (pageIndex == pdDocument.getNumberOfPages()) {
System.out.println("m = " + m);
break;
}
// 96為圖片的dpi,dpi越大,則圖片越清晰,圖片越大,轉換耗費的時間也越多
BufferedImage image = renderer.renderImageWithDPI(pageIndex, 106, ImageType.RGB);
int imageHeight = image.getHeight();
int imageWidth = image.getWidth();
if (i == 0) {
//計算高度和偏移量
//使用第一張圖片寬度;
width = imageWidth;
// 保存每頁圖片的像素值
// 加個判斷:如果m次循環後所剩的圖片總數小於pageLength,則圖片高度按剩餘的張數繪制,否則會出現長圖片下面全是黑色的情況
if ((pdDocument.getNumberOfPages() - m * pageLength) < pageLength) {
imageResult = new BufferedImage(width, imageHeight * (pdDocument.getNumberOfPages() - m * pageLength), BufferedImage.TYPE_INT_RGB);
} else {
imageResult = new BufferedImage(width, imageHeight * pageLength, BufferedImage.TYPE_INT_RGB);
}
} else {
// 將高度不斷累加
shiftHeight += imageHeight;
}
singleImgRGB = image.getRGB(0, 0, width, imageHeight, null, 0, width);
imageResult.setRGB(0, shiftHeight, width, imageHeight, singleImgRGB, 0, width);
}
// System.out.println("m = " + m);
File outFile = new File("C://Test//eClaimPDF//WrittenConfirmation.png");
System.out.println(outFile.getName());
// 寫圖片
ImageIO.write(imageResult, "png", outFile);
// 這個很重要,下面會有說明
shiftHeight = 0;
}
pdDocument.close();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(imageResult, "png", baos);//寫入流中
byte[] jpg_Bytes = baos.toByteArray();//轉換成位元組
BASE64Encoder encoder = new BASE64Encoder();
jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//轉換成base64串
jpg_base64 = jpg_base64.replaceAll(" ", "").replaceAll(" ", "");//刪除
// System.out.println("值為:"+"data:image/jpg;base64,"+jpg_base64);
} catch (Exception e) {
e.printStackTrace();
}
return "data:image/jpg;base64,"+jpg_base64;
}
// private static String base64PdfToJpg(String base64Pdf) {
// String jpg_base64 = null;
// int pdfdpi = 400;
//
// BASE64Decoder decoder = new BASE64Decoder();
// byte[] pdf_bytes = null;
// try {
// pdf_bytes = decoder.decodeBuffer(base64Pdf);
// } catch (IOException e1) {
// e1.printStackTrace();
// }
//
// try (final PDDocument document = PDDocument.load(pdf_bytes)) {
// int size = document.getNumberOfPages();
// for (int i = 0; i < size; i++) {
// BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);
// BufferedImage image = new PDFRenderer(document).
//
// ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
// ImageIO.write(image, "jpg", baos);//寫入流中
// byte[] jpg_Bytes = baos.toByteArray();//轉換成位元組
// BASE64Encoder encoder = new BASE64Encoder();
// jpg_base64 = encoder.encodeBuffer(jpg_Bytes).trim();//轉換成base64串
// jpg_base64 = jpg_base64.replaceAll(" ", "").replaceAll(" ", "");//刪除
//
// System.out.println("值為:"+"data:image/jpg;base64,"+jpg_base64);
//
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// return "data:image/jpg;base64,"+jpg_base64;
// }
private static void pdfTojpg(String pdfFilePath, String jpgFilePath) {
File pdfFile = new File(pdfFilePath);
int idx = jpgFilePath.lastIndexOf('.');
String jpgprefix = StringUtils.substring(jpgFilePath, 0, idx);
int pdfdpi = 400;
BASE64Decoder decoder = new BASE64Decoder();
byte[] bytes = null;
try {
bytes = decoder.decodeBuffer(pdfBase64);
} catch (IOException e1) {
e1.printStackTrace();
}
// try (final PDDocument document = PDDocument.load(pdfFile, "")) {
try (final PDDocument document = PDDocument.load(bytes)) {
int size = document.getNumberOfPages();
for (int i = 0; i < size; i++) {
BufferedImage image = new PDFRenderer(document).renderImageWithDPI(i, pdfdpi, ImageType.RGB);
/*
* ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
* ImageIO.write(image, "jpg", baos);//寫入流中 byte[] imgBytes =
* baos.toByteArray();//轉換成位元組 BASE64Encoder encoder = new BASE64Encoder();
* String png_base64 = encoder.encodeBuffer(imgBytes).trim();//轉換成base64串
* png_base64 = png_base64.replaceAll(" ", "").replaceAll(" ", "");//刪除
*
* System.out.println("值為:"+"data:image/jpg;base64,"+png_base64);
*/
File jpgFile = new File(jpgprefix + "_" + i + ".jpg");
ImageIO.write(image, "jpg", jpgFile);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static Double getDiffTime(Date lateTime, Date earlyTime) {
DecimalFormat df=new DecimalFormat("0.00");//設置保留位數
return Double.valueOf(df.format((double)(lateTime.getTime() - earlyTime.getTime()) / 1000));
}
public static String pdfBase64 = "***" //from web網頁鏈接, upload a PDF to get the base64 string (Base64 - Online Base64 decoder and encoder)
}
㈡ 求個將圖片轉成pdf文檔的java程序,最好有註解,我用pdfbox實現這一要求時,圖片不知道為什麼沒了
給你一個用IText寫的吧
// 寫PDF文件.
BufferedImage img = ImageIO.read(new File(imgPath));
FileOutputStream fos = new FileOutputStream(pdfFile);
// 創建PDF文檔
Document doc = new Document(null, 0, 0, 0, 0);
// 設置尺寸為圖片尺寸
doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
Image image = Image.getInstance(imgPath);
PdfWriter.getInstance(doc, fos);
doc.open();
doc.add(image);
doc.close();
㈢ Java PDFBox 能在PDF上添加標簽嗎
應該是能加東西的,PDFBox 提供了下面的功能:
(1)文本的提取
即在PDF文件裡面把文本內容提取出來。
(2) 合並和拆分PDF文檔
可以把幾個PDF合並成一個PDF文件,也可以把一個PDF文件拆分成多個PDF文件。
(3) 表單填充
從PDF文件的表單中提取數據或者寫入數據到PDF表單中。
(4) PDF/A的校驗
根據PDF/A ISO 標准校驗PDF文檔
(5)列印PDF
通過Java的API去列印PDF文件
(6) 把PDF轉換成圖片
把PDF文件轉換成圖片
(7) 創建PDF文件
(8) PDF簽名