public static void main(String[] args) {
System.out.println("开始");
System.out.println(DateTime.now());
DZZZ_TPExecutor dzzz_tpExecutor = new DZZZ_TPExecutor();
String path = DZZZ_TPExecutor.class.getResource("/").getPath().replaceFirst("/","");
List<String> ftpdfPathpng = new ArrayList<>();
File file = new File(path+"dywqd.pdf");
try {
PDDocument doc = PDDocument.load(file);
PDFRenderer renderer = new PDFRenderer(doc);
int pageCount = doc.getNumberOfPages();
for (int i = 0; i < pageCount; i++) {
BufferedImage image = renderer.renderImageWithDPI(i, 120);
ImageIO.write(image, "png", new File(path+"dywqd"+"_"+(i+1)+"."+"png"));
ftpdfPathpng.add(path+"dywqd"+"_"+(i+1)+"."+"png");
}
String mergrPdfPath = path+"hb.png";
dzzz_tpExecutor.mergePDFFilespng(ftpdfPathpng,mergrPdfPath);
for (int i = 0; i < ftpdfPathpng.size(); i++) {
File file1 = new File(ftpdfPathpng.get(i));
if (file1.exists()) {
file1.delete();
}
}
OutputStream os = null;
try {
Image srcImg = ImageIO.read(new File(mergrPdfPath));
BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null),
srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(
srcImg.getScaledInstance(srcImg.getWidth(null),
srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0,
null);
ImageIcon imgIcon = new ImageIcon(path+"qz.png");
Image img = imgIcon.getImage();
float alpha = 1f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,alpha));
g.drawImage(img, 300, 100, null);
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
g.dispose();
os = new FileOutputStream(path+"hbqz.png");
ImageIO.write(buffImg, "png", os);
System.out.println("图片完成添加水印图片");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != os)
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("结束");
System.out.println(DateTime.now());
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean mergePDFFilespng(List<String> fileList, String mergrPdfPath) {
boolean result = false;
BufferedImage destImage = null;
int allw = 0;int allh = 0;int allwMax = 0;int allhMax = 0;
try {
debugUtil = new DebugUtil(Level.INFO);
debugUtil.begin();
List<BufferedImage> listImg = new ArrayList<>();
for (int i = 0; i < fileList.size(); i++) {
File fileImg = new File(fileList.get(i));
BufferedImage img = ImageIO.read(fileImg);
allw += img.getWidth();
allh += img.getHeight();
if (img.getWidth() > allwMax) {
allwMax = img.getWidth();
}
if (img.getHeight() > allhMax) {
allhMax = img.getHeight();
}
listImg.add(img);
}
if (false) {
destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
} else {
destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
}
int wx = 0, wy = 0;
for (BufferedImage img : listImg) {
int w1 = img.getWidth();
int h1 = img.getHeight();
int[] ImageArrayOne = new int[w1 * h1];
ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1);
if (false) {
destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1);
} else {
destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1);
}
wx += w1;
wy += h1;
}
File outFile = new File(mergrPdfPath);
ImageIO.write(destImage, "jpg", outFile);
debugUtil.end();
debugUtil.writeLog("合并jpg",null);
return true;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
destImage = null;
System.gc();
}
}