㈠ 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签名