当前位置: 移动技术网 > IT编程>开发语言>Java > Java生成PDF文件的实例代码

Java生成PDF文件的实例代码

2019年07月22日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:package com.qhdstar.java.pdf; import java.awt.color;import java.io.fileoutp

复制代码 代码如下:

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


如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网