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

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

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

復(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)用第一個(gè)方法,向C盤生成一個(gè)名字為ITextTest.pdf 的文件
  try {
   writeSimplePdf();
  }
  catch (Exception e) { e.printStackTrace(); }

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

  
 }
 

 public static void writeSimplePdf() throws Exception {

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

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

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

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

  // 建立一個(gè)書寫器(Writer)與document對(duì)象關(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)文章

  • Java?POI導(dǎo)出Excel時(shí)合并單元格沒有邊框的問題解決

    Java?POI導(dǎo)出Excel時(shí)合并單元格沒有邊框的問題解決

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

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

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

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

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

    Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作

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

    java 泛型的詳解及實(shí)例

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

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

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

    Eclipse IDE可支持Java 14編程

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

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

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

    java -length的三種用法說明

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

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

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

最新評(píng)論