Java生成PDF文件的實(shí)例代碼
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盤(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)添加文本??梢杂梦谋炯捌淠J(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();
}
}
相關(guān)文章
SpringSecurity Jwt Token 自動(dòng)刷新的實(shí)現(xiàn)
這篇文章主要介紹了SpringSecurity Jwt Token 自動(dòng)刷新的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié)
這篇文章主要介紹了SpringBoot 在項(xiàng)目啟動(dòng)之后執(zhí)行自定義方法的兩種方式小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信)
這篇文章主要介紹了java實(shí)現(xiàn)基于TCP協(xié)議網(wǎng)絡(luò)socket編程(C/S通信),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Jenkins遷移job插件Job Import Plugin流程詳解
這篇文章主要介紹了Jenkins遷移job插件Job Import Plugin流程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
java實(shí)現(xiàn)支付寶支付接口的調(diào)用
本文主要介紹了java實(shí)現(xiàn)支付寶支付接口的調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
使用JAR包中MANIFEST.MF的注意事項(xiàng)
這篇文章主要介紹了使用JAR包中MANIFEST.MF的注意事項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java實(shí)現(xiàn)刪除排序鏈表中的重復(fù)元素的方法
這篇文章主要介紹了Java實(shí)現(xiàn)刪除排序鏈表中的重復(fù)元素的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
spring boot 利用注解實(shí)現(xiàn)權(quán)限驗(yàn)證的實(shí)現(xiàn)代碼
這篇文章主要介紹了spring boot 利用注解實(shí)現(xiàn)權(quán)限驗(yàn)證的實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11

