Sunday, 12 November 2017

Hibernate one to one mapping usnig boot

Below sample code for hibernate one to one mapping:

https://github.com/Sushil21/HibernateOneToOneMapping


I have used lib lombok also in this project so must integrate lombok in Eclipse/STS:

https://projectlombok.org/setup/eclipse


Saturday, 11 November 2017

Create Spring Boot Sample Project

You can download a running  sample of spring boot on below link:

https://github.com/Sushil21/SpringBootSample

Add Image Water Mark on Image in JAVA

Below code for add image water mark on image in java:


import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

public class TestImage41im {

public static void main(String[] args) {
try {
File sourceImageFile = new File("originalImagePath/originalimage.jpg");
File destImageFile = new File("destinationImagePath/destinationImage.png");
File watermarkImageFile = new File("waterMarkImagePath/watermark.png");

BufferedImage sourceImage = ImageIO.read(sourceImageFile);
BufferedImage watermarkImage = ImageIO.read(watermarkImageFile);

// graphic properties
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); //add transparency of watermark image 
g2d.setComposite(alphaChannel);

//coordinate where the image is painted below are the center
int x = (sourceImage.getWidth() - watermarkImage.getWidth()) / 2;
int y = (sourceImage.getHeight() - watermarkImage.getHeight()) / 2;

// draw the image watermark
g2d.drawImage(watermarkImage, x, y, null);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The image watermark is added to the image.");
}catch (Exception e) {
// TODO: handle exception
}
}

}

Add Text Water mark on Image in JAVA

Below code for add text water mark on image in JAVA:

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;

import com.itextpdf.awt.geom.Dimension;



public class TextWaterMarkOnImage {

public static void main(String[] args) {
try {
File sourceImageFile = new File("originalFilePath/original.jpg");
File destImageFile = new File("watermarkedFilePath/watermarked.png");
BufferedImage sourceImage = ImageIO.read(sourceImageFile);
Graphics2D g2d = (Graphics2D) sourceImage.getGraphics();
AlphaComposite alphaChannel = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); //set transparency of watermark
g2d.setComposite(alphaChannel);
g2d.setColor(Color.BLACK);
Font font = new Font("Arial", Font.BOLD, 64); //font of text
g2d.setFont(font);
String text = "Checked"; //watermark text
Dimension dmDimension = getDimentionOfText(g2d, font, text);
int centerX  = sourceImage.getWidth()/2 - (int)dmDimension.getWidth()/2;
int centerY = sourceImage.getHeight()/2 + (int)dmDimension.getHeight()/2;
// paints the textual watermark
g2d.drawString(text, centerX, centerY);
ImageIO.write(sourceImage, "png", destImageFile);
g2d.dispose();
System.out.println("The text watermark is added to the image.");
}catch (Exception e) {
// TODO: handle exception
}
}

public static Dimension getDimentionOfText(Graphics2D g2d, Font font , String text) {
// get metrics from the graphics
FontMetrics metrics = g2d.getFontMetrics(font);
// get the height of a line of text in this
// font and render context
int hgt = metrics.getHeight();
// get the advance of my text in this font
// and render context
int adv = metrics.stringWidth(text);
// calculate the size of a box to hold the
// text with some padding.
Dimension size = new Dimension(adv+2, hgt+2);
System.out.println(size.getHeight() +"  "+ size.getWidth());
return size;
}

}

Add image water mark on PDF in JAVA using iText

Below code for add image water mark on PDF using iText:

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class ImageWaterMarkOnPdf {
public static void main(String... args)   {

try {
PdfReader reader = new PdfReader("originalfilepath/otiginal.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("outputfilepath/output.pdf"));

// image watermark
Image img = Image.getInstance("watermarkfilepath/watermark.png");
float w = img.getScaledWidth();
float h = img.getScaledHeight();

// properties
PdfContentByte over;
Rectangle pagesize;
float x, y;

// add water mark on all pages
int n = reader.getNumberOfPages();
for (int i = 1; i <= n; i++) {

// get page size and position for add water mark
pagesize = reader.getPageSizeWithRotation(i);
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
over = stamper.getOverContent(i);
over.saveState();

// set transparency
PdfGState state = new PdfGState();
state.setFillOpacity(0.2f); //more transparent
over.setGState(state);

// add image water mark center
over.addImage(img, w, 0, 0, h, x - (w / 2), y - (h / 2));
over.restoreState();
}
stamper.close();
reader.close();
}catch (IOException e) {
// TODO: handle exception
}catch (DocumentException e) {
// TODO: handle exception
}
 
}


}

Get Bytes from ByteArrayOutputStream

Below code for get byte array from ByteArrayOutputStream:


byte[] waterMarkedBytes = baos.toByteArray();

PdfStamper from bytes

Below code of create PdfStamper object from Bytes in JAVA:

PdfReader reader = new PdfReader(bytes);
ByteArrayOutputStream baos = new ByteArrayOutputStream();

PdfStamper stamper = new PdfStamper(reader, baos);

How to read secured PDF using iText JAVA


Below code for read secured (encrypted using a public certificate ) PDF using iText:

PdfReader.unethicalreading = true;

Add Text Water Mark on PDF JAVA using iText

Sample Code:

Running code for add text water mark on PDF in java using iText , the water mark drawing on center horizontally.

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class TextWaterMarkOnPDF {
public static void main(String args[]) {
try {
PdfReader reader = new PdfReader("originalFilePath/file.pdf");
PdfStamper stamper = new PdfStamper(reader,
new FileOutputStream("outputPath/watermarked.pdf"));

// text watermark

Font FONT = new Font(Font.FontFamily.HELVETICA, 50, Font.BOLDnew             GrayColor(0.5f));
Phrase p = new Phrase("Checked"FONT);

// properties
PdfContentByte over;
Rectangle pagesize;
float xy;

// add water on every page
int n = reader.getNumberOfPages();
for (int i = 1; i <= ni++) {

// get page size and position
pagesize = reader.getPageSizeWithRotation(i);
x = (pagesize.getLeft() + pagesize.getRight()) / 2;
y = (pagesize.getTop() + pagesize.getBottom()) / 2;
over = stamper.getOverContent(i);
over.saveState();

// set transparency of watermark : if opacity less then it more transparent
PdfGState state = new PdfGState();
state.setFillOpacity(0.2f);
over.setGState(state);

// add watermark text in center horizontal

ColumnText.showTextAligned(over, Element.ALIGN_CENTERpxy, 0);
over.restoreState();
}
stamper.close();
reader.close();

}catch (IOException e) {
// TODO: handle exception
}catch (DocumentException e) {
// TODO: handle exception
}

}}

Add watermark diagonally:
ColumnText.showTextAligned(over, Element.ALIGN_CENTERpxy, 45);



links for Data Structure

  1) 𝐁𝐞𝐜𝐨𝐦𝐞 𝐌𝐚𝐬𝐭𝐞𝐫 𝐢𝐧 𝐋𝐢𝐧𝐤𝐞𝐝 𝐋𝐢𝐬𝐭:  https://lnkd.in/gXQux4zj 2) 𝐀𝐥𝐥 𝐭𝐲𝐩𝐞𝐬 𝐨𝐟 𝐓𝐫𝐞𝐞 𝐓𝐫𝐚𝐯𝐞𝐫𝐬𝐚𝐥𝐬...