Java generated instance code for PDF file _java

Source: Internet
Author: User

Copy Code code 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.pdf.PdfWriter;

/**
* Description: TODO "Java build 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 method and generate a file named Itexttest.pdf to the C disk
try {
Writesimplepdf ();
}
catch (Exception e) {e.printstacktrace ();}


Call the second method, and add the chapter to the file named Itexttest.pdf on C disk.
try {
Writecharpter ();
}
catch (Exception e) {e.printstacktrace ();}


}

public static void Writesimplepdf () throws Exception {

1. New Document Object
The first parameter is the page size. The next parameters are 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, which can be written to disk by the writer (Writer).
Creating a PDFWriter Object The first parameter is a reference to the Document object, the second parameter is the actual name of the file, and its output path is also given in that name.
PDFWriter writer = pdfwriter.getinstance (document, New FileOutputStream ("C:\\itexttest.pdf"));

3. Open the Document
Document.open ();

4. Add content to the document
Add text by Com.lowagie.text.Paragraph. You can use text and its default font, color, size, and so on to create a default paragraph
Document.add (New Paragraph ("the document.");
Document.add (The New Paragraph ("Some more text on the" the "the" and "different color and font type.", Fontfactory.getfont (Fo Ntfactory.courier, 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 new Document object is the page size. The next parameters are left, right, top, and bottom margins.
Document document = new Document (PAGESIZE.A4, 20, 20, 20, 20);

Establishes a writer (Writer) that is associated with the document object and can write documents to 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 the 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 ("the document.");
Document.add (New Paragraph ("the document.");
Document.add (New Paragraph ("the document.");
Document.add (New Paragraph ("the document.");
Document.add (The New Paragraph ("Some more text on the" the "the" and "different color and font type.", Fontfactory.getfont (Fon Tfactory.defaultencoding, Font.Bold, New Color (0, 0, 0));
Paragraph title1 = new Paragraph ("Chapter 1", Fontfactory.getfont (Fontfactory.helvetica, Font.bolditalic, New Color ( 0, 0, 255));

New chapter
Chapter chapter1 = new Chapter (title1, 1);
Chapter1.setnumberdepth (0);
Paragraph title11 = new Paragraph ("This are section 1 in Chapter 1", Fontfactory.getfont (Fontfactory.helvetica., Font.bo LD, 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 ();
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.