itext笔记

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.eclipse.swt.widgets.Text;

import com.lowagie.text.Anchor;
import com.lowagie.text.Chapter;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.GreekList;
import com.lowagie.text.List;
import com.lowagie.text.ListItem;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.RomanList;
import com.lowagie.text.Section;
import com.lowagie.text.ZapfDingbatsList;
import com.lowagie.text.ZapfDingbatsNumberList;
import com.lowagie.text.pdf.PdfWriter;


public class BasePdf {

public Document document=new Document();

public static void main(String[] args) throws DocumentException {
BasePdf base=new BasePdf();
base.createMixed();
base.testOtherStyleList();
base.testBookMarks();
base.document.close();
}



public void createMixed()
{
/*A Chunk is the smallest significant part of text that can be added to a document.
It’s the atomic building block of most of the other high-level text objects. A Chunk
contains a String of which all the characters have the same font, font size, font
style, font color, rendition, and so forth.*/

/*
* Chunk有几个不同的构造方法new Chunk();
*
*
*
*/

try {
PdfWriter writer=PdfWriter.getInstance(document, new FileOutputStream("c:\\demo.pdf"));
document.open();
Font font = new Font(Font.COURIER, 10, Font.BOLD);
font.setColor(new Color(0xFF, 0xFF, 0xFF));

Chunk fox = new Chunk("quick brown fox", font);
fox.setBackground(new Color(0xa5, 0x2a, 0x2a));
Chunk jumps = new Chunk(" jumps over ", new Font());
Chunk dog = new Chunk("the lazy dog",new Font(Font.TIMES_ROMAN, 14, Font.ITALIC));
/*A phrase, on the other hand, is defined as “a string of
words.” It isn’t solid; it’s a composed object. I thought it was a good word to use to
refer to a concatenation of chunks. Translated to iText and Java, a Phrase is an
ArrayList of Chunk objects.
*
*/
Phrase phrase=new Phrase();
phrase.add(fox);
phrase.add(jumps);
phrase.add(dog);

//constructor1
Phrase phrase1=new Phrase("constructor11111111111111111111111111111");
//constructor2
Phrase phrase2=new Phrase(new Chunk("constructor22222222222222222222222",new Font(Font.COURIER,12,Font.BOLD)));
//constructor3
Phrase phrase3=new Phrase("constructor33333333333333333",new Font(Font.COURIER,12));

/*
* The Paragraph class is derived from Phrase; this means you can create a Paragraph
* and specify the leading, but you also can do much more.
*/
Paragraph p=new Paragraph();
p.add(phrase1);
p.add(phrase2);
p.add(phrase3);

p.setAlignment(Element.ALIGN_LEFT);
document.add(p);
p.setAlignment(Element.ALIGN_CENTER);
document.add(p);

document.add(getAnchor());

//增加矛点
addInternalAnchor();
//List
testListItem();
}catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

//test external anchor
public Anchor getAnchor()
{
Anchor anchor=new Anchor("this is a link,you can test!");
//set external link
anchor.setReference("www.sina.com");
return anchor;
}
//test internal anchor
public void addInternalAnchor() throws DocumentException
{
Paragraph p=new Paragraph("internal");
//添加一个茅点
Anchor anchor=new Anchor("destionation");
//指定锚点
anchor.setReference("#destionation");

p.add(anchor);
p.add("pass by !!!!!!!!!");
document.add(p);
//换页
document.newPage();
//
Anchor anchor1=new Anchor("Link !");
//设定锚点到达的位置
anchor1.setName("destionation");
document.add(anchor1);

}
/*
* ListItem is a subclass of Paragraph. A ListItem has the same functionality as a
Paragraph (such as leading and indentation), except for two differences:
■ You can’t add a ListItem to a document directly. You have to add ListItem
objects to a List.
■ The classes List and ListItem have a member variable that represents the
list symbol.
*/
public void testListItem() throws DocumentException
{
List list1=new List(List.ORDERED,20);
list1.add(new ListItem("the lazy dog"));
document.add(list1);

List list2 = new List(List.UNORDERED, 10);
list2.add("the lazy cat");
document.add(list2);

List list3 = new List(List.ORDERED, List.ALPHABETICAL, 20);
list3.add(new ListItem("the fence"));
document.add(list3);

List list4 = new List(List.UNORDERED, 30);
list4.setListSymbol("----->");
list4.setIndentationLeft(10);
list4.add("the lazy dog");
document.add(list4);

List list5 = new List(List.ORDERED, 20);
list5.setFirst(11);
list5.add(new ListItem("the lazy cat"));
document.add(list5);

//nested list
List list = new List(List.UNORDERED, 10);

list.add(list1);
list.add(list3);
list.add(list5);
document.add(list);

}

public void testOtherStyleList() throws DocumentException
{
/*
* RomanList and GreekList work well if your list has no more than 26 or 24 items.
*/
RomanList romanlist = new RomanList(20);
romanlist.setRomanLower(false);
romanlist.add(new ListItem("the lazy dog"));
document.add(romanlist);

GreekList greeklist = new GreekList(20);
greeklist.setGreekLower(true);
greeklist.add(new ListItem("the lazy cat"));
document.add(greeklist);

ZapfDingbatsList zapfdingbatslist = new ZapfDingbatsList(42, 15);
zapfdingbatslist.add(new ListItem("the lazy dog"));
zapfdingbatslist.add(new ListItem("the lazy cat"));
document.add(zapfdingbatslist);

//ZapfDingbatsNumberList has no more than 10 words
ZapfDingbatsNumberList zapfdingbatsnumberlist
= new ZapfDingbatsNumberList(0, 15);
zapfdingbatsnumberlist.add(new ListItem("the lazy cat"));
document.add(zapfdingbatsnumberlist);
System.out.println("success");
}

public void testBookMarks() throws DocumentException
{
//在左侧增加一个章节
Chapter chapter1=new Chapter(new Paragraph(""), 1);
chapter1.add(new Paragraph("chapter add: "));

Section section=chapter1.addSection("quick",0);
Section section1=chapter1.addSection("2222222",1);

section.add(new Paragraph("section adddd: "));
document.add(chapter1);
//section nested section
section1.add(section);
Chapter chapter2=new Chapter("",0);
chapter2.addSection("").add(section);

chapter1.setBookmarkTitle("bookMark title");
chapter1.setBookmarkOpen(false);
:oops: document.add(chapter2);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值