Archives: PDF

Java iText – Add Anchor / Link to PDF

[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]


Java iText – Add Image to PDF

[code lang=”java”]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();
}
}
}[/code]


Java iText – Add Table to PDF

[code lang=”java”]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){

}
}
}[/code]

Table Width
[code lang=”java”]table.setWidthPercentage(100);[/code]

Spacing Before and After Table
[code lang=”java”]table.setSpacingBefore(10f);
table.setSpacingAfter(10f);[/code]

Column Span
[code lang=”java”]cell.setColspan(2);[/code]

Cell Alignment
[code lang=”java”]cell.setHorizontalAlignment(Element.ALIGN_LEFT);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);[/code]

Cell Padding
[code lang=”java”]cell.setPadding(5);

cell.setPaddingLeft(8);
cell.setPaddingRight(8);
cell.setPaddingTop(8);
cell.setPaddingBottom(8);[/code]