import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.Chapter;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.FontFactory;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;
import com.lowagie.text.pdf.PdfWriter;
/**
* Description: TODO [JAVA generate PDF]
* <p>
*
* @title GeneratePDF
* @author SYJ
* @email [email protected]
* @date 2013-4-6
* @version V1.0
*/
public class GeneratePDF {
public static void main(String[] args) {
//Call the first method to generate a file named ITextTest.pdf to the C drive
try {
writeSimplePdf();
}
catch (Exception e) { e.printStackTrace(); }
//Call the second method to add chapters to the file named ITextTest.pdf on drive C.
try {
writeCharpter();
}
catch (Exception e) { e.printStackTrace(); }
}
public static void writeSimplePdf() throws Exception {
// 1. Create a new document object
// The first parameter is the page size. The next parameters are the left, right, top and bottom margins.
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
// 2. Establish a writer (Writer) associated with the document object. The document can be written to the disk through the writer (Writer).
//The first parameter of creating a PdfWriter object is a reference to the document object, and the second parameter is the actual name of the file, in which its output path is also given.
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C://ITextTest.pdf"));
// 3. Open the document
document.open();
// 4. Add content to the document
// Add text via com.lowagie.text.Paragraph. You can create a default paragraph with text and its default font, color, size, etc. settings
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200)))) ;
// 5. Close the document
document.close();
}
/**
* Add pdf files containing chapters
*
* @throwsException
*/
public static void writeCharpter() throws Exception {
//The first parameter of the new document object is the page size. The next parameters are the left, right, top and bottom margins.
Document document = new Document(PageSize.A4, 20, 20, 20, 20);
// Establish a writer (Writer) associated with the document object. The document can be written to the disk through the writer (Writer).
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c://ITextTest.pdf"));
//Open file
document.open();
// title
document.addTitle("Hello mingri example");
// author
document.addAuthor("wolf");
// theme
document.addSubject("This example explains how to add metadata.");
document.addKeywords("iText, Hello mingri");
document.addCreator("My program using iText");
// document.newPage();
//Add content to the document
document.add(new Paragraph("/n"));
document.add(new Paragraph("/n"));
document.add(new Paragraph("/n"));
document.add(new Paragraph("/n"));
document.add(new Paragraph("/n"));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("First page of the document."));
document.add(new Paragraph("Some more text on the first page with different color and font type.", FontFactory.getFont(FontFactory.defaultEncoding, 10, Font.BOLD, new Color(0, 0, 0)))) ;
Paragraph title1 = new Paragraph("Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
//Create a new chapter
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1", FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This text comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
document.add(chapter1);
//Close document
document.close();
}
}