Skip to content

Instantly share code, notes, and snippets.

@milindjagre
Last active April 13, 2016 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milindjagre/0065368747a86aa9e7fe688dd2bab60d to your computer and use it in GitHub Desktop.
Save milindjagre/0065368747a86aa9e7fe688dd2bab60d to your computer and use it in GitHub Desktop.
This standalone java code will enable us to write PDF files using JAVA API
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.milind.word.to.pdf;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.List;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*
* @author milind
*/
public class WritePdf {
public static void main(String[] args) throws DocumentException, BadElementException, IOException {
try {
File file = new File("D:\\mypdf.pdf");
FileOutputStream fileout = new FileOutputStream(file);
Document document = new Document();
PdfWriter.getInstance(document, fileout);
document.addAuthor("Milind");
document.addTitle("My PDF File");
document.open();
Chunk chunk = new Chunk("Milind Jagre");
Font font = new Font();
font.setStyle(Font.UNDERLINE);
font.setStyle(Font.ITALIC);
chunk.setFont(font);
chunk.setBackground(BaseColor.CYAN);
document.add(chunk);
Paragraph paragraph = new Paragraph();
paragraph.add("Hello World");
paragraph.setAlignment(Element.ALIGN_CENTER);
document.add(paragraph);
List list = new List(true, 15);
list.add("Bangalore");
list.add("Nagpur");
document.add(list);
document.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment