To create PDF file we need iText 5 jar. Download iText Jars from iText Website or Maven Repository
CustomDashedLine.java
Output
Reference : iText Website
Maven Dependency
com.itextpdf itextpdf 5.5.11
CustomDashedLine.java
01.
package
com.javatutorialcorner.itextpdf;
02.
03.
import
com.itextpdf.text.Chunk;
04.
import
com.itextpdf.text.Document;
05.
import
com.itextpdf.text.DocumentException;
06.
import
com.itextpdf.text.Paragraph;
07.
import
com.itextpdf.text.pdf.PdfContentByte;
08.
import
com.itextpdf.text.pdf.PdfWriter;
09.
import
com.itextpdf.text.pdf.draw.DottedLineSeparator;
10.
11.
12.
import
java.io.File;
13.
import
java.io.FileOutputStream;
14.
import
java.io.IOException;
15.
16.
public
class
CustomDashedLine {
17.
18.
class
CustomDashedLineSeparator
extends
DottedLineSeparator {
19.
protected
float
dash =
5
;
20.
protected
float
phase =
2
.5f;
21.
22.
public
float
getDash() {
23.
return
dash;
24.
}
25.
26.
public
float
getPhase() {
27.
return
phase;
28.
}
29.
30.
public
void
setDash(
float
dash) {
31.
this
.dash = dash;
32.
}
33.
34.
public
void
setPhase(
float
phase) {
35.
this
.phase = phase;
36.
}
37.
38.
public
void
draw(PdfContentByte canvas,
float
llx,
float
lly,
float
urx,
float
ury,
float
y) {
39.
canvas.saveState();
40.
canvas.setLineWidth(lineWidth);
41.
canvas.setLineDash(dash, gap, phase);
42.
drawLine(canvas, llx, urx, y);
43.
canvas.restoreState();
44.
}
45.
}
46.
47.
public
static
final
String DEST =
"C:/JTC/CustomDashedLine.pdf"
;
48.
49.
public
static
void
main(String[] args)
throws
IOException, DocumentException {
50.
File file =
new
File(DEST);
51.
file.getParentFile().mkdirs();
52.
new
CustomDashedLine().createPdf(DEST);
53.
}
54.
55.
public
void
createPdf(String dest)
throws
IOException, DocumentException {
56.
Document document =
new
Document();
57.
PdfWriter.getInstance(document,
new
FileOutputStream(dest));
58.
document.open();
59.
document.add(
new
Paragraph(
"Before dashed line"
));
60.
CustomDashedLineSeparator separator =
new
CustomDashedLineSeparator();
61.
separator.setDash(
10
);
62.
separator.setGap(
7
);
63.
separator.setLineWidth(
3
);
64.
Chunk linebreak =
new
Chunk(separator);
65.
document.add(linebreak);
66.
document.add(
new
Paragraph(
"After dashed line"
));
67.
document.close();
68.
}
69.
}
Output
Reference : iText Website
0 comments:
Post a Comment