Document的几U构造函敎ͼ(x) public Document(); public Document(Rectangle pageSize); public Document(Rectangle pageSize, int marginLeft, int marginRight, int marginTop, int marginBottom); 下面两种比较有用Q如果是你想定义U张大小和边~的时候。对于MarginQiText上提到“You can also change the margins while you are adding content. Note that the changes will only be noticed on the NEXT page. If you want the margins mirrored (odd and even pages), you can do this with this method: setMarginMirroring(true). ”不q,对于tablegq不好。tableq不?x)?jin)理会(x)你设定的marginQ如果想改变它的maginq是需要去改变它的宽度QsetWidthQ?br /> 2 pdf表单
使用PdfStamper是可以填充pdf表单的,q样qZ(jin)一U很好的报表生成思\?br />word制作报表样式-->acrobat转pdf-->itext填充数据-->输出pdf q做非常单,因ؓ(f)可以比较Ҏ(gu)的控制pdf的样式。我对于Java的报表工具了(jin)解的q不多,不过在jasperreportsQ即使用GUI工具做一个样式比较复杂的报表也不是怎么Ҏ(gu)。比如有那种斜线的表_(d)比较花哨的嵌套表根{这L(fng)情况q是比较多见的,客户不会(x)关系你实现v来是否困难。不q想要用这U方式也有不的地方。首先是acrobat把word转化成pdf的时候,格式L保持不好Q特别的是字体。然后是文g的体U这L(fng)成的pdf?x)比直接用iText生成的pdf文g大很多,acrobat在pdf里加入了(jin)太多无用的信息。初ơ用iText填充Adobe Designer生成的pdf表单时会(x)有点麻?ch)。在Designer中设计了(jin)一个name的text文本框的l定名ؓ(f)name。照着iText中例子用用PdfStamper的setFieldҎ(gu)去这样写form.setField("name", "XXXX");q不?x)成功。原因是Adobe Designer生成的表单名都是h层次的,它可能是q个样子form1[0].#subform[0].name[0]。不q我们可以用一个方法把它们列出来,只要做一ơ就知道l构?jin),可以使用cM下面的代码:(x) PdfReader reader = new PdfReader("form.pdf"); PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("registered_flat.pdf")); AcroFields form = stamp.getAcroFields(); for (Iterator it = form.getFields().keySet().iterator(); it .hasNext();) { System.out.println(it.next()); } 如果直接用iText~程生成的表单就不会(x)有这L(fng)问题Q设定的什么名字就是什么名字?br /> 3 表单元素
/**
* @author
* TODO 学习(fn)用例 ,pdw2009@tom.com
*
*/
import org.jdom.*;
import org.jdom.input.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.*;
import java.util.*;
public class readabc {
public static void main(String[] args) throws Exception{
SAXBuilder sb=new SAXBuilder();//建立构造器
Document doc=sb.build(new FileInputStream("E:\\eclipse\\myworkspace\\base\\src\\jdom\\abc.xml"));
Element root=doc.getRootElement(); //获得根结?
List list=root.getChildren(); //所有根l点下的子结Ҏ(gu)入list
for(int i=0;i<list.size();i++){
System.out.println("----------------------");
Element item=(Element)list.get(i); //获得实体l体
1 一个xml文g输出C个字W串对象?
XMLOutputter outputter = new XMLOutputter();
Element element = new Element("Greeting");
String hello = outputter.outputString(element);
/**
* @author pdw2009@tom.com
* TODO 一个List对象,转换一个XML文g
*/
class Person{
private String ID; //学号
private String name; //姓名
private String addr; //地址
private String tel; //联系?sh)?
private String email; //email地址
/**
* @return Returns the addr.
*/
public String getAddr() {
return addr;
}
/**
* @param addr The addr to set.
*/
public void setAddr(String addr) {
this.addr = addr;
}
/**
* @return Returns the iD.
*/
public String getID() {
return ID;
}
/**
* @param id The iD to set.
*/
public void setID(String id) {
ID = id;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the tel.
*/
public String getTel() {
return tel;
}
/**
* @param tel The tel to set.
*/
public void setTel(String tel) {
this.tel = tel;
}
/**
* @return Returns the email.
*/
public String getEmail() {
return email;
}
/**
* @param email The email to set.
*/
public void setEmail(String email) {
this.email = email;
}
}
public class listtoxml {
public List creatlist(){
List list=new ArrayList();
Person person=new Person();
person.setAddr("q西合");
person.setID("1048");
person.setName("hfggf");
person.setTel("138777778888");
person.setEmail("pdw2009@tom.com");
list.add(person);
person.setAddr("q西南宁");
person.setID("1036");
person.setName("蠢卢");
person.setTel("1387778888");
person.setEmail("XXXXX@tom.com");
list.add(person);
return list;
}
public static void main(String[] args) throws Exception {
listtoxml lx=new listtoxml();
List list=lx.creatlist(); //生成Personcȝ对象list
Element root=new Element("Student");//Ҏ(gu)?
Iterator it=list.iterator(); //获取iterator接口
while(it.hasNext()){
Person p=(Person)it.next();
Element person=new Element("Person");
person.setAttribute("ID",p.getID());
root.addContent(person); //在根元素?d一个person子元?
Element ele=new Element("Name");
ele.setText(p.getName());
person.addContent(ele); //在person元素下添加一个name的子元素
Element addr=new Element("Address");
addr.setText(p.getAddr());
person.addContent(addr);
Element tel=new Element("tel");
tel.setText(p.getTel());
person.addContent(tel);
Element email=new Element("email");
email.setText(p.getEmail());
person.addContent(email);