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) {

  //调用第一个方法,向C盘生成一个名字为ITextTest.pdf 的文件
  try {
   writeSimplePdf();
  }
  catch (Exception e) { e.printStackTrace(); }

  
  //调用第二个方法,向C盘名字为ITextTest.pdf的文件,添加章节。
  try {
   writeCharpter();
  }
  catch (Exception e) { e.printStackTrace(); }

  
 }
 

 public static void writeSimplePdf() throws Exception {

  // 1.新建document对象
  // 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
  Document document = new Document(PageSize.A4, 50, 50, 50, 50);

  // 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
  // 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
  PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));

  // 3.打开文档
  document.open();

  // 4.向文档中添加内容
  // 通过 com.lowagie.text.Paragraph 来添加文本。可以用文本及其默认的字体、颜色、大小等等设置来创建一个默认段落
  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();
 }


 /**
  * 添加含有章节的pdf文件
  *
  * @throws Exception
  */
 public static void writeCharpter() throws Exception {

  // 新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。
  Document document = new Document(PageSize.A4, 20, 20, 20, 20);

  // 建立一个书写器(Writer)与document对象关联,通过书写器(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();
  // 向文档中添加内容
  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)));

  // 新建章节
  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();
 }
 


相关文章

  • SpringSecurity Jwt Token 自动刷新的实现

    SpringSecurity Jwt Token 自动刷新的实现

    这篇文章主要介绍了SpringSecurity Jwt Token 自动刷新的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • Java JSON提取工具JsonExtractor的使用

    Java JSON提取工具JsonExtractor的使用

    本文主要介绍了Java JSON提取工具JsonExtractor的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-05-05
  • SpringBoot 在项目启动之后执行自定义方法的两种方式小结

    SpringBoot 在项目启动之后执行自定义方法的两种方式小结

    这篇文章主要介绍了SpringBoot 在项目启动之后执行自定义方法的两种方式小结,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • java实现基于TCP协议网络socket编程(C/S通信)

    java实现基于TCP协议网络socket编程(C/S通信)

    这篇文章主要介绍了java实现基于TCP协议网络socket编程(C/S通信),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-10-10
  • Jenkins迁移job插件Job Import Plugin流程详解

    Jenkins迁移job插件Job Import Plugin流程详解

    这篇文章主要介绍了Jenkins迁移job插件Job Import Plugin流程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-08-08
  • java实现支付宝支付接口的调用

    java实现支付宝支付接口的调用

    本文主要介绍了java实现支付宝支付接口的调用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • 使用JAR包中MANIFEST.MF的注意事项

    使用JAR包中MANIFEST.MF的注意事项

    这篇文章主要介绍了使用JAR包中MANIFEST.MF的注意事项,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • Java实现删除排序链表中的重复元素的方法

    Java实现删除排序链表中的重复元素的方法

    这篇文章主要介绍了Java实现删除排序链表中的重复元素的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-02-02
  • Spring学习教程之AOP模块的概述

    Spring学习教程之AOP模块的概述

    AOP 从功能的角度来讲,可能看作OOP编程方式的一种补充,提供了一种不同的代码或者系统组织方式,下面这篇文章主要给大家介绍了关于Spring学习教程之AOP模块的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2018-05-05
  • spring boot 利用注解实现权限验证的实现代码

    spring boot 利用注解实现权限验证的实现代码

    这篇文章主要介绍了spring boot 利用注解实现权限验证的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11

最新评论