麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 編程 > Java > 正文

Java生成PDF文件的實(shí)例代碼

2019-11-26 16:07:14
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

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) {

  //調(diào)用第一個(gè)方法,向C盤(pán)生成一個(gè)名字為ITextTest.pdf 的文件
  try {
   writeSimplePdf();
  }
  catch (Exception e) { e.printStackTrace(); }

  
  //調(diào)用第二個(gè)方法,向C盤(pán)名字為ITextTest.pdf的文件,添加章節(jié)。
  try {
   writeCharpter();
  }
  catch (Exception e) { e.printStackTrace(); }

  
 }
 

 public static void writeSimplePdf() throws Exception {

  // 1.新建document對(duì)象
  // 第一個(gè)參數(shù)是頁(yè)面大小。接下來(lái)的參數(shù)分別是左、右、上和下頁(yè)邊距。
  Document document = new Document(PageSize.A4, 50, 50, 50, 50);

  // 2.建立一個(gè)書(shū)寫(xiě)器(Writer)與document對(duì)象關(guān)聯(lián),通過(guò)書(shū)寫(xiě)器(Writer)可以將文檔寫(xiě)入到磁盤(pán)中。
  // 創(chuàng)建 PdfWriter 對(duì)象 第一個(gè)參數(shù)是對(duì)文檔對(duì)象的引用,第二個(gè)參數(shù)是文件的實(shí)際名稱,在該名稱中還會(huì)給出其輸出路徑。
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C://ITextTest.pdf"));

  // 3.打開(kāi)文檔
  document.open();

  // 4.向文檔中添加內(nèi)容
  // 通過(guò) com.lowagie.text.Paragraph 來(lái)添加文本。可以用文本及其默認(rèn)的字體、顏色、大小等等設(shè)置來(lái)創(chuàng)建一個(gè)默認(rèn)段落
  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.關(guān)閉文檔
  document.close();
 }


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

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

  // 建立一個(gè)書(shū)寫(xiě)器(Writer)與document對(duì)象關(guān)聯(lián),通過(guò)書(shū)寫(xiě)器(Writer)可以將文檔寫(xiě)入到磁盤(pán)中。
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c://ITextTest.pdf"));

  // 打開(kāi)文件
  document.open();

  // 標(biāo)題
  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();
  // 向文檔中添加內(nèi)容
  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)));

  // 新建章節(jié)
  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);

  // 關(guān)閉文檔
  document.close();
 }
 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 视频一区二区三区在线观看 | 欧美大片一级毛片 | 国产精品成人久久久久a级 欧美特黄一级高清免费的香蕉 | 欧美日韩亚洲视频 | 国产在线精品91 | 亚洲第一综合 | 欧美一级鲁丝片免费看 | 国产日韩久久久久69影院 | 97伦理 | 免费一级特黄毛片 | 天天骑夜夜操 | 视频一区二区视频 | 日本在线视频二区 | 最新av网址在线观看 | 深夜网站在线观看 | 亚洲特黄a级毛片在线播放 久久久入口 | 国产午夜电影 | 国产色视频一区 | 久久久电影电视剧免费看 | 免费一级在线观看 | 久久久久久久.comav | 亚洲影视在线 | 国产欧美精品一区二区三区四区 | 视频一区二区不卡 | av电影在线观看网址 | 色婷婷一区二区三区 | 久久人人做 | 91成人午夜性a一级毛片 | 国产精品一区在线观看 | 狠狠干五月 | 色吧综合网| 欧美一级在线免费 | 欧美激情性色生活片在线观看 | 欧美日韩国产成人在线观看 | 一区二区视 | 国产婷婷一区二区三区 | 日韩欧美电影一区二区三区 | 史上最强炼体老祖动漫在线观看 | 久久久久久亚洲综合影院红桃 | 中文字幕在线视频日本 | 精品一区二区三区日本 |