您的位置:首页 > 其它

ITEXT实例学习与研究(二) 之 创建一个细长的浅黄色背景的页面以及纵向页面与横向页面之间的切换

2012-02-17 17:44 537 查看
iTextSharp.text.Document-object共有三个构造函数:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
int marginRight,
int marginTop,
int marginBottom);
第一个构造函数以A4页面作为参数调用第二个构造函数,第二个构造函数以每边36磅页边距为参数调用第三个构造函数
u 页面尺寸:
你可以通过指定的颜色和大小创建你自己的页面,下面的示例代码创建一个细长的浅黄色背景的页面:
Rectangle pageSize = new Rectangle(144, 720);
//pageSize.BackgroundColor = new Color(0xFF, 0xFF, 0xDE);
pageSize.setBorderColor(newBaseColor(0xFF, 0xFF, 0xDE));
Document document = new Document(pageSize);
通常,你不必创建这样的页面,而可以从下面页面尺寸中选择:
A0-A10, LEGAL, LETTER, HALFLETTER,_11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA 和 FLSE

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Chap0102 {

public static void main(String[] args) {

System.out.println("Chapter 1 example 2: PageSize");

// step 1: creation of a document-object
Rectangle pageSize = new Rectangle(144, 720);
pageSize.setBackgroundColor(new java.awt.Color(0xFF, 0xFF, 0xDE));
Document document = new Document(pageSize);

try {

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileOutputStream("Chap0102.pdf"));

// step 3: we open the document
document.open();

// step 4: we add some paragraphs to the document
for (int i = 0; i < 5; i++) {
document.add(new Paragraph("Hello World"));
}

}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.close();
}
}


大多数情况下使用纵向页面,如果希望使用横向页面,你只须使用rotate()函数:
Document document = new Document(PageSize.A4.rotate());
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Chap0103 {

public static void main(String[] args) {

System.out.println("Chapter 1 example 3: PageSize");

// step 1: creation of a document-object
Document document = new Document(PageSize.A4.rotate());

try {

// step 2:
// we create a writer that listens to the document
// and directs a PDF-stream to a file

PdfWriter.getInstance(document, new FileOutputStream("Chap0103.pdf"));

// step 3: we open the document
document.open();

// step 4: we add some phrases to the document
for (int i = 0; i < 20; i++) {
document.add(new Phrase("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. "));
}

}
catch(DocumentException de) {
System.err.println(de.getMessage());
}
catch(IOException ioe) {
System.err.println(ioe.getMessage());
}

// step 5: we close the document
document.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐