java generate pdf

iText-2.1.7.jar

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;
/**
* 描述:TODO 【JAVA生成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 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 new document object
  // The first parameter is the page size. The next parameters are the left, right, top, and bottom margins, respectively.
  Document document = new Document(PageSize.A4, 50, 50, 50, 50);

  // 2. Establish a writer (Writer) associated with the document object, through which the document (Writer) can be written to the disk.
  // Create a PdfWriter object. The first parameter is a reference to the document object, and the second parameter is the actual name of the file, where 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
  // via com. lowagie.text.Paragraph to add text. A default paragraph can be created 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 file with 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 the left, right, top, and bottom margins, respectively.
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

  // Establish a writer (Writer) associated with the document object, through which the document (Writer) can be written to the disk.
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));

  // open 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();
  // 向文档中添加内容
  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)));

  // 新建章节
  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 the document
  document.close();
}

}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326929515&siteId=291194637