Copy codeThe Code is as follows: package com.qhdstar.java.pdf;
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=. writable writer;
/**
* Description: TODO [JAVA-generated PDF]
* <P>
*
* @ Title GeneratePDF
* @ Author SYJ
* @ Email songyanjun_stars@126.com
* @ Date 2013-4-6
* @ Version V1.0
*/
Public class GeneratePDF {
Public static void main (String [] args ){
// Call the first token to generate a file named itexttest.pdf to the c drive.
Try {
WriteSimplePdf ();
}
Catch (Exception e) {e. printStackTrace ();}
// Call the second example and add a chapter to the file named itexttest.pdf on the C drive.
Try {
WriteCharpter ();
}
Catch (Exception e) {e. printStackTrace ();}
}
Public static void writeSimplePdf () throws Exception {
// 1. Create a document Object
// The first parameter is the page size. The following parameters are left, right, top, and bottom margins.
Document document = new Document (PageSize. A4, 50, 50, 50, 50 );
// 2. Create a Writer to associate with the document object. The Writer can write the document to the disk.
// Create an external writer object. The first parameter is a reference to the document object, and the second parameter is the actual name of the file. The output path is also provided in the name.
Using writer = Using writer. getInstance (document, new FileOutputStream ("C :\\ ITextTest.pdf "));
// 3. Open the document
Document. open ();
// 4. add content to the document
// Use com. lowagie. text. Paragraph to add text. You can use text and its default font, color, size, and other settings to create a default section.
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
*
* @ Throws Exception
*/
Public static void writeCharpter () throws Exception {
// The first parameter of the newly created document object is the page size. The following parameters are left, right, top, and bottom margins.
Document document = new Document (PageSize. A4, 20, 20, 20, 20 );
// Create a Writer to associate with the document object. The Writer can write the document to the disk.
Using writer = Using writer. getInstance (document, new FileOutputStream ("c :\\ ITextTest.pdf "));
// Open the file
Document. open ();
// Title
Document. addTitle ("Hello mingri example ");
// Author
Document. addAuthor ("wolf ");
// Subject
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 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 3X2 table .");
Section1.add (someSectionText );
Document. add (chapter1 );
// Close the document
Document. close ();
}
}