package com.platform.common.util.test;
import java.awt.Color;
import java.io.FileOutputStream;
import com.lowagie.text.*;
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.A3, 20, 20, 20, 20);
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("c:\\ITextTest.pdf"));
Image jpg = Image.getInstance("d:/test.jpg");
jpg.setAlignment(Image.ALIGN_CENTER);
// 打开文件
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(jpg);
// document.add(new Paragraph("data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAB9AAAASwCAYAAACjJNG+AAAgAElEQVR4nOzdz4/ra0Ln9/mfasfGm+xql41Xk5VVEbAozdBCQIpEQfwwGgnSchLdRC2LSySEO+oh6E7RyUAz7YhWT0/akEHREKc7ICI0zDXTSVCSpkWQnln0dcnl+tqPy89T55zv/bxe0ndzT9n145y3HnV/yl//vQIAAAAAAAAAlL/3vr8AAAAAAAAAAPgQGNABAAAAAAAAoBjQAQAAAAAAAKCUYkAHAAAAAAAAgFKKAR0AAAAAAAAASikGdAAAAAAAAAAopRjQAQAAAAAAAKCUYkAHAAAAAAAAgFKKAR0AAIAR2+12Zb1eP7s2m82bf97tdvvi82632zf9fNfabDYvvtbdbnf187U89vBr6qXl6xn62bzl3yMAAAAfPgM6AABAgKGh+a2udzlAzmazcnNz8+xaLBZv/nkXi8U7+7zb7bbc3NyU29vbMp/Py+Pj46seP/QzWq/XV3890+m03N7eluVyefF4vVwuy8PDw7OvZbVaXf01lPLDf9Pz+bzc3Ny8+mey977+/QAAAPDhMqADAAAEWK/XL4bCt7rODZCvHfLPDbTL5XLw88/n81f9XK65Hh4eXnzeh4eHq5/v3Pe5Wq1efK7b29tnP9Nzj+85oA/9O3p4eKg+7vb29uz38FrL5bJMJpOn55pMJlf94oYBHQAAgGMGdAAAgAAfyoD+2q/j1NC72WzOPu7SVyS/q5/Jtd9nKaXc39+f/SWB/cA+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QVqvVyffiPr5Nee0a+kWD29vbs4+pDejHP4Nz7wv+mlvFDw3Ny+Xy7GOGfvHg+Hs4/vew3W7fZEA/9b71Qz/T1zKgAwAAvB9f+cpXnkb03/u93zv5cZcO6N/97nfLT/3UT5W7u7vy8z//802vQjegAwAABBga0E+9CvnUe0cPDYu9BvRSXr4KfbVald1uV5bLZbm/vy+TyeTZaLtarc4OukOviN6/n3qrS17Rf6w2oB9/zPH3d/g53mKoPve9HN9m//hW/ntDY/3x8+5vgT7081gsFs/+/e3f832IAR0AAGDcfud3fudpRP/kk08GP+aSAX2z2ZQf/dEfLXd3d2WxWJQf/OAHTV+XAR0AACDApQP60Gh7OJoev4K754B+PHjf398/vYr88NoPt0OveD4cW49foX742FJe3hL8NdfQcz88PJx9zPHHT6fTF6/43g/Q++f6EAb0oX8T+9vGH177X3ZYLpdls9mc/bse+h4O39/94eGhevt9AzoAAMD4ff3rX38a0b/85S+/+PPagP77v//7T4//9V//9S5fkwEdAAAgwPEriIcG9KGxemj0PdRzQB+6jXsp5cWtzA9fRX78Z4fvg378Z5d87e/r2v/M9sP8drv9IAb0c79QUfv3NHQHgEOtw7UBHQAA4PPhW9/61tMI/vHHHz/7s3MD+m/91m89Pe63f/u3u309BnQAAIAAQ0Ph4au1d7tdmUwmrxpXSzk/oA/devs1A/r+z47H/8Mh/NT7oA+9Ov349u0f4oC+v8V57Wf7rr6eoRH81HVs6K4Hh97FgH7q9u/nnsOADgAA8O798R//cfmxH/uxcnd3Vz766KOn/35qQP+1X/u1p/H861//etevxYAOAAAQYOi9qfd2u93J964+fhX3/tq/2vjcyLter8vt7W1ZrVZPn+vcgP74+Dj4Z+du43488O7H9aHh95Lbz7/vAX273T6NvrWf7aXX0N/tcrk8+5j9z/eSuxKcGq/v7++f/fnxe76/9YC+//f32ucwoAMAALwf3/nOd8oXvvCFcnd3V37lV36lfP/7338xoH//+98vX/ziF8vd3V358R//8fJHf/RH3b8OAzoAAECA45Hw8FXcQyPizc0PX8293W4HX5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