Skip to content

Instantly share code, notes, and snippets.

@mehikmat
Created February 14, 2014 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehikmat/9008170 to your computer and use it in GitHub Desktop.
Save mehikmat/9008170 to your computer and use it in GitHub Desktop.
PDF document generation using iText framework in Java
package com.test;
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfOutline;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class FirstPDFDoc
{
private static String FILE="first_pdf_doc.pdf";
public FirstPDFDoc() throws DocumentException, MalformedURLException, IOException
{
Document document=new Document();
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream(FILE));
document.open();
Font font=new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0x92, 0x90, 0x83));
Chunk chunk=new Chunk("Testing Text Element",font);
chunk.setBackground(new Color( 0xff,0xe4,0x00));
Phrase phrase=new Phrase(20, "This is initial text.");
for(int i=0;i<8;i++)
{
phrase.add(chunk);
if(i==4)
phrase.add("\n\n");
}
Paragraph para=new Paragraph(phrase);
para.setAlignment(Element.ALIGN_CENTER);
para.setIndentationLeft(20);
para.setIndentationRight(30);
document.add(para);
float[] colswidht={1f,2f};
PdfPTable table=new PdfPTable(colswidht);
Paragraph para1=new Paragraph("Tesitng span table");
PdfPCell cell=new PdfPCell(para1);
cell.setColspan(2);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidthPercentage(50);
document.add(table);
document.newPage();
document.add(new Chunk("CHapter 0").setLocalDestination("2"));
document.add(new Paragraph(new Chunk("http://www.geek.com").setAnchor("http://www.google.com")));
document.add(table);
document.newPage();
document.add(new Chunk("Chapter 1"));
document.add(new Paragraph(new Chunk("Click here to go chapter 0").setRemoteGoto("hk.pdf", 2)));
document.newPage();
document.add(new Chunk("chapter 1").setLocalDestination("1"));
document.newPage();
document.add(new Chunk("chapter 2").setLocalDestination("2"));
document.newPage();
document.add(new Paragraph(new Chunk("sub 21").setLocalDestination("21")));
document.add(new Paragraph(new Chunk("sub 22").setLocalDestination("22")));
document.newPage();
document.add(new Chunk("chapter 3").setLocalDestination("3"));
PdfContentByte cb=writer.getDirectContent();
PdfOutline root=cb.getRootOutline();
PdfOutline online1=new PdfOutline(root, PdfAction.gotoLocalPage("1", false), "chapter1");
PdfOutline online2=new PdfOutline(root, PdfAction.gotoLocalPage("2", false), "chapter2");
PdfOutline online21=new PdfOutline(root, PdfAction.gotoLocalPage("21", false), "chapter21");
PdfOutline online22=new PdfOutline(online21, PdfAction.gotoLocalPage("22", false), "chapter22");
document.close();
}
public static void main(String args[]) throws DocumentException, MalformedURLException, IOException
{
new FirstPDFDoc();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment