最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java生成PDF文件的實例代碼

 更新時間:2013年05月09日 09:56:09   作者:  
Java生成PDF文件的實例代碼,需要的朋友可以參考一下

復(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 songyanjun_stars@126.com
 * @date 2013-4-6
 * @version V1.0
 */
public class GeneratePDF {

 public static void main(String[] args) {

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

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

  
 }
 

 public static void writeSimplePdf() throws Exception {

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

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

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

  // 4.向文檔中添加內(nèi)容
  // 通過 com.lowagie.text.Paragraph 來添加文本。可以用文本及其默認的字體、顏色、大小等等設(shè)置來創(chuàng)建一個默認段落
  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對象 第一個參數(shù)是頁面大小。接下來的參數(shù)分別是左、右、上和下頁邊距。
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

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

  // 打開文件
  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();
 }
 


相關(guān)文章

  • SpringSecurity Jwt Token 自動刷新的實現(xiàn)

    SpringSecurity Jwt Token 自動刷新的實現(xiàn)

    這篇文章主要介紹了SpringSecurity Jwt Token 自動刷新的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Java JSON提取工具JsonExtractor的使用

    Java JSON提取工具JsonExtractor的使用

    本文主要介紹了Java JSON提取工具JsonExtractor的使用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • SpringBoot 在項目啟動之后執(zhí)行自定義方法的兩種方式小結(jié)

    SpringBoot 在項目啟動之后執(zhí)行自定義方法的兩種方式小結(jié)

    這篇文章主要介紹了SpringBoot 在項目啟動之后執(zhí)行自定義方法的兩種方式小結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java實現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)

    java實現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)

    這篇文章主要介紹了java實現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Jenkins遷移job插件Job Import Plugin流程詳解

    Jenkins遷移job插件Job Import Plugin流程詳解

    這篇文章主要介紹了Jenkins遷移job插件Job Import Plugin流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • java實現(xiàn)支付寶支付接口的調(diào)用

    java實現(xiàn)支付寶支付接口的調(diào)用

    本文主要介紹了java實現(xiàn)支付寶支付接口的調(diào)用,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • 使用JAR包中MANIFEST.MF的注意事項

    使用JAR包中MANIFEST.MF的注意事項

    這篇文章主要介紹了使用JAR包中MANIFEST.MF的注意事項,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java實現(xiàn)刪除排序鏈表中的重復(fù)元素的方法

    Java實現(xiàn)刪除排序鏈表中的重復(fù)元素的方法

    這篇文章主要介紹了Java實現(xiàn)刪除排序鏈表中的重復(fù)元素的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Spring學(xué)習(xí)教程之AOP模塊的概述

    Spring學(xué)習(xí)教程之AOP模塊的概述

    AOP 從功能的角度來講,可能看作OOP編程方式的一種補充,提供了一種不同的代碼或者系統(tǒng)組織方式,下面這篇文章主要給大家介紹了關(guān)于Spring學(xué)習(xí)教程之AOP模塊的相關(guān)資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2018-05-05
  • spring boot 利用注解實現(xiàn)權(quán)限驗證的實現(xiàn)代碼

    spring boot 利用注解實現(xiàn)權(quán)限驗證的實現(xiàn)代碼

    這篇文章主要介紹了spring boot 利用注解實現(xiàn)權(quán)限驗證的實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11

最新評論

醴陵市| 洛阳市| 白河县| 湘阴县| 开阳县| 老河口市| 紫金县| 曲阜市| 盘锦市| 康乐县| 镇巴县| 静安区| 龙里县| 克拉玛依市| 贵定县| 万年县| 景宁| 平乐县| 沿河| 明水县| 红河县| 顺平县| 永泰县| 靖安县| 外汇| 玛纳斯县| 凤城市| 林口县| 金华市| 东阳市| 莲花县| 石门县| 上虞市| 鸡西市| 达日县| 吴忠市| 丘北县| 禹州市| 宁德市| 根河市| 阜城县|