欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java生成PDF文件的實例代碼

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

復制代碼 代碼如下:

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對象關聯(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 來添加文本??梢杂梦谋炯捌淠J的字體、顏色、大小等等設置來創(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.關閉文檔
  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對象關聯(lián),通過書寫器(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();
  // 向文檔中添加內(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);

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


相關文章

  • Java?POI導出Excel時合并單元格沒有邊框的問題解決

    Java?POI導出Excel時合并單元格沒有邊框的問題解決

    這篇文章主要給大家介紹了關于Java?POI導出Excel時合并單元格沒有邊框的問題解決辦法,文中通過代碼介紹的非常詳細,對大家學習或者使用java具有一定的參考學習價值,需要的朋友可以參考下
    2023-07-07
  • Java打印數(shù)組的三種方法整理

    Java打印數(shù)組的三種方法整理

    許多學編程專業(yè)的同學面試的時候,考官都會問到Java如何打印數(shù)組這樣的問題,下面這篇文章主要給大家介紹了關于Java打印數(shù)組的三種方法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Base64與File之間的相互轉(zhuǎn)化方式

    Base64與File之間的相互轉(zhuǎn)化方式

    這篇文章主要介紹了Base64與File之間的相互轉(zhuǎn)化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • Java 手動解析不帶引號的JSON字符串的操作

    Java 手動解析不帶引號的JSON字符串的操作

    這篇文章主要介紹了Java 手動解析不帶引號的JSON字符串的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • java 泛型的詳解及實例

    java 泛型的詳解及實例

    這篇文章主要介紹了java 泛型的詳解及實例的相關資料,希望通過本文大家能徹底掌握泛型的使用方法,需要的朋友可以參考下
    2017-08-08
  • SpringBoot整合RabbitMQ實現(xiàn)RPC遠程調(diào)用功能

    SpringBoot整合RabbitMQ實現(xiàn)RPC遠程調(diào)用功能

    在分布式系統(tǒng)中,RPC(Remote?Procedure?Call)是一種常用的通信機制,它可以讓不同的節(jié)點之間像調(diào)用本地函數(shù)一樣進行函數(shù)調(diào)用,隱藏了底層的網(wǎng)絡通信細節(jié),通過本教程,你可以了解RPC的基本原理以及如何使用Java實現(xiàn)一個簡單的RPC客戶端和服務端
    2023-06-06
  • Eclipse IDE可支持Java 14編程

    Eclipse IDE可支持Java 14編程

    這篇文章主要介紹了Eclipse IDE可支持Java 14編程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Spring-webflux訪問關系型數(shù)據(jù)庫實戰(zhàn)

    Spring-webflux訪問關系型數(shù)據(jù)庫實戰(zhàn)

    這篇文章主要為大家介紹了Spring-webflux訪問關系型數(shù)據(jù)庫實戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • java -length的三種用法說明

    java -length的三種用法說明

    這篇文章主要介紹了java -length的三種用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • easycode配置成mybatis-plus模板的實現(xiàn)方法

    easycode配置成mybatis-plus模板的實現(xiàn)方法

    本文主要介紹了easycode配置成mybatis-plus模板的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09

最新評論