Java生成PDF文件的实例代码

时间:2022-05-24 07:22:37 


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();
 }
 


标签:PDF,文件
0
投稿

猜你喜欢

  • Java编程中ArrayList源码分析

    2023-05-20 00:05:25
  • Java的后台文件夹下文件的遍历完整代码

    2023-09-18 12:32:19
  • Android Studio实现音乐播放器

    2021-07-01 14:02:43
  • C# 中的动态创建组件(属性及事件)的实现思路及方法

    2021-07-20 04:58:31
  • java仿windows记事本小程序

    2023-11-25 09:24:43
  • 使用IDEA配置Maven搭建开发框架ssm教程

    2023-04-05 13:33:21
  • Netty分布式FastThreadLocal的set方法实现逻辑剖析

    2021-08-22 04:51:54
  • Mybatis实现Mapper动态代理方式详解

    2023-08-13 08:37:41
  • Java中的ThreadLocal详解

    2023-01-02 16:02:21
  • C#开发教程之利用特性自定义数据导出到Excel

    2023-03-02 22:55:42
  • DevExpress实现为TextEdit设置水印文字的方法

    2021-11-11 14:27:32
  • Java Socket实现单线程通信的方法示例

    2022-04-22 15:43:02
  • Java如何构造DSL方法重构

    2021-10-10 02:04:09
  • 基于Spring-Security自定义登陆错误提示信息

    2021-09-20 17:33:40
  • Spring MVC过滤器-登录过滤的代码实现

    2021-06-05 15:01:43
  • openCV中meanshift算法查找目标的实现

    2023-04-03 21:24:21
  • C#面向对象特征的具体实现及作用详解

    2023-11-14 06:43:39
  • 深入理解C#序列化与反序列化的详解

    2022-06-23 05:11:58
  • Java中\\n和\\r区别

    2023-01-16 10:38:53
  • Java web spring异步方法实现步骤解析

    2023-12-19 03:14:58
  • asp之家 软件编程 m.aspxhome.com