October 25, 2010
|
By Prasanna Sherekar |
[code lang=”java”]import com.itextpdf.text.Anchor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class AnchorDemo {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("AnchorDemo.pdf"));
document.open();
String content = "You can learn Java programming on the " + "following website: ";
Paragraph paragraph = new Paragraph(content);
//
// Creates a new anchor that link to external website
// and add this anchor to the paragraph.
//
Anchor anchor = new Anchor("javac – Java Examples Source Code");
anchor.setReference("http://javac.in");
paragraph.add(anchor);
document.add(paragraph);
} catch (DocumentException | FileNotFoundException e) {
e.printStackTrace();
} finally {
document.close();
}
}
}[/code]
April 30, 2010
|
By Prasanna Sherekar |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import com . itextpdf . text . Document ;
import com . itextpdf . text . Image ;
import com . itextpdf . text . pdf . PdfWriter ;
import java . io . FileOutputStream ;
public class ImageExample {
public static void main ( String [ ] args ) {
Document document = new Document ( ) ;
try {
PdfWriter . getInstance ( document , new FileOutputStream ( "Image.pdf" ) ) ;
document . open ( ) ;
Image image1 = Image . getInstance ( "watermark.png" ) ;
document . add ( image1 ) ;
String imageUrl = "http://javac.in/wp-content/themes/Whitey/images/javac.jpg" ;
Image image2 = Image . getInstance ( new URL ( imageUrl ) ) ;
document . add ( image2 ) ;
document . close ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
April 25, 2010
|
By Prasanna Sherekar |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import com . itextpdf . text . Document ;
import com . itextpdf . text . Paragraph ;
import com . itextpdf . text . pdf . PdfPCell ;
import com . itextpdf . text . pdf . PdfPTable ;
import com . itextpdf . text . pdf . PdfWriter ;
import java . io . FileOutputStream ;
public class TableExample {
public static void main ( String [ ] args ) {
Document document = new Document ( ) ;
try {
PdfWriter . getInstance ( document , new FileOutputStream ( "HelloWorld-Table.pdf" ) ) ;
document . open ( ) ;
PdfPTable table = new PdfPTable ( 3 ) ; // 3 columns.
PdfPCell cell1 = new PdfPCell ( new Paragraph ( "Cell 1" ) ) ;
PdfPCell cell2 = new PdfPCell ( new Paragraph ( "Cell 2" ) ) ;
PdfPCell cell3 = new PdfPCell ( new Paragraph ( "Cell 3" ) ) ;
table . addCell ( cell1 ) ;
table . addCell ( cell2 ) ;
table . addCell ( cell3 ) ;
document . add ( table ) ;
document . close ( ) ;
} catch ( Exception e ) {
}
}
}
Table Width
table . setWidthPercentage ( 100 ) ;
Spacing Before and After Table
table . setSpacingBefore ( 10f ) ;
table . setSpacingAfter ( 10f ) ;
Column Span
Cell Alignment
cell . setHorizontalAlignment ( Element . ALIGN_LEFT ) ;
cell . setVerticalAlignment ( Element . ALIGN_MIDDLE ) ;
Cell Padding
cell . setPadding ( 5 ) ;
cell . setPaddingLeft ( 8 ) ;
cell . setPaddingRight ( 8 ) ;
cell . setPaddingTop ( 8 ) ;
cell . setPaddingBottom ( 8 ) ;