java導出pdf格式文檔

// 1.新建document對象
// 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
Document document = new Document(PageSize.A4, 50, 50, 50, 50);字體

// 2.創建一個書寫器(Writer)與document對象關聯,經過書寫器(Writer)能夠將文檔寫入到磁盤中。
// 建立 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:\\ITextTest.pdf"));.net

// 3.打開文檔
document.open();
// 4.向文檔中添加內容
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);

// 經過 com.lowagie.text.Paragraph 來添加文本。能夠用文本及其默認的字體、顏色、大小等等設置來建立一個默認段落3d

document.add(new Paragraph("First page of the document."));
document.add(new Paragraph(value,fontChinese));
// 5.關閉文檔
document.close();htm

 /**
  * 添加含有章節的pdf文件
  */
 public static void writeCharpter() throws Exception {

  // 新建document對象 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

  // 創建一個書寫器(Writer)與document對象關聯,經過書寫器(Writer)能夠將文檔寫入到磁盤中。
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));

  // 打開文件
  document.open();

  // 標題
  document.addTitle("Hello mingri example");

  // 做者
  document.addAuthor("wolf");

  // 主題
  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);

  // 關閉文檔
  document.close();
 }
 對象

摘自:http://www.jb51.net/article/36626.htmblog

相關文章
相關標籤/搜索