java 新建参数接受pdf_【文件】java生成PDF文件

packagetest;importjava.awt.Color;importjava.io.FileOutputStream;importorg.junit.Test;importcom.lowagie.text.Chapter;importcom.lowagie.text.Document;importcom.lowagie.text.Font;importcom.lowagie.text.FontFactory;importcom.lowagie.text.PageSize;importcom.lowagie.text.Paragraph;importcom.lowagie.text.Section;importcom.lowagie.text.pdf.PdfWriter;public classPDFTest {

@Testpublic void writeSimplePdf() throwsException{//1.新建document对象//第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。

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

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\TMP\\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文件

*@throwsException*/@Testpublic void writeCharpter() throwsException{//新建document对象 第一个参数是页面大小。接下来的参数分别是左、右、上和下页边距。

Document document = new Document(PageSize.A4, 20, 20, 20, 20);//建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。

PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:\\TMP\\ITextTest.pdf"));//标题

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.open();//向文档中添加内容

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值