signature=856b33220dd5780f46999a8510e32386,java - Visual signature with pdfbox 1.8.8 - Stack Overflo...

Display the text over the signatory some time it doesn't look nice. For me, I create new image base on the text that I want to display. And merge it with the signatory image. I can add background for signatory image (company name watermark)

Here is the code create new signatory image with text and background:

public class ImageSignatory {

public static void main(String[] args) {

DateFormat df = new SimpleDateFormat("MM.dd.yyyy");

Date today = Calendar.getInstance().getTime();

String reportDate = df.format(today);

String text = "Signature eletronic Company AA DUC NGUYEN - Date " + reportDate;

String background = "background.png";

String signImage = "sign.jpg";

String textImage = createImageFromText(text);

String imageResultURL = null ;

try {

String mergedImage = mergeTwoImage(signImage, textImage);

imageResultURL = addBackgroundToImage(background, mergedImage);

} catch (Exception ex) {

}

}

public static String StringDivider(String s) {

StringBuilder sb = new StringBuilder(s);

int i = 0;

while ((i = sb.indexOf(" ", i + 30)) != -1) {

sb.replace(i, i + 1, "\n");

}

return sb.toString();

}

public static BufferedImage toBufferedImage(Image img) {

if (img instanceof BufferedImage) {

return (BufferedImage) img;

}

BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);

Graphics2D bGr = bimage.createGraphics();

bGr.drawImage(img, 0, 0, null);

bGr.dispose();

return bimage;

}

public static String addBackgroundToImage(String backgroundPATH, String imagePath) {

try {

String imageResult = "result.jpg";

Image backgroundImage = ImageIO.read(new File(backgroundPATH));

int width = backgroundImage.getWidth(null);

int height = backgroundImage.getHeight(null);

Image signAndText = ImageIO.read(new File(imagePath));

signAndText = signAndText.getScaledInstance(width, height, Image.SCALE_SMOOTH);

signAndText = toBufferedImage(signAndText);

BufferedImage combined = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g2 = combined.getGraphics();

g2.drawImage(backgroundImage, 0, 0, null);

g2.drawImage(signAndText, 0, 0, null);

g2.dispose();

ImageIO.write(combined, "JPG", new File(imageResult));

return imageResult;

} catch (IOException ex) {

return null;

}

}

public static String mergeTwoImage(String first, String second) {

try {

String tempFileName = "merged_image.png";

Image signatoryImage = ImageIO.read(new File(first));

Image addtionalTextImage = ImageIO.read(new File(second));

float ratio = (float) signatoryImage.getWidth(null) / (float) addtionalTextImage.getWidth(null);

addtionalTextImage = addtionalTextImage.getScaledInstance((int) (ratio * (float) addtionalTextImage.getWidth(null)), (int) (ratio * (float) addtionalTextImage.getHeight(null)), Image.SCALE_SMOOTH);

addtionalTextImage = toBufferedImage(addtionalTextImage);

BufferedImage combinedTemp = new BufferedImage(signatoryImage.getWidth(null), signatoryImage.getHeight(null) + (int) (ratio * (float) addtionalTextImage.getHeight(null)), BufferedImage.TYPE_INT_ARGB);

Graphics g = combinedTemp.getGraphics();

g.drawImage(signatoryImage, 0, 0, null);

g.drawImage(addtionalTextImage, 0, signatoryImage.getHeight(null), null);

g.dispose();

ImageIO.write(combinedTemp, "PNG", new File(tempFileName));

return tempFileName;

} catch (IOException ex) {

return null;

}

}

public static String createImageFromText(String text) {

String tempFileName = "text.png";

text = StringDivider(text);

String[] textParts = text.split("\n");

BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = img.createGraphics();

Font font = new Font("Arial", Font.PLAIN, 48);

g2d.setFont(font);

FontMetrics fm = g2d.getFontMetrics();

int width = 0;

for (String textPart : textParts) {

int tempWidth = fm.stringWidth(textPart);

if (tempWidth > width) {

width = tempWidth;

}

}

width += 10;

int oneLineHeight = fm.getHeight();

int height = (oneLineHeight) * textParts.length;

g2d.dispose();

img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

g2d = img.createGraphics();

g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);

g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE);

g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);

g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);

g2d.setFont(font);

fm = g2d.getFontMetrics();

g2d.setColor(Color.BLACK);

int index = 0;

for (String textPart : textParts) {

g2d.drawString(textPart, 5, (oneLineHeight) * index + fm.getAscent());

index++;

}

g2d.dispose();

try {

ImageIO.write(img, "PNG", new File(tempFileName));

} catch (IOException ex) {

return null;

}

return tempFileName;

}

public static void removeFile(String fileName) {

try {

File file = new File(fileName);

file.delete();

} catch (Exception ex) {

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值