iText初探之HelloWorld (一)
来源:互联网 发布:东莞网络推广培训班 编辑:程序博客网 时间:2024/05/17 07:03
如今应用程序中生成PDF的功能很常用,然而Java本身并不好实现这个功能,因为Java本身并没有提供处理PDF的API。可是开源框架iText提供了非常丰富的功能API。
iText是一个开源的框架,它可以让你的应用程序你动态的创建PDF。
首先,要想使用它,我们得获取它的jar包。可以在这里下载lib库以及src文件:
https://github.com/itext/itextpdf/releases/tag/5.5.9
下面是一个简单的例子
package com.sy.pdf;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import com.itextpdf.text.Chapter;import com.itextpdf.text.Chunk;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Font;import com.itextpdf.text.FontFactory;import com.itextpdf.text.Paragraph;import com.itextpdf.text.pdf.PdfWriter;/** * Example written by Bruno Lowagie in answer to: * http://stackoverflow.com/questions/28431148/itext-chapter-font-overrides-paragraph-font */public class ChapterAndTitle { public static final String DEST = "results/objects/chapter_title.pdf"; public static void main(String[] args) throws IOException, DocumentException { File file = new File(DEST); file.getParentFile().mkdirs(); new ChapterAndTitle().createPdf(DEST); } public void createPdf(String dest) throws IOException, DocumentException { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(dest)); document.open(); Font chapterFont = FontFactory.getFont(FontFactory.HELVETICA, 16, Font.BOLDITALIC); Font paragraphFont = FontFactory.getFont(FontFactory.HELVETICA, 12, Font.NORMAL); //这是可以添加到文档中的最小的文本的一部分。 Chunk chunk = new Chunk("This is the title", chapterFont); //章节Chapter 段落Paragraph Chapter chapter = new Chapter(new Paragraph(chunk), 1); chapter.setNumberDepth(0); chapter.add(new Paragraph("This is the paragraph", paragraphFont)); document.add(chapter); document.close(); System.out.println("生成pdf文件成功------->" + dest); }}执行完这段代码后就在项目路径results/objects下生成chapter_title.pdf文件。
下面就是生成的PDF文件效果。
1 0
- iText初探之HelloWorld (一)
- iText入门之HelloWorld
- ITEXT实例学习与研究(一) 之 HELLOWORLD的实现,解决中文问题,ITEXT框架
- 一、Matlab 之 初探
- Scala 之初探(一)
- Spring Boot(Web)初探一( 入门【HelloWorld】)
- JNI入门之HelloWorld(一)
- MiniGUI编程之Helloworld(一)
- 学习SpringMVC(一)之helloworld
- (一) spring boot 之 helloworld
- Struts2(一)-Struts2之HelloWorld
- 初探hibernate3(3.6.10.final)之helloworld
- scrapy(一)之初探爬虫
- Spring笔记之(一)初探
- WebRTC之初探(一)
- python学习(一)之初探
- Qt之Qwt初探(一)
- React Native HelloWorld初探
- MTK最新最全的智能机roadmap(珍藏版)
- 百科—最短路
- Android中Path类的lineTo方法和quadTo方法画线的区别
- Eclipse下修改工程名
- Fortran 当数组大小不一样时,会出现什么情况。
- iText初探之HelloWorld (一)
- Unity 5.4公開測試,免費用戶也能下載使用!
- android 选择图片后对图片压缩
- base64转换,c代码实现
- java 常用类 日期类 Date SimpleDateFormat Calendar
- Eclipse快捷键 10个最有用的快捷键
- 混合高斯模型(Mixtures of Gaussians)和EM算法
- Android进阶之ListView实现多条数据同时倒计时功能
- 遍历Map的四种方法