To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository
PositionContentInCell.java
Output
Reference : iText Website
Maven Dependency
2.
<
dependency
>
3.
<
groupid
>com.itextpdf</
groupid
>
4.
<
artifactid
>itextpdf</
artifactid
>
5.
<
version
>5.5.11</
version
>
6.
</
dependency
>
PositionContentInCell.java
001.
package
com.javatutorialcorner.itextpdf;
002.
003.
import
com.itextpdf.text.Document;
004.
import
com.itextpdf.text.DocumentException;
005.
import
com.itextpdf.text.Element;
006.
import
com.itextpdf.text.Image;
007.
import
com.itextpdf.text.Phrase;
008.
import
com.itextpdf.text.Rectangle;
009.
import
com.itextpdf.text.pdf.ColumnText;
010.
import
com.itextpdf.text.pdf.PdfContentByte;
011.
import
com.itextpdf.text.pdf.PdfPCell;
012.
import
com.itextpdf.text.pdf.PdfPCellEvent;
013.
import
com.itextpdf.text.pdf.PdfPTable;
014.
import
com.itextpdf.text.pdf.PdfWriter;
015.
016.
017.
import
java.io.File;
018.
import
java.io.FileOutputStream;
019.
import
java.io.IOException;
020.
021.
public
class
PositionContentInCell {
022.
public
static
final
String DEST =
"C:/JTC/PositionContentInCell.pdf"
;
023.
public
static
final
String IMG =
"C:/JTC/info.png"
;
024.
public
enum
POSITION { TOP_LEFT, TOP_RIGHT, BOTTOM_LEFT, BOTTOM_RIGHT };
025.
026.
class
ImageEvent
implements
PdfPCellEvent {
027.
protected
Image img;
028.
public
ImageEvent(Image img) {
029.
this
.img = img;
030.
}
031.
public
void
cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
032.
img.scaleToFit(position.getWidth(), position.getHeight());
033.
img.setAbsolutePosition(position.getLeft() + (position.getWidth() - img.getScaledWidth()) /
2
,
034.
position.getBottom() + (position.getHeight() - img.getScaledHeight()) /
2
);
035.
PdfContentByte canvas = canvases[PdfPTable.BACKGROUNDCANVAS];
036.
try
{
037.
canvas.addImage(img);
038.
}
catch
(DocumentException ex) {
039.
// do nothing
040.
}
041.
}
042.
}
043.
044.
class
PositionEvent
implements
PdfPCellEvent {
045.
protected
Phrase content;
046.
protected
POSITION pos;
047.
048.
public
PositionEvent(Phrase content, POSITION pos) {
049.
this
.content = content;
050.
this
.pos = pos;
051.
}
052.
053.
public
void
cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
054.
PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
055.
float
x =
0
;
056.
float
y =
0
;
057.
int
alignment =
0
;
058.
switch
(pos) {
059.
case
TOP_LEFT:
060.
x = position.getLeft(
3
);
061.
y = position.getTop(content.getLeading());
062.
alignment = Element.ALIGN_LEFT;
063.
break
;
064.
case
TOP_RIGHT:
065.
x = position.getRight(
3
);
066.
y = position.getTop(content.getLeading());
067.
alignment = Element.ALIGN_RIGHT;
068.
break
;
069.
case
BOTTOM_LEFT:
070.
x = position.getLeft(
3
);
071.
y = position.getBottom(
3
);
072.
alignment = Element.ALIGN_LEFT;
073.
break
;
074.
case
BOTTOM_RIGHT:
075.
x = position.getRight(
3
);
076.
y = position.getBottom(
3
);
077.
alignment = Element.ALIGN_RIGHT;
078.
break
;
079.
}
080.
ColumnText.showTextAligned(canvas, alignment, content, x, y,
0
);
081.
}
082.
}
083.
084.
public
static
void
main(String[] args)
throws
IOException, DocumentException {
085.
File file =
new
File(DEST);
086.
file.getParentFile().mkdirs();
087.
new
PositionContentInCell().createPdf(DEST);
088.
}
089.
090.
public
void
createPdf(String dest)
throws
IOException, DocumentException {
091.
// 1. Create a Document which contains a table:
092.
Document document =
new
Document();
093.
PdfWriter.getInstance(document,
new
FileOutputStream(dest));
094.
document.open();
095.
PdfPTable table =
new
PdfPTable(
2
);
096.
PdfPCell cell1 =
new
PdfPCell();
097.
PdfPCell cell2 =
new
PdfPCell();
098.
PdfPCell cell3 =
new
PdfPCell();
099.
PdfPCell cell4 =
new
PdfPCell();
100.
// 2. Inside that table, make each cell with specific height:
101.
cell1.setFixedHeight(
50
);
102.
cell2.setFixedHeight(
50
);
103.
cell3.setFixedHeight(
50
);
104.
cell4.setFixedHeight(
50
);
105.
// 3. Each cell has the same background image
106.
ImageEvent imgEvent =
new
ImageEvent(Image.getInstance(IMG));
107.
cell1.setCellEvent(imgEvent);
108.
cell2.setCellEvent(imgEvent);
109.
cell3.setCellEvent(imgEvent);
110.
cell4.setCellEvent(imgEvent);
111.
// 4. Add text in front of the image at specific position
112.
cell1.setCellEvent(
new
PositionEvent(
new
Phrase(
"Top left"
), POSITION.TOP_LEFT));
113.
cell2.setCellEvent(
new
PositionEvent(
new
Phrase(
"Top right"
), POSITION.TOP_RIGHT));
114.
cell3.setCellEvent(
new
PositionEvent(
new
Phrase(
"Bottom left"
), POSITION.BOTTOM_LEFT));
115.
cell4.setCellEvent(
new
PositionEvent(
new
Phrase(
"Bottom right"
), POSITION.BOTTOM_RIGHT));
116.
// Wrap it all up!
117.
table.addCell(cell1);
118.
table.addCell(cell2);
119.
table.addCell(cell3);
120.
table.addCell(cell4);
121.
document.add(table);
122.
document.close();
123.
}
124.
}
Output
Reference : iText Website
0 comments:
Post a Comment