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盤生成一個(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í)合并單元格沒有邊框的問題解決
這篇文章主要給大家介紹了關(guān)于Java?POI導(dǎo)出Excel時(shí)合并單元格沒有邊框的問題解決辦法,文中通過代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-07-07Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作
這篇文章主要介紹了Java 手動(dòng)解析不帶引號(hào)的JSON字符串的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-10-10SpringBoot整合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-06Spring-webflux訪問關(guān)系型數(shù)據(jù)庫實(shí)戰(zhàn)
這篇文章主要為大家介紹了Spring-webflux訪問關(guān)系型數(shù)據(jù)庫實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07easycode配置成mybatis-plus模板的實(shí)現(xiàn)方法
本文主要介紹了easycode配置成mybatis-plus模板的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09