公共服务订阅(throws exception )。
//1 .新建文档对象
//第一个参数是页面大小。 以下参数分别为左、右、上、下页边距:
文档文档=新文档(pagesize.a4,50,50,50,50 );
//2 .将“写入器”(Writer )与文档对象相关联,以便“写入器”(Writer )可以将文档写入磁盘。
创建PdfWriter对象的第一个参数是对文档对象的引用,第二个参数是文件的实际名称,该名称还具有输出路径。
pdf写入器写入器=pdf写入器. getinstance (document,new file output stream (c : (itext test.pdf ) );
//3 .打开文档
document.open (;
//4 .向文档中添加内容
在com.lowagie.text.Paragraph中添加文本。 可以使用文本及其默认字体、颜色、大小等设置创建默认段落
文档. add (new paragraph (firstpageofthedocument.' );
document.add (somemoretextonthefirstpagewithdifferentcolorandfonttype.',
font factory.get font (font factory.courier,14,Font.BOLD,新颜色(255,150,200 ) )
//5 .关闭文档
document.close (;
}
//*
添加包含*章的pdf文件
* @throws Exception
*/
publicstaticvoidwritecharpter throws exception {
//新document对象的第一个参数是页面大小。 以下参数分别为左、右、上、下页边距:
文档文档=新文档(pagesize.a4,20,20,20,20 );
//可以创建“写入器”(Writer )以与文档对象关联,然后使用“写入器”(Writer )将文档写入磁盘。
pdf写入器写入器=pdf写入器. getinstance (document,new file output stream (c : (itext test.pdf ) );
//打开文件
document.open (;
//标题
document.add title (hellomingriexample );
//作者
文档. add author (wolf );
//主题
document.add subject (' thisexampleexplainshowtoaddmetadata.';
document.addKeywords(itext,Hello mingri );
document.add creator (myprogramusingitext );
//document.newPage (;
//将内容添加到文档
文档. add (new paragraph ((n ) );
文档. add (new paragraph ((n ) );
文档. add (new paragraph ((n ) );
文档. add (new paragraph ((n ) );
文档. add (new paragraph ((n ) );
文档. add (new paragraph (firstpageofthedocument.' );
文档. add (new paragraph (firstpageofthedocument.' );
文档. add (new paragraph (firstpageofthedocument.' );
文档. add (new paragraph (firstpageofthedocument.' );
document.add (somemoretextonthefirstpagewithdifferentcolorandfonttype.',
font factory.get font (font factory.default encoding,10,Font.BOLD,new color (0,0,0 ) )
paragraph title1=new paragraph (' chapter1),
font factory.get font (font factory.Helvetica,18,Font.BOLDITALIC,new color (0,0,255 ) )
//创造新的篇章
chapter chapter1=new chapter (title 1,1 );
chapter1.setnumberdepth(0;
paragraph title 11=new paragraph (this is section1in chapter1),
font factory.get font (font factory.Helvetica,16,Font.BOLD,newcolor ) 255,0,0 ) )
第一节=chapter1. add section (title 11;
paragraphsomesectiontext=new paragraph (thistextcomesaspartofsection1of chapter1. ' );
section1.add(somesectiontext;
somesection text=new paragraph (' following is a3 x2 table.';
section1.add(somesectiontext;
document.add(chapter1;
//关闭文档
document.close (;
}